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_audioTrackProducers(),
48 m_videoTrackProducers(),
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 KUrl url = KUrl(xml.attribute("resource"));
77 if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
79 if (xml.hasAttribute("duration")) {
80 setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
82 int out = xml.attribute("out").toInt();
83 int in = xml.attribute("in").toInt();
84 setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
87 if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
89 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
91 // Setup timer to trigger audio thumbs creation
92 m_audioTimer.setSingleShot(true);
93 m_audioTimer.setInterval(800);
94 connect(&m_audioTimer, SIGNAL(timeout()), m_thumbProd, SLOT(slotCreateAudioThumbs()));
98 DocClipBase::~DocClipBase()
103 qDeleteAll(m_toDeleteProducers);
104 m_toDeleteProducers.clear();
105 qDeleteAll(m_baseTrackProducers);
106 m_baseTrackProducers.clear();
107 qDeleteAll(m_audioTrackProducers);
108 m_audioTrackProducers.clear();
109 qDeleteAll(m_videoTrackProducers);
110 m_videoTrackProducers.clear();
113 void DocClipBase::setZone(QPoint zone)
115 m_properties.insert("zone_in", QString::number(zone.x()));
116 m_properties.insert("zone_out", QString::number(zone.y()));
119 QPoint DocClipBase::zone() const
121 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
126 bool DocClipBase::hasAudioThumb() const
128 if (m_clipType == AUDIO || m_clipType == AV || m_clipType == PLAYLIST) return true;
132 void DocClipBase::slotClearAudioCache()
134 m_audioFrameCache.clear();
135 m_audioThumbCreated = false;
138 /*void DocClipBase::getClipMainThumb() {
139 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
142 KThumb *DocClipBase::thumbProducer()
147 bool DocClipBase::audioThumbCreated() const
149 return m_audioThumbCreated;
152 const QString DocClipBase::name() const
155 return m_properties.value("name");
158 const QString &DocClipBase::getId() const
163 const CLIPTYPE & DocClipBase::clipType() const
168 void DocClipBase::setClipType(CLIPTYPE type)
171 m_properties.insert("type", QString::number((int) type));
174 KUrl DocClipBase::fileURL() const
176 QString res = m_properties.value("resource");
177 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
181 void DocClipBase::setClipThumbFrame(const uint &ix)
183 m_properties.insert("thumbnail", QString::number((int) ix));
186 uint DocClipBase::getClipThumbFrame() const
188 return (uint) m_properties.value("thumbnail").toInt();
191 const QString DocClipBase::description() const
193 return m_properties.value("description");
196 bool DocClipBase::isTransparent() const
198 return (m_properties.value("transparency") == "1");
201 const QString DocClipBase::getProperty(const QString &prop) const
203 return m_properties.value(prop);
206 void DocClipBase::setDuration(GenTime dur)
209 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
212 const GenTime &DocClipBase::duration() const
217 const GenTime DocClipBase::maxDuration() const
219 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
220 /*const GenTime dur(15000, KdenliveSettings::project_fps());
227 bool DocClipBase::hasFileSize() const
232 qulonglong DocClipBase::fileSize() const
234 return m_properties.value("file_size").toULongLong();
238 QDomElement DocClipBase::toXML(bool hideTemporaryProperties) const
241 QDomElement clip = doc.createElement("producer");
243 QMapIterator<QString, QString> i(m_properties);
244 while (i.hasNext()) {
246 if (hideTemporaryProperties && i.key().startsWith('_')) continue;
247 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
249 doc.appendChild(clip);
250 if (!m_cutZones.isEmpty()) {
252 for (int i = 0; i < m_cutZones.size(); i++) {
253 CutZoneInfo info = m_cutZones.at(i);
254 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
256 clip.setAttribute("cutzones", cuts.join(";"));
258 //kDebug() << "/// CLIP XML: " << doc.toString();
259 return doc.documentElement();
263 void DocClipBase::setAudioThumbCreated(bool isDone)
265 m_audioThumbCreated = isDone;
268 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
270 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
271 m_audioFrameCache = data;
272 m_audioThumbCreated = true;
276 QList < GenTime > DocClipBase::snapMarkers() const
278 QList < GenTime > markers;
280 for (int count = 0; count < m_snapMarkers.count(); ++count) {
281 markers.append(m_snapMarkers.at(count).time());
287 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
289 return m_snapMarkers;
293 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
295 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
296 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
297 if ((*it).time() >= time)
301 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
302 (*it).setComment(comment);
303 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
305 CommentedTime t(time, comment);
306 m_snapMarkers.insert(it, t);
311 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
313 QList < CommentedTime >::Iterator it;
314 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
315 if ((*it).time() == time)
318 if (it != m_snapMarkers.end()) {
319 (*it).setComment(comment);
321 kError() << "trying to edit Snap Marker that does not already exists";
325 QString DocClipBase::deleteSnapMarker(const GenTime & time)
327 QString result = i18n("Marker");
328 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
330 while (itt != m_snapMarkers.end()) {
331 if ((*itt).time() == time)
336 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
337 result = (*itt).comment();
338 m_snapMarkers.erase(itt);
344 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
346 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
348 while (itt != m_snapMarkers.end()) {
349 if ((*itt).time() == time)
357 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
360 for (it = 0; it < m_snapMarkers.count(); it++) {
361 if (m_snapMarkers.at(it).time() >= currTime)
364 if (it == 0) return GenTime();
365 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
366 return m_snapMarkers.at(it).time();
367 else return m_snapMarkers.at(it - 1).time();
370 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
373 for (it = 0; it < m_snapMarkers.count(); it++) {
374 if (m_snapMarkers.at(it).time() > currTime)
377 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
381 QString DocClipBase::markerComment(GenTime t)
383 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
385 while (itt != m_snapMarkers.end()) {
386 if ((*itt).time() == t)
387 return (*itt).comment();
393 void DocClipBase::clearThumbProducer()
395 if (m_thumbProd) m_thumbProd->clearProducer();
398 void DocClipBase::reloadThumbProducer()
400 if (m_thumbProd && !m_thumbProd->hasProducer())
401 m_thumbProd->setProducer(getProducer());
404 void DocClipBase::deleteProducers()
406 if (m_thumbProd) m_thumbProd->clearProducer();
408 if (numReferences() > 0 && (!m_baseTrackProducers.isEmpty() || !m_videoTrackProducers.isEmpty() || !m_audioTrackProducers.isEmpty())) {
409 // Clip is used in timeline, delay producers deletion
410 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
411 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
413 for (int i = 0; i < m_videoTrackProducers.count(); i++) {
414 m_toDeleteProducers.append(m_videoTrackProducers.at(i));
416 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
417 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
421 qDeleteAll(m_baseTrackProducers);
422 qDeleteAll(m_videoTrackProducers);
423 qDeleteAll(m_audioTrackProducers);
424 m_replaceMutex.unlock();
426 m_baseTrackProducers.clear();
427 m_videoTrackProducers.clear();
428 m_audioTrackProducers.clear();
431 void DocClipBase::cleanupProducers()
435 kDebug()<<"----------------------------------------------------------------------------------";
436 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
437 if (m_toDeleteProducers.at(i) != NULL) {
438 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
439 if (props.ref_count() > 2) {
440 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
448 qDeleteAll(m_toDeleteProducers);
449 m_toDeleteProducers.clear();
450 m_replaceMutex.unlock();
454 bool DocClipBase::isClean() const
456 return m_toDeleteProducers.isEmpty();
459 void DocClipBase::setValid()
461 m_placeHolder = false;
464 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
466 if (producer == NULL) return;
468 QMutexLocker locker(&m_producerMutex);
469 m_replaceMutex.lock();
472 QString id = producer->get("id");
473 if (m_placeHolder || !producer->is_valid()) {
474 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
475 producer->set("markup", tmp);
476 producer->set("bgcolour", "0xff0000ff");
477 producer->set("pad", "10");
480 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
481 if (m_clipType != AUDIO) {
482 if (!id.endsWith("_audio"))
483 m_thumbProd->setProducer(producer);
485 else m_thumbProd->setProducer(producer);
488 bool updated = false;
489 if (id.contains('_')) {
490 // this is a subtrack producer, insert it at correct place
491 id = id.section('_', 1);
492 if (id.endsWith("audio")) {
493 int pos = id.section('_', 0, 0).toInt();
494 if (pos >= m_audioTrackProducers.count()) {
495 while (m_audioTrackProducers.count() - 1 < pos) {
496 m_audioTrackProducers.append(NULL);
499 if (m_audioTrackProducers.at(pos) == NULL) {
500 m_audioTrackProducers[pos] = producer;
503 else delete producer;
505 } else if (id.endsWith("video")) {
506 int pos = id.section('_', 0, 0).toInt();
507 if (pos >= m_videoTrackProducers.count()) {
508 while (m_videoTrackProducers.count() - 1 < pos) {
509 m_videoTrackProducers.append(NULL);
512 if (m_videoTrackProducers.at(pos) == NULL) {
513 m_videoTrackProducers[pos] = producer;
516 else delete producer;
519 int pos = id.toInt();
520 if (pos >= m_baseTrackProducers.count()) {
521 while (m_baseTrackProducers.count() - 1 < pos) {
522 m_baseTrackProducers.append(NULL);
525 if (m_baseTrackProducers.at(pos) == NULL) {
526 m_baseTrackProducers[pos] = producer;
529 else delete producer;
531 if (m_baseTrackProducers.isEmpty()) {
532 m_baseTrackProducers.append(producer);
535 else if (m_baseTrackProducers.at(0) == NULL) {
536 m_baseTrackProducers[0] = producer;
539 else delete producer;
541 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
542 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
545 static double getPixelAspect(QMap<QString, QString>& props) {
546 int width = props.value("frame_size").section('x', 0, 0).toInt();
547 int height = props.value("frame_size").section('x', 1, 1).toInt();
548 int aspectNumerator = props.value("force_aspect_num").toInt();
549 int aspectDenominator = props.value("force_aspect_den").toInt();
550 if (aspectDenominator != 0 && width != 0)
551 return double(height) * aspectNumerator / aspectDenominator / width;
556 Mlt::Producer *DocClipBase::audioProducer(int track)
558 QMutexLocker locker(&m_producerMutex);
559 if (m_audioTrackProducers.count() <= track) {
560 while (m_audioTrackProducers.count() - 1 < track) {
561 m_audioTrackProducers.append(NULL);
564 if (m_audioTrackProducers.at(track) == NULL) {
566 for (i = 0; i < m_audioTrackProducers.count(); i++)
567 if (m_audioTrackProducers.at(i) != NULL) break;
569 if (i >= m_audioTrackProducers.count()) {
570 // Could not find a valid producer for that clip
572 base = getProducer();
578 else base = m_audioTrackProducers.at(i);
579 m_audioTrackProducers[track] = cloneProducer(base);
580 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
582 return m_audioTrackProducers.at(track);
586 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
588 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
589 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
590 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
591 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
592 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
593 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
594 if (mute) prod->set("audio_index", -1);
595 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
596 if (blind) prod->set("video_index", -1);
597 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
598 prod->set("id", id.toUtf8().constData());
599 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
600 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
601 if (m_properties.contains("proxy_out")) {
602 // We have a proxy clip, make sure the proxy has same duration as original
603 prod->set("length", m_properties.value("duration").toInt());
604 prod->set("out", m_properties.value("proxy_out").toInt());
609 Mlt::Producer *DocClipBase::videoProducer(int track)
611 QMutexLocker locker(&m_producerMutex);
612 if (m_videoTrackProducers.count() <= track) {
613 while (m_videoTrackProducers.count() - 1 < track) {
614 m_videoTrackProducers.append(NULL);
617 if (m_videoTrackProducers.at(track) == NULL) {
619 for (i = 0; i < m_videoTrackProducers.count(); i++)
620 if (m_videoTrackProducers.at(i) != NULL) break;
622 if (i >= m_videoTrackProducers.count()) {
623 // Could not find a valid producer for that clip
625 base = getProducer();
631 else base = m_videoTrackProducers.at(i);
632 m_videoTrackProducers[track] = cloneProducer(base);
633 adjustProducerProperties(m_videoTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_video"), true, false);
635 return m_videoTrackProducers.at(track);
638 Mlt::Producer *DocClipBase::getCloneProducer()
640 Mlt::Producer *source = NULL;
641 Mlt::Producer *prod = NULL;
642 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
643 source = getProducer();
644 if (!source) return NULL;
646 if (m_clipType == COLOR) {
647 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
648 } else if (m_clipType == TEXT) {
649 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
650 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
651 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
655 QMutexLocker locker(&m_producerMutex);
656 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
657 if (m_baseTrackProducers.at(i) != NULL) {
658 source = m_baseTrackProducers.at(i);
662 if (!source) return NULL;
664 prod = cloneProducer(source);
667 adjustProducerProperties(prod, getId() + "_", false, false);
668 if (!m_properties.contains("proxy_out")) {
669 // Adjust length in case...
670 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
671 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
678 Mlt::Producer *DocClipBase::getProducer(int track)
680 QMutexLocker locker(&m_producerMutex);
681 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
682 if (m_baseTrackProducers.count() == 0) {
685 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
686 if (m_baseTrackProducers.at(i) != NULL) {
687 return m_baseTrackProducers.at(i);
692 if (track >= m_baseTrackProducers.count()) {
693 while (m_baseTrackProducers.count() - 1 < track) {
694 m_baseTrackProducers.append(NULL);
697 if (m_baseTrackProducers.at(track) == NULL) {
699 for (i = 0; i < m_baseTrackProducers.count(); i++)
700 if (m_baseTrackProducers.at(i) != NULL) break;
702 if (i >= m_baseTrackProducers.count()) {
703 // Could not find a valid producer for that clip, check in
706 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
707 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
708 m_baseTrackProducers[track] = prod;
710 return m_baseTrackProducers.at(track);
714 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
716 Mlt::Producer *result = NULL;
717 QString url = QString::fromUtf8(source->get("resource"));
718 if (url == "<playlist>" || url == "<tractor>" || url == "<producer>") {
719 // Xml producer sometimes loses the correct url
720 url = m_properties.value("resource");
722 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
723 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
725 if (result == NULL || !result->is_valid()) {
727 QString txt = "+" + i18n("Missing clip") + ".txt";
728 char *tmp = qstrdup(txt.toUtf8().constData());
729 result = new Mlt::Producer(*source->profile(), tmp);
731 if (result == NULL || !result->is_valid())
732 result = new Mlt::Producer(*(source->profile()), "colour:red");
734 result->set("bgcolour", "0xff0000ff");
735 result->set("pad", "10");
739 /*Mlt::Properties src_props(source->get_properties());
740 Mlt::Properties props(result->get_properties());
741 props.inherit(src_props);*/
745 void DocClipBase::setProducerProperty(const char *name, int data)
747 QMutexLocker locker(&m_producerMutex);
748 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
749 if (m_baseTrackProducers.at(i) != NULL)
750 m_baseTrackProducers[i]->set(name, data);
754 void DocClipBase::setProducerProperty(const char *name, double data)
756 QMutexLocker locker(&m_producerMutex);
757 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
758 if (m_baseTrackProducers.at(i) != NULL)
759 m_baseTrackProducers[i]->set(name, data);
763 void DocClipBase::setProducerProperty(const char *name, const char *data)
765 QMutexLocker locker(&m_producerMutex);
766 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
767 if (m_baseTrackProducers.at(i) != NULL)
768 m_baseTrackProducers[i]->set(name, data);
772 void DocClipBase::resetProducerProperty(const char *name)
774 QMutexLocker locker(&m_producerMutex);
775 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
776 if (m_baseTrackProducers.at(i) != NULL)
777 m_baseTrackProducers[i]->set(name, (const char*) NULL);
781 const char *DocClipBase::producerProperty(const char *name) const
783 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
784 if (m_baseTrackProducers.at(i) != NULL) {
785 return m_baseTrackProducers.at(i)->get(name);
792 void DocClipBase::slotRefreshProducer()
794 if (m_baseTrackProducers.count() == 0) return;
795 if (m_clipType == SLIDESHOW) {
796 setProducerProperty("ttl", getProperty("ttl").toInt());
797 //m_clipProducer->set("id", getProperty("id"));
798 if (!getProperty("animation").isEmpty()) {
799 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
801 Mlt::Filter *filter = clipService.filter(ct);
803 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
805 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
806 clipService.detach(*filter);
808 filter = clipService.filter(ct);
811 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
812 // filter does not exist, create it.
813 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
814 if (filter && filter->is_valid()) {
815 int cycle = getProperty("ttl").toInt();
816 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
817 if (!geometry.isEmpty()) {
818 if (getProperty("animation").contains("low-pass")) {
819 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
820 if (blur && blur->is_valid())
821 clipService.attach(*blur);
823 filter->set("transition.geometry", geometry.toUtf8().data());
824 filter->set("transition.cycle", cycle);
825 clipService.attach(*filter);
830 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
832 Mlt::Filter *filter = clipService.filter(0);
834 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
835 clipService.detach(*filter);
837 filter = clipService.filter(ct);
840 if (getProperty("fade") == "1") {
841 // we want a fade filter effect
842 kDebug() << "//////////// FADE WANTED";
843 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
845 Mlt::Filter *filter = clipService.filter(ct);
847 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
851 filter = clipService.filter(ct);
854 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
855 filter->set("cycle", getProperty("ttl").toInt());
856 filter->set("duration", getProperty("luma_duration").toInt());
857 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
858 if (!getProperty("softness").isEmpty()) {
859 int soft = getProperty("softness").toInt();
860 filter->set("luma.softness", (double) soft / 100.0);
863 // filter does not exist, create it...
864 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
865 filter->set("cycle", getProperty("ttl").toInt());
866 filter->set("duration", getProperty("luma_duration").toInt());
867 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
868 if (!getProperty("softness").isEmpty()) {
869 int soft = getProperty("softness").toInt();
870 filter->set("luma.softness", (double) soft / 100.0);
872 clipService.attach(*filter);
875 kDebug() << "//////////// FADE NOT WANTED!!!";
876 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
878 Mlt::Filter *filter = clipService.filter(0);
880 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
881 clipService.detach(*filter);
883 filter = clipService.filter(ct);
886 if (getProperty("crop") == "1") {
887 // we want a center crop filter effect
888 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
890 Mlt::Filter *filter = clipService.filter(ct);
892 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
896 filter = clipService.filter(ct);
899 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
900 // filter does not exist, create it...
901 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
902 filter->set("center", 1);
903 clipService.attach(*filter);
906 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
908 Mlt::Filter *filter = clipService.filter(0);
910 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
911 clipService.detach(*filter);
913 filter = clipService.filter(ct);
919 void DocClipBase::setProperties(QMap <QString, QString> properties)
921 // changing clip type is not allowed
922 properties.remove("type");
923 QMapIterator<QString, QString> i(properties);
924 bool refreshProducer = false;
926 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
927 QString oldProxy = m_properties.value("proxy");
928 while (i.hasNext()) {
930 setProperty(i.key(), i.value());
931 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
933 if (properties.contains("proxy")) {
934 QString value = properties.value("proxy");
935 // If value is "-", that means user manually disabled proxy on this clip
936 if (value.isEmpty() || value == "-") {
938 emit abortProxy(m_id, oldProxy);
941 emit createProxy(m_id);
944 if (refreshProducer) slotRefreshProducer();
947 void DocClipBase::setMetadata(QMap <QString, QString> properties)
949 QMapIterator<QString, QString> i(properties);
950 while (i.hasNext()) {
952 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
953 m_metadata.remove(i.key());
955 m_metadata.insert(i.key(), i.value());
960 QMap <QString, QString> DocClipBase::metadata() const
965 void DocClipBase::clearProperty(const QString &key)
967 m_properties.remove(key);
970 void DocClipBase::getFileHash(const QString &url)
972 if (m_clipType == SLIDESHOW) return;
974 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
977 //kDebug() << "SETTING HASH of" << value;
978 m_properties.insert("file_size", QString::number(file.size()));
980 * 1 MB = 1 second per 450 files (or faster)
981 * 10 MB = 9 seconds per 450 files (or faster)
983 if (file.size() > 1000000*2) {
984 fileData = file.read(1000000);
985 if (file.seek(file.size() - 1000000))
986 fileData.append(file.readAll());
988 fileData = file.readAll();
990 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
991 m_properties.insert("file_hash", QString(fileHash.toHex()));
995 bool DocClipBase::checkHash() const
997 KUrl url = fileURL();
998 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
1002 QString DocClipBase::getClipHash() const
1005 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
1006 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
1007 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
1009 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
1010 if (hash.isEmpty()) hash = getHash(fileURL().path());
1016 void DocClipBase::setPlaceHolder(bool place)
1018 m_placeHolder = place;
1022 QString DocClipBase::getHash(const QString &path)
1025 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1026 QByteArray fileData;
1027 QByteArray fileHash;
1029 * 1 MB = 1 second per 450 files (or faster)
1030 * 10 MB = 9 seconds per 450 files (or faster)
1032 if (file.size() > 1000000*2) {
1033 fileData = file.read(1000000);
1034 if (file.seek(file.size() - 1000000))
1035 fileData.append(file.readAll());
1037 fileData = file.readAll();
1039 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1044 void DocClipBase::refreshThumbUrl()
1046 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1049 void DocClipBase::setProperty(const QString &key, const QString &value)
1051 m_properties.insert(key, value);
1052 if (key == "resource") {
1054 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1055 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1056 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1057 else if (key == "colour") {
1058 setProducerProperty("colour", value.toUtf8().data());
1059 } else if (key == "templatetext") {
1060 setProducerProperty("templatetext", value.toUtf8().data());
1061 setProducerProperty("force_reload", 1);
1062 } else if (key == "xmldata") {
1063 setProducerProperty("xmldata", value.toUtf8().data());
1064 setProducerProperty("force_reload", 1);
1065 } else if (key == "force_aspect_num") {
1066 if (value.isEmpty()) {
1067 m_properties.remove("force_aspect_num");
1068 resetProducerProperty("force_aspect_ratio");
1069 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1070 } else if (key == "force_aspect_den") {
1071 if (value.isEmpty()) {
1072 m_properties.remove("force_aspect_den");
1073 resetProducerProperty("force_aspect_ratio");
1074 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1075 } else if (key == "force_fps") {
1076 if (value.isEmpty()) {
1077 m_properties.remove("force_fps");
1078 resetProducerProperty("force_fps");
1079 } else setProducerProperty("force_fps", value.toDouble());
1080 } else if (key == "force_progressive") {
1081 if (value.isEmpty()) {
1082 m_properties.remove("force_progressive");
1083 resetProducerProperty("force_progressive");
1084 } else setProducerProperty("force_progressive", value.toInt());
1085 } else if (key == "force_tff") {
1086 if (value.isEmpty()) {
1087 m_properties.remove("force_tff");
1088 resetProducerProperty("force_tff");
1089 } else setProducerProperty("force_tff", value.toInt());
1090 } else if (key == "threads") {
1091 if (value.isEmpty()) {
1092 m_properties.remove("threads");
1093 setProducerProperty("threads", 1);
1094 } else setProducerProperty("threads", value.toInt());
1095 } else if (key == "video_index") {
1096 if (value.isEmpty()) {
1097 m_properties.remove("video_index");
1098 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1099 } else setProducerProperty("video_index", value.toInt());
1100 } else if (key == "audio_index") {
1101 if (value.isEmpty()) {
1102 m_properties.remove("audio_index");
1103 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1104 } else setProducerProperty("audio_index", value.toInt());
1105 } else if (key == "force_colorspace") {
1106 if (value.isEmpty()) {
1107 m_properties.remove("force_colorspace");
1108 resetProducerProperty("force_colorspace");
1109 } else setProducerProperty("force_colorspace", value.toInt());
1110 } else if (key == "full_luma") {
1111 if (value.isEmpty()) {
1112 m_properties.remove("full_luma");
1113 resetProducerProperty("set.force_full_luma");
1114 } else setProducerProperty("set.force_full_luma", value.toInt());
1118 QMap <QString, QString> DocClipBase::properties() const
1120 return m_properties;
1123 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1125 QMap <QString, QString> currentProps;
1126 QMap<QString, QString>::const_iterator i = props.constBegin();
1127 while (i != props.constEnd()) {
1128 currentProps.insert(i.key(), m_properties.value(i.key()));
1131 return currentProps;
1134 bool DocClipBase::getAudioThumbs()
1136 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1137 if (m_audioThumbCreated) {
1140 m_audioTimer.start();
1144 bool DocClipBase::isPlaceHolder() const
1146 return m_placeHolder;
1149 void DocClipBase::addCutZone(int in, int out, QString desc)
1152 info.zone = QPoint(in, out);
1153 info.description = desc;
1154 for (int i = 0; i < m_cutZones.count(); i++)
1155 if (m_cutZones.at(i).zone == info.zone) {
1158 m_cutZones.append(info);
1161 bool DocClipBase::hasCutZone(QPoint p) const
1163 for (int i = 0; i < m_cutZones.count(); i++)
1164 if (m_cutZones.at(i).zone == p) return true;
1169 void DocClipBase::removeCutZone(int in, int out)
1172 for (int i = 0; i < m_cutZones.count(); i++) {
1173 if (m_cutZones.at(i).zone == p) {
1174 m_cutZones.removeAt(i);
1180 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1182 QPoint old(oldin, oldout);
1183 for (int i = 0; i < m_cutZones.size(); ++i) {
1184 if (m_cutZones.at(i).zone == old) {
1186 info.zone = QPoint(in, out);
1187 info.description = desc;
1188 m_cutZones.replace(i, info);
1194 QList <CutZoneInfo> DocClipBase::cutZones() const
1199 bool DocClipBase::hasVideoCodec(const QString &codec) const
1201 Mlt::Producer *prod = NULL;
1202 if (m_baseTrackProducers.count() == 0) return false;
1203 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1204 if (m_baseTrackProducers.at(i) != NULL) {
1205 prod = m_baseTrackProducers.at(i);
1210 if (!prod) return false;
1211 int default_video = prod->get_int("video_index");
1213 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1214 return prod->get(property) == codec;
1217 bool DocClipBase::hasAudioCodec(const QString &codec) const
1219 Mlt::Producer *prod = NULL;
1220 if (m_baseTrackProducers.count() == 0) return false;
1221 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1222 if (m_baseTrackProducers.at(i) != NULL) {
1223 prod = m_baseTrackProducers.at(i);
1227 if (!prod) return false;
1228 int default_video = prod->get_int("audio_index");
1230 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1231 return prod->get(property) == codec;
1235 void DocClipBase::slotExtractImage(QList <int> frames)
1237 if (m_thumbProd == NULL) return;
1238 m_thumbProd->extractImage(frames);
1241 QImage DocClipBase::extractImage(int frame, int width, int height)
1243 if (m_thumbProd == NULL) return QImage();
1244 QMutexLocker locker(&m_producerMutex);
1245 return m_thumbProd->extractImage(frame, width, height);