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, 3), attributes.item(i).nodeValue());
68 } else m_properties.insert(name, attributes.item(i).nodeValue());
71 if (xml.hasAttribute("cutzones")) {
72 QStringList cuts = xml.attribute("cutzones").split(';', QString::SkipEmptyParts);
73 for (int i = 0; i < cuts.count(); i++) {
74 QString z = cuts.at(i);
75 addCutZone(z.section('-', 0, 0).toInt(), z.section('-', 1, 1).toInt(), z.section('-', 2, 2));
79 if (xml.hasAttribute("analysisdata")) {
80 QStringList adata = xml.attribute("analysisdata").split('#', QString::SkipEmptyParts);
81 for (int i = 0; i < adata.count(); i++)
82 m_analysisdata.insert(adata.at(i).section('?', 0, 0), adata.at(i).section('?', 1, 1));
85 KUrl url = KUrl(xml.attribute("resource"));
86 if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
88 if (xml.hasAttribute("duration")) {
89 setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
91 int out = xml.attribute("out").toInt();
92 int in = xml.attribute("in").toInt();
93 setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
96 if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
98 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
100 // Setup timer to trigger audio thumbs creation
101 m_audioTimer.setSingleShot(true);
102 m_audioTimer.setInterval(800);
103 connect(&m_audioTimer, SIGNAL(timeout()), m_thumbProd, SLOT(slotCreateAudioThumbs()));
107 DocClipBase::~DocClipBase()
112 qDeleteAll(m_toDeleteProducers);
113 m_toDeleteProducers.clear();
114 qDeleteAll(m_baseTrackProducers);
115 m_baseTrackProducers.clear();
116 qDeleteAll(m_audioTrackProducers);
117 m_audioTrackProducers.clear();
118 qDeleteAll(m_videoTrackProducers);
119 m_videoTrackProducers.clear();
122 void DocClipBase::setZone(QPoint zone)
124 m_properties.insert("zone_in", QString::number(zone.x()));
125 m_properties.insert("zone_out", QString::number(zone.y()));
128 QPoint DocClipBase::zone() const
130 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
135 bool DocClipBase::hasAudioThumb() const
137 if (m_clipType == AUDIO || m_clipType == AV || m_clipType == PLAYLIST) return true;
141 void DocClipBase::slotClearAudioCache()
143 audioFrameCache.clear();
144 m_audioThumbCreated = false;
147 /*void DocClipBase::getClipMainThumb() {
148 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
151 KThumb *DocClipBase::thumbProducer()
156 bool DocClipBase::audioThumbCreated() const
158 return m_audioThumbCreated;
161 const QString DocClipBase::name() const
164 return m_properties.value("name");
167 const QString &DocClipBase::getId() const
172 const CLIPTYPE & DocClipBase::clipType() const
177 void DocClipBase::setClipType(CLIPTYPE type)
180 m_properties.insert("type", QString::number((int) type));
183 KUrl DocClipBase::fileURL() const
185 QString res = m_properties.value("resource");
186 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
190 void DocClipBase::setClipThumbFrame(const uint &ix)
192 m_properties.insert("thumbnail", QString::number((int) ix));
195 uint DocClipBase::getClipThumbFrame() const
197 return (uint) m_properties.value("thumbnail").toInt();
200 const QString DocClipBase::description() const
202 return m_properties.value("description");
205 bool DocClipBase::isTransparent() const
207 return (m_properties.value("transparency") == "1");
210 const QString DocClipBase::getProperty(const QString &prop) const
212 return m_properties.value(prop);
215 void DocClipBase::setDuration(GenTime dur)
218 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
221 const GenTime &DocClipBase::duration() const
226 const GenTime DocClipBase::maxDuration() const
228 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
229 /*const GenTime dur(15000, KdenliveSettings::project_fps());
236 bool DocClipBase::hasFileSize() const
241 qulonglong DocClipBase::fileSize() const
243 return m_properties.value("file_size").toULongLong();
247 QDomElement DocClipBase::toXML(bool hideTemporaryProperties) const
250 QDomElement clip = doc.createElement("producer");
252 QMapIterator<QString, QString> i(m_properties);
253 while (i.hasNext()) {
255 if (hideTemporaryProperties && i.key().startsWith('_')) continue;
256 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
258 doc.appendChild(clip);
259 if (!m_cutZones.isEmpty()) {
261 for (int i = 0; i < m_cutZones.size(); i++) {
262 CutZoneInfo info = m_cutZones.at(i);
263 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
265 clip.setAttribute("cutzones", cuts.join(";"));
268 if (!m_analysisdata.isEmpty()) {
269 QMapIterator<QString, QString> i(m_analysisdata);
270 while (i.hasNext()) {
272 //WARNING: a ? and # separator is not a good idea
273 adata.append(i.key() + "?" + i.value() + "#");
276 clip.setAttribute("analysisdata", adata);
277 //kDebug() << "/// CLIP XML: " << doc.toString();
278 return doc.documentElement();
282 void DocClipBase::setAudioThumbCreated(bool isDone)
284 m_audioThumbCreated = isDone;
287 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
289 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
290 audioFrameCache = data;
291 m_audioThumbCreated = true;
295 QList < GenTime > DocClipBase::snapMarkers() const
297 QList < GenTime > markers;
298 for (int count = 0; count < m_snapMarkers.count(); ++count) {
299 markers.append(m_snapMarkers.at(count).time());
305 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
307 return m_snapMarkers;
311 void DocClipBase::addSnapMarker(const CommentedTime marker)
313 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
314 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
315 if ((*it).time() >= marker.time())
319 if ((it != m_snapMarkers.end()) && ((*it).time() == marker.time())) {
320 (*it).setComment(marker.comment());
321 (*it).setMarkerType(marker.markerType());
322 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
324 m_snapMarkers.insert(it, marker);
328 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
330 QList < CommentedTime >::Iterator it;
331 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
332 if ((*it).time() == time)
335 if (it != m_snapMarkers.end()) {
336 (*it).setComment(comment);
338 kError() << "trying to edit Snap Marker that does not already exists";
342 QString DocClipBase::deleteSnapMarker(const GenTime & time)
344 QString result = i18n("Marker");
345 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
347 while (itt != m_snapMarkers.end()) {
348 if ((*itt).time() == time)
353 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
354 result = (*itt).comment();
355 m_snapMarkers.erase(itt);
361 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
363 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
365 while (itt != m_snapMarkers.end()) {
366 if ((*itt).time() == time)
374 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
377 for (it = 0; it < m_snapMarkers.count(); it++) {
378 if (m_snapMarkers.at(it).time() >= currTime)
381 if (it == 0) return GenTime();
382 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
383 return m_snapMarkers.at(it).time();
384 else return m_snapMarkers.at(it - 1).time();
387 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
390 for (it = 0; it < m_snapMarkers.count(); it++) {
391 if (m_snapMarkers.at(it).time() > currTime)
394 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
398 QString DocClipBase::markerComment(GenTime t) const
400 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
401 while (itt != m_snapMarkers.end()) {
402 if ((*itt).time() == t)
403 return (*itt).comment();
409 CommentedTime DocClipBase::markerAt(GenTime t) const
411 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
412 while (itt != m_snapMarkers.end()) {
413 if ((*itt).time() == t)
417 return CommentedTime();
420 void DocClipBase::clearThumbProducer()
422 if (m_thumbProd) m_thumbProd->clearProducer();
425 void DocClipBase::reloadThumbProducer()
427 if (m_thumbProd && !m_thumbProd->hasProducer())
428 m_thumbProd->setProducer(getProducer());
431 void DocClipBase::deleteProducers()
433 if (m_thumbProd) m_thumbProd->clearProducer();
435 if (numReferences() > 0 && (!m_baseTrackProducers.isEmpty() || !m_videoTrackProducers.isEmpty() || !m_audioTrackProducers.isEmpty())) {
436 // Clip is used in timeline, delay producers deletion
437 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
438 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
440 for (int i = 0; i < m_videoTrackProducers.count(); i++) {
441 m_toDeleteProducers.append(m_videoTrackProducers.at(i));
443 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
444 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
448 qDeleteAll(m_baseTrackProducers);
449 qDeleteAll(m_videoTrackProducers);
450 qDeleteAll(m_audioTrackProducers);
451 m_replaceMutex.unlock();
453 m_baseTrackProducers.clear();
454 m_videoTrackProducers.clear();
455 m_audioTrackProducers.clear();
458 void DocClipBase::cleanupProducers()
462 kDebug()<<"----------------------------------------------------------------------------------";
463 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
464 if (m_toDeleteProducers.at(i) != NULL) {
465 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
466 if (props.ref_count() > 2) {
467 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
475 qDeleteAll(m_toDeleteProducers);
476 m_toDeleteProducers.clear();
477 m_replaceMutex.unlock();
481 bool DocClipBase::isClean() const
483 return m_toDeleteProducers.isEmpty();
486 void DocClipBase::setValid()
488 m_placeHolder = false;
491 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
493 if (producer == NULL) return;
495 QMutexLocker locker(&m_producerMutex);
496 m_replaceMutex.lock();
499 QString id = producer->get("id");
500 if (m_placeHolder || !producer->is_valid()) {
501 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
502 producer->set("markup", tmp);
503 producer->set("bgcolour", "0xff0000ff");
504 producer->set("pad", "10");
507 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
508 if (m_clipType != AUDIO) {
509 if (!id.endsWith("_audio"))
510 m_thumbProd->setProducer(producer);
512 else m_thumbProd->setProducer(producer);
515 bool updated = false;
516 if (id.contains('_')) {
517 // this is a subtrack producer, insert it at correct place
518 id = id.section('_', 1);
519 if (id.endsWith("audio")) {
520 int pos = id.section('_', 0, 0).toInt();
521 if (pos >= m_audioTrackProducers.count()) {
522 while (m_audioTrackProducers.count() - 1 < pos) {
523 m_audioTrackProducers.append(NULL);
526 if (m_audioTrackProducers.at(pos) == NULL) {
527 m_audioTrackProducers[pos] = producer;
530 else delete producer;
532 } else if (id.endsWith("video")) {
534 // Keep compatibility with older projects where video only producers were not track specific
535 if (id.contains('_')) pos = id.section('_', 0, 0).toInt();
536 if (pos >= m_videoTrackProducers.count()) {
537 while (m_videoTrackProducers.count() - 1 < pos) {
538 m_videoTrackProducers.append(NULL);
541 if (m_videoTrackProducers.at(pos) == NULL) {
542 m_videoTrackProducers[pos] = producer;
545 else delete producer;
548 int pos = id.toInt();
549 if (pos >= m_baseTrackProducers.count()) {
550 while (m_baseTrackProducers.count() - 1 < pos) {
551 m_baseTrackProducers.append(NULL);
554 if (m_baseTrackProducers.at(pos) == NULL) {
555 m_baseTrackProducers[pos] = producer;
558 else delete producer;
560 if (m_baseTrackProducers.isEmpty()) {
561 m_baseTrackProducers.append(producer);
564 else if (m_baseTrackProducers.at(0) == NULL) {
565 m_baseTrackProducers[0] = producer;
568 else delete producer;
570 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
571 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
574 static double getPixelAspect(QMap<QString, QString>& props) {
575 int width = props.value("frame_size").section('x', 0, 0).toInt();
576 int height = props.value("frame_size").section('x', 1, 1).toInt();
577 int aspectNumerator = props.value("force_aspect_num").toInt();
578 int aspectDenominator = props.value("force_aspect_den").toInt();
579 if (aspectDenominator != 0 && width != 0)
580 return double(height) * aspectNumerator / aspectDenominator / width;
585 Mlt::Producer *DocClipBase::audioProducer(int track)
587 QMutexLocker locker(&m_producerMutex);
588 if (m_audioTrackProducers.count() <= track) {
589 while (m_audioTrackProducers.count() - 1 < track) {
590 m_audioTrackProducers.append(NULL);
593 if (m_audioTrackProducers.at(track) == NULL) {
595 for (i = 0; i < m_audioTrackProducers.count(); i++)
596 if (m_audioTrackProducers.at(i) != NULL) break;
598 if (i >= m_audioTrackProducers.count()) {
599 // Could not find a valid producer for that clip
601 base = getProducer();
607 else base = m_audioTrackProducers.at(i);
608 m_audioTrackProducers[track] = cloneProducer(base);
609 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
611 return m_audioTrackProducers.at(track);
615 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
617 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
618 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
619 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
620 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
621 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
622 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
623 if (mute) prod->set("audio_index", -1);
624 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
625 if (blind) prod->set("video_index", -1);
626 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
627 prod->set("id", id.toUtf8().constData());
628 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
629 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
630 if (m_properties.contains("proxy_out")) {
631 // We have a proxy clip, make sure the proxy has same duration as original
632 prod->set("length", m_properties.value("duration").toInt());
633 prod->set("out", m_properties.value("proxy_out").toInt());
638 Mlt::Producer *DocClipBase::videoProducer(int track)
640 QMutexLocker locker(&m_producerMutex);
641 if (m_videoTrackProducers.count() <= track) {
642 while (m_videoTrackProducers.count() - 1 < track) {
643 m_videoTrackProducers.append(NULL);
646 if (m_videoTrackProducers.at(track) == NULL) {
648 for (i = 0; i < m_videoTrackProducers.count(); i++)
649 if (m_videoTrackProducers.at(i) != NULL) break;
651 if (i >= m_videoTrackProducers.count()) {
652 // Could not find a valid producer for that clip
654 base = getProducer();
660 else base = m_videoTrackProducers.at(i);
661 m_videoTrackProducers[track] = cloneProducer(base);
662 adjustProducerProperties(m_videoTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_video"), true, false);
664 return m_videoTrackProducers.at(track);
667 Mlt::Producer *DocClipBase::getCloneProducer()
669 Mlt::Producer *source = NULL;
670 Mlt::Producer *prod = NULL;
671 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
672 source = getProducer();
673 if (!source) return NULL;
675 if (m_clipType == COLOR) {
676 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
677 } else if (m_clipType == TEXT) {
678 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
679 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
680 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
684 QMutexLocker locker(&m_producerMutex);
685 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
686 if (m_baseTrackProducers.at(i) != NULL) {
687 source = m_baseTrackProducers.at(i);
691 if (!source) return NULL;
693 prod = cloneProducer(source);
696 adjustProducerProperties(prod, getId() + "_", false, false);
697 if (!m_properties.contains("proxy_out")) {
698 // Adjust length in case...
699 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
700 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
702 if (m_clipType == AUDIO) {
703 prod->set("_audioclip", 1);
710 Mlt::Producer *DocClipBase::getProducer(int track)
712 QMutexLocker locker(&m_producerMutex);
713 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
714 if (m_baseTrackProducers.count() == 0) {
717 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
718 if (m_baseTrackProducers.at(i) != NULL) {
719 return m_baseTrackProducers.at(i);
724 if (track >= m_baseTrackProducers.count()) {
725 while (m_baseTrackProducers.count() - 1 < track) {
726 m_baseTrackProducers.append(NULL);
729 if (m_baseTrackProducers.at(track) == NULL) {
731 for (i = 0; i < m_baseTrackProducers.count(); i++)
732 if (m_baseTrackProducers.at(i) != NULL) break;
734 if (i >= m_baseTrackProducers.count()) {
735 // Could not find a valid producer for that clip, check in
738 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
739 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
740 m_baseTrackProducers[track] = prod;
742 return m_baseTrackProducers.at(track);
746 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
748 Mlt::Producer *result = NULL;
749 QString url = QString::fromUtf8(source->get("resource"));
750 if (url == "<playlist>" || url == "<tractor>" || url == "<producer>") {
751 // Xml producer sometimes loses the correct url
752 url = m_properties.value("resource");
754 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
755 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
757 if (result == NULL || !result->is_valid()) {
759 QString txt = "+" + i18n("Missing clip") + ".txt";
760 char *tmp = qstrdup(txt.toUtf8().constData());
761 result = new Mlt::Producer(*source->profile(), tmp);
763 if (result == NULL || !result->is_valid())
764 result = new Mlt::Producer(*(source->profile()), "colour:red");
766 result->set("bgcolour", "0xff0000ff");
767 result->set("pad", "10");
771 /*Mlt::Properties src_props(source->get_properties());
772 Mlt::Properties props(result->get_properties());
773 props.inherit(src_props);*/
777 void DocClipBase::setProducerProperty(const char *name, int data)
779 QMutexLocker locker(&m_producerMutex);
780 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
781 if (m_baseTrackProducers.at(i) != NULL)
782 m_baseTrackProducers[i]->set(name, data);
786 void DocClipBase::setProducerProperty(const char *name, double data)
788 QMutexLocker locker(&m_producerMutex);
789 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
790 if (m_baseTrackProducers.at(i) != NULL)
791 m_baseTrackProducers[i]->set(name, data);
795 void DocClipBase::setProducerProperty(const char *name, const char *data)
797 QMutexLocker locker(&m_producerMutex);
798 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
799 if (m_baseTrackProducers.at(i) != NULL)
800 m_baseTrackProducers[i]->set(name, data);
804 void DocClipBase::resetProducerProperty(const char *name)
806 QMutexLocker locker(&m_producerMutex);
807 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
808 if (m_baseTrackProducers.at(i) != NULL)
809 m_baseTrackProducers[i]->set(name, (const char*) NULL);
813 const char *DocClipBase::producerProperty(const char *name) const
815 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
816 if (m_baseTrackProducers.at(i) != NULL) {
817 return m_baseTrackProducers.at(i)->get(name);
824 void DocClipBase::slotRefreshProducer()
826 if (m_baseTrackProducers.count() == 0) return;
827 if (m_clipType == SLIDESHOW) {
828 setProducerProperty("ttl", getProperty("ttl").toInt());
829 //m_clipProducer->set("id", getProperty("id"));
830 if (!getProperty("animation").isEmpty()) {
831 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
833 Mlt::Filter *filter = clipService.filter(ct);
835 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
837 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
838 clipService.detach(*filter);
840 filter = clipService.filter(ct);
843 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
844 // filter does not exist, create it.
845 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
846 if (filter && filter->is_valid()) {
847 int cycle = getProperty("ttl").toInt();
848 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
849 if (!geometry.isEmpty()) {
850 if (getProperty("animation").contains("low-pass")) {
851 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
852 if (blur && blur->is_valid())
853 clipService.attach(*blur);
855 filter->set("transition.geometry", geometry.toUtf8().data());
856 filter->set("transition.cycle", cycle);
857 clipService.attach(*filter);
862 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
864 Mlt::Filter *filter = clipService.filter(0);
866 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
867 clipService.detach(*filter);
869 filter = clipService.filter(ct);
872 if (getProperty("fade") == "1") {
873 // we want a fade filter effect
874 kDebug() << "//////////// FADE WANTED";
875 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
877 Mlt::Filter *filter = clipService.filter(ct);
879 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
883 filter = clipService.filter(ct);
886 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
887 filter->set("cycle", getProperty("ttl").toInt());
888 filter->set("duration", getProperty("luma_duration").toInt());
889 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
890 if (!getProperty("softness").isEmpty()) {
891 int soft = getProperty("softness").toInt();
892 filter->set("luma.softness", (double) soft / 100.0);
895 // filter does not exist, create it...
896 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
897 filter->set("cycle", getProperty("ttl").toInt());
898 filter->set("duration", getProperty("luma_duration").toInt());
899 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
900 if (!getProperty("softness").isEmpty()) {
901 int soft = getProperty("softness").toInt();
902 filter->set("luma.softness", (double) soft / 100.0);
904 clipService.attach(*filter);
907 kDebug() << "//////////// FADE NOT WANTED!!!";
908 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
910 Mlt::Filter *filter = clipService.filter(0);
912 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
913 clipService.detach(*filter);
915 filter = clipService.filter(ct);
918 if (getProperty("crop") == "1") {
919 // we want a center crop filter effect
920 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
922 Mlt::Filter *filter = clipService.filter(ct);
924 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
928 filter = clipService.filter(ct);
931 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
932 // filter does not exist, create it...
933 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
934 filter->set("center", 1);
935 clipService.attach(*filter);
938 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
940 Mlt::Filter *filter = clipService.filter(0);
942 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
943 clipService.detach(*filter);
945 filter = clipService.filter(ct);
951 void DocClipBase::setProperties(QMap <QString, QString> properties)
953 // changing clip type is not allowed
954 properties.remove("type");
955 QMapIterator<QString, QString> i(properties);
956 bool refreshProducer = false;
958 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
959 QString oldProxy = m_properties.value("proxy");
960 while (i.hasNext()) {
962 setProperty(i.key(), i.value());
963 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
965 if (properties.contains("proxy")) {
966 QString value = properties.value("proxy");
967 // If value is "-", that means user manually disabled proxy on this clip
968 if (value.isEmpty() || value == "-") {
970 emit abortProxy(m_id, oldProxy);
973 emit createProxy(m_id);
976 if (refreshProducer) slotRefreshProducer();
979 void DocClipBase::setMetadata(QMap <QString, QString> properties)
981 QMapIterator<QString, QString> i(properties);
982 while (i.hasNext()) {
984 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
985 m_metadata.remove(i.key());
987 m_metadata.insert(i.key(), i.value());
992 QMap <QString, QString> DocClipBase::metadata() const
997 void DocClipBase::clearProperty(const QString &key)
999 m_properties.remove(key);
1002 void DocClipBase::getFileHash(const QString &url)
1004 if (m_clipType == SLIDESHOW) return;
1006 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1007 QByteArray fileData;
1008 QByteArray fileHash;
1009 //kDebug() << "SETTING HASH of" << value;
1010 m_properties.insert("file_size", QString::number(file.size()));
1012 * 1 MB = 1 second per 450 files (or faster)
1013 * 10 MB = 9 seconds per 450 files (or faster)
1015 if (file.size() > 1000000*2) {
1016 fileData = file.read(1000000);
1017 if (file.seek(file.size() - 1000000))
1018 fileData.append(file.readAll());
1020 fileData = file.readAll();
1022 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1023 m_properties.insert("file_hash", QString(fileHash.toHex()));
1027 bool DocClipBase::checkHash() const
1029 KUrl url = fileURL();
1030 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
1034 QString DocClipBase::getClipHash() const
1037 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
1038 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
1039 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
1041 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
1042 if (hash.isEmpty()) hash = getHash(fileURL().path());
1048 void DocClipBase::setPlaceHolder(bool place)
1050 m_placeHolder = place;
1054 QString DocClipBase::getHash(const QString &path)
1057 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1058 QByteArray fileData;
1059 QByteArray fileHash;
1061 * 1 MB = 1 second per 450 files (or faster)
1062 * 10 MB = 9 seconds per 450 files (or faster)
1064 if (file.size() > 1000000*2) {
1065 fileData = file.read(1000000);
1066 if (file.seek(file.size() - 1000000))
1067 fileData.append(file.readAll());
1069 fileData = file.readAll();
1071 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1076 void DocClipBase::refreshThumbUrl()
1078 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1081 void DocClipBase::setProperty(const QString &key, const QString &value)
1083 m_properties.insert(key, value);
1084 if (key == "resource") {
1086 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1087 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1088 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1089 else if (key == "colour") {
1090 setProducerProperty("colour", value.toUtf8().data());
1091 } else if (key == "templatetext") {
1092 setProducerProperty("templatetext", value.toUtf8().data());
1093 setProducerProperty("force_reload", 1);
1094 } else if (key == "xmldata") {
1095 setProducerProperty("xmldata", value.toUtf8().data());
1096 setProducerProperty("force_reload", 1);
1097 } else if (key == "force_aspect_num") {
1098 if (value.isEmpty()) {
1099 m_properties.remove("force_aspect_num");
1100 resetProducerProperty("force_aspect_ratio");
1101 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1102 } else if (key == "force_aspect_den") {
1103 if (value.isEmpty()) {
1104 m_properties.remove("force_aspect_den");
1105 resetProducerProperty("force_aspect_ratio");
1106 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1107 } else if (key == "force_fps") {
1108 if (value.isEmpty()) {
1109 m_properties.remove("force_fps");
1110 resetProducerProperty("force_fps");
1111 } else setProducerProperty("force_fps", value.toDouble());
1112 } else if (key == "force_progressive") {
1113 if (value.isEmpty()) {
1114 m_properties.remove("force_progressive");
1115 resetProducerProperty("force_progressive");
1116 } else setProducerProperty("force_progressive", value.toInt());
1117 } else if (key == "force_tff") {
1118 if (value.isEmpty()) {
1119 m_properties.remove("force_tff");
1120 resetProducerProperty("force_tff");
1121 } else setProducerProperty("force_tff", value.toInt());
1122 } else if (key == "threads") {
1123 if (value.isEmpty()) {
1124 m_properties.remove("threads");
1125 setProducerProperty("threads", 1);
1126 } else setProducerProperty("threads", value.toInt());
1127 } else if (key == "video_index") {
1128 if (value.isEmpty()) {
1129 m_properties.remove("video_index");
1130 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1131 } else setProducerProperty("video_index", value.toInt());
1132 } else if (key == "audio_index") {
1133 if (value.isEmpty()) {
1134 m_properties.remove("audio_index");
1135 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1136 } else setProducerProperty("audio_index", value.toInt());
1137 } else if (key == "force_colorspace") {
1138 if (value.isEmpty()) {
1139 m_properties.remove("force_colorspace");
1140 resetProducerProperty("force_colorspace");
1141 } else setProducerProperty("force_colorspace", value.toInt());
1142 } else if (key == "full_luma") {
1143 if (value.isEmpty()) {
1144 m_properties.remove("full_luma");
1145 resetProducerProperty("set.force_full_luma");
1146 } else setProducerProperty("set.force_full_luma", value.toInt());
1150 QMap <QString, QString> DocClipBase::properties() const
1152 return m_properties;
1155 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1157 QMap <QString, QString> currentProps;
1158 QMap<QString, QString>::const_iterator i = props.constBegin();
1159 while (i != props.constEnd()) {
1160 currentProps.insert(i.key(), m_properties.value(i.key()));
1163 return currentProps;
1166 bool DocClipBase::getAudioThumbs()
1168 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1169 if (m_audioThumbCreated) {
1172 m_audioTimer.start();
1176 bool DocClipBase::isPlaceHolder() const
1178 return m_placeHolder;
1181 void DocClipBase::addCutZone(int in, int out, QString desc)
1184 info.zone = QPoint(in, out);
1185 info.description = desc;
1186 for (int i = 0; i < m_cutZones.count(); i++)
1187 if (m_cutZones.at(i).zone == info.zone) {
1190 m_cutZones.append(info);
1193 bool DocClipBase::hasCutZone(QPoint p) const
1195 for (int i = 0; i < m_cutZones.count(); i++)
1196 if (m_cutZones.at(i).zone == p) return true;
1201 void DocClipBase::removeCutZone(int in, int out)
1204 for (int i = 0; i < m_cutZones.count(); i++) {
1205 if (m_cutZones.at(i).zone == p) {
1206 m_cutZones.removeAt(i);
1212 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1214 QPoint old(oldin, oldout);
1215 for (int i = 0; i < m_cutZones.size(); ++i) {
1216 if (m_cutZones.at(i).zone == old) {
1218 info.zone = QPoint(in, out);
1219 info.description = desc;
1220 m_cutZones.replace(i, info);
1226 QList <CutZoneInfo> DocClipBase::cutZones() const
1231 bool DocClipBase::hasVideoCodec(const QString &codec) const
1233 Mlt::Producer *prod = NULL;
1234 if (m_baseTrackProducers.count() == 0) return false;
1235 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1236 if (m_baseTrackProducers.at(i) != NULL) {
1237 prod = m_baseTrackProducers.at(i);
1242 if (!prod) return false;
1243 int default_video = prod->get_int("video_index");
1245 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1246 return prod->get(property) == codec;
1249 bool DocClipBase::hasAudioCodec(const QString &codec) const
1251 Mlt::Producer *prod = NULL;
1252 if (m_baseTrackProducers.count() == 0) return false;
1253 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1254 if (m_baseTrackProducers.at(i) != NULL) {
1255 prod = m_baseTrackProducers.at(i);
1259 if (!prod) return false;
1260 int default_video = prod->get_int("audio_index");
1262 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1263 return prod->get(property) == codec;
1267 void DocClipBase::slotExtractImage(QList <int> frames)
1269 if (m_thumbProd == NULL) return;
1270 m_thumbProd->extractImage(frames);
1273 QImage DocClipBase::extractImage(int frame, int width, int height)
1275 if (m_thumbProd == NULL) return QImage();
1276 QMutexLocker locker(&m_producerMutex);
1277 return m_thumbProd->extractImage(frame, width, height);
1280 void DocClipBase::setAnalysisData(const QString &name, const QString &data, int offset)
1282 if (data.isEmpty()) m_analysisdata.remove(name);
1284 if (m_analysisdata.contains(name)) {
1285 if (KMessageBox::questionYesNo(kapp->activeWindow(), i18n("Clip already contains analysis data %1", name), QString(), KGuiItem(i18n("Merge")), KGuiItem(i18n("Add"))) == KMessageBox::Yes) {
1287 Mlt::Profile *profile = m_baseTrackProducers.at(0)->profile();
1288 Mlt::Geometry geometry(m_analysisdata.value(name).toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1289 Mlt::Geometry newGeometry(data.toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1290 Mlt::GeometryItem item;
1292 while (!newGeometry.next_key(&item, pos)) {
1294 item.frame(pos + offset);
1296 geometry.insert(item);
1298 m_analysisdata.insert(name, geometry.serialise());
1301 // Add data with another name
1303 QString newname = name + " " + QString::number(i);
1304 while (m_analysisdata.contains(newname)) {
1306 newname = name + " " + QString::number(i);
1308 m_analysisdata.insert(newname, geometryWithOffset(data, offset));
1311 else m_analysisdata.insert(name, geometryWithOffset(data, offset));
1315 const QString DocClipBase::geometryWithOffset(QString data, int offset)
1317 if (offset == 0) return data;
1318 Mlt::Profile *profile = m_baseTrackProducers.at(0)->profile();
1319 Mlt::Geometry geometry(data.toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1320 Mlt::Geometry newgeometry("", m_properties.value("duration").toInt(), profile->width(), profile->height());
1321 Mlt::GeometryItem item;
1323 while (!geometry.next_key(&item, pos)) {
1325 item.frame(pos + offset);
1327 newgeometry.insert(item);
1329 return newgeometry.serialise();
1332 QMap <QString, QString> DocClipBase::analysisData() const
1334 return m_analysisdata;