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) :
47 m_baseTrackProducers(),
48 m_videoTrackProducers(),
49 m_audioTrackProducers(),
50 m_snapMarkers(QList < CommentedTime >()),
53 m_audioThumbCreated(false),
55 m_placeHolder(xml.hasAttribute("placeholder")),
58 int type = xml.attribute("type").toInt();
59 m_clipType = (CLIPTYPE) type;
60 if (m_placeHolder) xml.removeAttribute("placeholder");
61 QDomNamedNodeMap attributes = xml.attributes();
62 for (int i = 0; i < attributes.count(); i++) {
63 QString name = attributes.item(i).nodeName();
64 if (name.startsWith("meta.attr.")) {
65 m_metadata.insert(name.section('.', 2, 3), attributes.item(i).nodeValue());
66 } else m_properties.insert(name, attributes.item(i).nodeValue());
69 if (xml.hasAttribute("cutzones")) {
70 QStringList cuts = xml.attribute("cutzones").split(';', QString::SkipEmptyParts);
71 for (int i = 0; i < cuts.count(); i++) {
72 QString z = cuts.at(i);
73 addCutZone(z.section('-', 0, 0).toInt(), z.section('-', 1, 1).toInt(), z.section('-', 2, 2));
77 if (xml.hasAttribute("analysisdata")) {
78 QStringList adata = xml.attribute("analysisdata").split('#', QString::SkipEmptyParts);
79 for (int i = 0; i < adata.count(); i++)
80 m_analysisdata.insert(adata.at(i).section('?', 0, 0), adata.at(i).section('?', 1, 1));
83 KUrl url = KUrl(xml.attribute("resource"));
84 if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
86 if (xml.hasAttribute("duration")) {
87 setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
89 int out = xml.attribute("out").toInt();
90 int in = xml.attribute("in").toInt();
91 setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
94 if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
96 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
98 // Setup timer to trigger audio thumbs creation
99 m_audioTimer.setSingleShot(true);
100 m_audioTimer.setInterval(800);
101 connect(&m_audioTimer, SIGNAL(timeout()), m_thumbProd, SLOT(slotCreateAudioThumbs()));
105 DocClipBase::~DocClipBase()
110 qDeleteAll(m_toDeleteProducers);
111 m_toDeleteProducers.clear();
112 qDeleteAll(m_baseTrackProducers);
113 m_baseTrackProducers.clear();
114 qDeleteAll(m_audioTrackProducers);
115 m_audioTrackProducers.clear();
116 qDeleteAll(m_videoTrackProducers);
117 m_videoTrackProducers.clear();
120 void DocClipBase::setZone(QPoint zone)
122 m_properties.insert("zone_in", QString::number(zone.x()));
123 m_properties.insert("zone_out", QString::number(zone.y()));
126 QPoint DocClipBase::zone() const
128 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
133 bool DocClipBase::hasAudioThumb() const
135 if (m_clipType == AUDIO || m_clipType == AV || m_clipType == PLAYLIST) return true;
139 void DocClipBase::slotClearAudioCache()
141 audioFrameCache.clear();
142 m_audioThumbCreated = false;
145 /*void DocClipBase::getClipMainThumb() {
146 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
149 KThumb *DocClipBase::thumbProducer()
154 bool DocClipBase::audioThumbCreated() const
156 return m_audioThumbCreated;
159 const QString DocClipBase::name() const
162 return m_properties.value("name");
165 const QString &DocClipBase::getId() const
170 const CLIPTYPE & DocClipBase::clipType() const
175 void DocClipBase::setClipType(CLIPTYPE type)
178 m_properties.insert("type", QString::number((int) type));
181 KUrl DocClipBase::fileURL() const
183 QString res = m_properties.value("resource");
184 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
188 void DocClipBase::setClipThumbFrame(const uint &ix)
190 m_properties.insert("thumbnail", QString::number((int) ix));
193 uint DocClipBase::getClipThumbFrame() const
195 return (uint) m_properties.value("thumbnail").toInt();
198 const QString DocClipBase::description() const
200 return m_properties.value("description");
203 bool DocClipBase::isTransparent() const
205 return (m_properties.value("transparency") == "1");
208 const QString DocClipBase::getProperty(const QString &prop) const
210 return m_properties.value(prop);
213 void DocClipBase::setDuration(GenTime dur)
216 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
219 const GenTime &DocClipBase::duration() const
224 const GenTime DocClipBase::maxDuration() const
226 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
227 /*const GenTime dur(15000, KdenliveSettings::project_fps());
234 bool DocClipBase::hasFileSize() const
239 qulonglong DocClipBase::fileSize() const
241 return m_properties.value("file_size").toULongLong();
245 QDomElement DocClipBase::toXML(bool hideTemporaryProperties) const
248 QDomElement clip = doc.createElement("producer");
250 QMapIterator<QString, QString> i(m_properties);
251 while (i.hasNext()) {
253 if (hideTemporaryProperties && i.key().startsWith('_')) continue;
254 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
256 doc.appendChild(clip);
257 if (!m_cutZones.isEmpty()) {
259 for (int i = 0; i < m_cutZones.size(); i++) {
260 CutZoneInfo info = m_cutZones.at(i);
261 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
263 clip.setAttribute("cutzones", cuts.join(";"));
266 if (!m_analysisdata.isEmpty()) {
267 QMapIterator<QString, QString> i(m_analysisdata);
268 while (i.hasNext()) {
270 //WARNING: a ? and # separator is not a good idea
271 adata.append(i.key() + "?" + i.value() + "#");
274 clip.setAttribute("analysisdata", adata);
275 //kDebug() << "/// CLIP XML: " << doc.toString();
276 return doc.documentElement();
280 void DocClipBase::setAudioThumbCreated(bool isDone)
282 m_audioThumbCreated = isDone;
285 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
287 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
288 audioFrameCache = data;
289 m_audioThumbCreated = true;
293 QList < GenTime > DocClipBase::snapMarkers() const
295 QList < GenTime > markers;
297 for (int count = 0; count < m_snapMarkers.count(); ++count) {
298 markers.append(m_snapMarkers.at(count).time());
304 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
306 return m_snapMarkers;
310 void DocClipBase::addSnapMarker(const CommentedTime marker)
312 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
313 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
314 if ((*it).time() >= marker.time())
318 if ((it != m_snapMarkers.end()) && ((*it).time() == marker.time())) {
319 (*it).setComment(marker.comment());
320 (*it).setMarkerType(marker.markerType());
321 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
323 m_snapMarkers.insert(it, marker);
327 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
329 QList < CommentedTime >::Iterator it;
330 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
331 if ((*it).time() == time)
334 if (it != m_snapMarkers.end()) {
335 (*it).setComment(comment);
337 kError() << "trying to edit Snap Marker that does not already exists";
341 QString DocClipBase::deleteSnapMarker(const GenTime & time)
343 QString result = i18n("Marker");
344 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
346 while (itt != m_snapMarkers.end()) {
347 if ((*itt).time() == time)
352 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
353 result = (*itt).comment();
354 m_snapMarkers.erase(itt);
360 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
362 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
364 while (itt != m_snapMarkers.end()) {
365 if ((*itt).time() == time)
373 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
376 for (it = 0; it < m_snapMarkers.count(); it++) {
377 if (m_snapMarkers.at(it).time() >= currTime)
380 if (it == 0) return GenTime();
381 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
382 return m_snapMarkers.at(it).time();
383 else return m_snapMarkers.at(it - 1).time();
386 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
389 for (it = 0; it < m_snapMarkers.count(); it++) {
390 if (m_snapMarkers.at(it).time() > currTime)
393 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
397 QString DocClipBase::markerComment(GenTime t) const
399 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
400 while (itt != m_snapMarkers.end()) {
401 if ((*itt).time() == t)
402 return (*itt).comment();
408 CommentedTime DocClipBase::markerAt(GenTime t) const
410 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
411 while (itt != m_snapMarkers.end()) {
412 if ((*itt).time() == t)
416 return CommentedTime();
419 void DocClipBase::clearThumbProducer()
421 if (m_thumbProd) m_thumbProd->clearProducer();
424 void DocClipBase::reloadThumbProducer()
426 if (m_thumbProd && !m_thumbProd->hasProducer())
427 m_thumbProd->setProducer(getProducer());
430 void DocClipBase::deleteProducers()
432 if (m_thumbProd) m_thumbProd->clearProducer();
434 if (numReferences() > 0 && (!m_baseTrackProducers.isEmpty() || !m_videoTrackProducers.isEmpty() || !m_audioTrackProducers.isEmpty())) {
435 // Clip is used in timeline, delay producers deletion
436 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
437 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
439 for (int i = 0; i < m_videoTrackProducers.count(); i++) {
440 m_toDeleteProducers.append(m_videoTrackProducers.at(i));
442 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
443 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
447 qDeleteAll(m_baseTrackProducers);
448 qDeleteAll(m_videoTrackProducers);
449 qDeleteAll(m_audioTrackProducers);
450 m_replaceMutex.unlock();
452 m_baseTrackProducers.clear();
453 m_videoTrackProducers.clear();
454 m_audioTrackProducers.clear();
457 void DocClipBase::cleanupProducers()
461 kDebug()<<"----------------------------------------------------------------------------------";
462 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
463 if (m_toDeleteProducers.at(i) != NULL) {
464 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
465 if (props.ref_count() > 2) {
466 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
474 qDeleteAll(m_toDeleteProducers);
475 m_toDeleteProducers.clear();
476 m_replaceMutex.unlock();
480 bool DocClipBase::isClean() const
482 return m_toDeleteProducers.isEmpty();
485 void DocClipBase::setValid()
487 m_placeHolder = false;
490 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
492 if (producer == NULL) return;
494 QMutexLocker locker(&m_producerMutex);
495 m_replaceMutex.lock();
498 QString id = producer->get("id");
499 if (m_placeHolder || !producer->is_valid()) {
500 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
501 producer->set("markup", tmp);
502 producer->set("bgcolour", "0xff0000ff");
503 producer->set("pad", "10");
506 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
507 if (m_clipType != AUDIO) {
508 if (!id.endsWith("_audio"))
509 m_thumbProd->setProducer(producer);
511 else m_thumbProd->setProducer(producer);
514 bool updated = false;
515 if (id.contains('_')) {
516 // this is a subtrack producer, insert it at correct place
517 id = id.section('_', 1);
518 if (id.endsWith("audio")) {
519 int pos = id.section('_', 0, 0).toInt();
520 if (pos >= m_audioTrackProducers.count()) {
521 while (m_audioTrackProducers.count() - 1 < pos) {
522 m_audioTrackProducers.append(NULL);
525 if (m_audioTrackProducers.at(pos) == NULL) {
526 m_audioTrackProducers[pos] = producer;
529 else delete producer;
531 } else if (id.endsWith("video")) {
533 // Keep compatibility with older projects where video only producers were not track specific
534 if (id.contains('_')) pos = id.section('_', 0, 0).toInt();
535 if (pos >= m_videoTrackProducers.count()) {
536 while (m_videoTrackProducers.count() - 1 < pos) {
537 m_videoTrackProducers.append(NULL);
540 if (m_videoTrackProducers.at(pos) == NULL) {
541 m_videoTrackProducers[pos] = producer;
544 else delete producer;
547 int pos = id.toInt();
548 if (pos >= m_baseTrackProducers.count()) {
549 while (m_baseTrackProducers.count() - 1 < pos) {
550 m_baseTrackProducers.append(NULL);
553 if (m_baseTrackProducers.at(pos) == NULL) {
554 m_baseTrackProducers[pos] = producer;
557 else delete producer;
559 if (m_baseTrackProducers.isEmpty()) {
560 m_baseTrackProducers.append(producer);
563 else if (m_baseTrackProducers.at(0) == NULL) {
564 m_baseTrackProducers[0] = producer;
567 else delete producer;
569 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
570 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
573 static double getPixelAspect(QMap<QString, QString>& props) {
574 int width = props.value("frame_size").section('x', 0, 0).toInt();
575 int height = props.value("frame_size").section('x', 1, 1).toInt();
576 int aspectNumerator = props.value("force_aspect_num").toInt();
577 int aspectDenominator = props.value("force_aspect_den").toInt();
578 if (aspectDenominator != 0 && width != 0)
579 return double(height) * aspectNumerator / aspectDenominator / width;
584 Mlt::Producer *DocClipBase::audioProducer(int track)
586 QMutexLocker locker(&m_producerMutex);
587 if (m_audioTrackProducers.count() <= track) {
588 while (m_audioTrackProducers.count() - 1 < track) {
589 m_audioTrackProducers.append(NULL);
592 if (m_audioTrackProducers.at(track) == NULL) {
594 for (i = 0; i < m_audioTrackProducers.count(); i++)
595 if (m_audioTrackProducers.at(i) != NULL) break;
597 if (i >= m_audioTrackProducers.count()) {
598 // Could not find a valid producer for that clip
600 base = getProducer();
606 else base = m_audioTrackProducers.at(i);
607 m_audioTrackProducers[track] = cloneProducer(base);
608 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
610 return m_audioTrackProducers.at(track);
614 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
616 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
617 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
618 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
619 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
620 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
621 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
622 if (mute) prod->set("audio_index", -1);
623 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
624 if (blind) prod->set("video_index", -1);
625 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
626 prod->set("id", id.toUtf8().constData());
627 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
628 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
629 if (m_properties.contains("proxy_out")) {
630 // We have a proxy clip, make sure the proxy has same duration as original
631 prod->set("length", m_properties.value("duration").toInt());
632 prod->set("out", m_properties.value("proxy_out").toInt());
637 Mlt::Producer *DocClipBase::videoProducer(int track)
639 QMutexLocker locker(&m_producerMutex);
640 if (m_videoTrackProducers.count() <= track) {
641 while (m_videoTrackProducers.count() - 1 < track) {
642 m_videoTrackProducers.append(NULL);
645 if (m_videoTrackProducers.at(track) == NULL) {
647 for (i = 0; i < m_videoTrackProducers.count(); i++)
648 if (m_videoTrackProducers.at(i) != NULL) break;
650 if (i >= m_videoTrackProducers.count()) {
651 // Could not find a valid producer for that clip
653 base = getProducer();
659 else base = m_videoTrackProducers.at(i);
660 m_videoTrackProducers[track] = cloneProducer(base);
661 adjustProducerProperties(m_videoTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_video"), true, false);
663 return m_videoTrackProducers.at(track);
666 Mlt::Producer *DocClipBase::getCloneProducer()
668 Mlt::Producer *source = NULL;
669 Mlt::Producer *prod = NULL;
670 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
671 source = getProducer();
672 if (!source) return NULL;
674 if (m_clipType == COLOR) {
675 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
676 } else if (m_clipType == TEXT) {
677 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
678 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
679 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
683 QMutexLocker locker(&m_producerMutex);
684 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
685 if (m_baseTrackProducers.at(i) != NULL) {
686 source = m_baseTrackProducers.at(i);
690 if (!source) return NULL;
692 prod = cloneProducer(source);
695 adjustProducerProperties(prod, getId() + "_", false, false);
696 if (!m_properties.contains("proxy_out")) {
697 // Adjust length in case...
698 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
699 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
701 if (m_clipType == AUDIO) {
702 prod->set("_audioclip", 1);
709 Mlt::Producer *DocClipBase::getProducer(int track)
711 QMutexLocker locker(&m_producerMutex);
712 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
713 if (m_baseTrackProducers.count() == 0) {
716 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
717 if (m_baseTrackProducers.at(i) != NULL) {
718 return m_baseTrackProducers.at(i);
723 if (track >= m_baseTrackProducers.count()) {
724 while (m_baseTrackProducers.count() - 1 < track) {
725 m_baseTrackProducers.append(NULL);
728 if (m_baseTrackProducers.at(track) == NULL) {
730 for (i = 0; i < m_baseTrackProducers.count(); i++)
731 if (m_baseTrackProducers.at(i) != NULL) break;
733 if (i >= m_baseTrackProducers.count()) {
734 // Could not find a valid producer for that clip, check in
737 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
738 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
739 m_baseTrackProducers[track] = prod;
741 return m_baseTrackProducers.at(track);
745 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
747 Mlt::Producer *result = NULL;
748 QString url = QString::fromUtf8(source->get("resource"));
749 if (url == "<playlist>" || url == "<tractor>" || url == "<producer>") {
750 // Xml producer sometimes loses the correct url
751 url = m_properties.value("resource");
753 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
754 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
756 if (result == NULL || !result->is_valid()) {
758 QString txt = "+" + i18n("Missing clip") + ".txt";
759 char *tmp = qstrdup(txt.toUtf8().constData());
760 result = new Mlt::Producer(*source->profile(), tmp);
762 if (result == NULL || !result->is_valid())
763 result = new Mlt::Producer(*(source->profile()), "colour:red");
765 result->set("bgcolour", "0xff0000ff");
766 result->set("pad", "10");
770 /*Mlt::Properties src_props(source->get_properties());
771 Mlt::Properties props(result->get_properties());
772 props.inherit(src_props);*/
776 void DocClipBase::setProducerProperty(const char *name, int data)
778 QMutexLocker locker(&m_producerMutex);
779 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
780 if (m_baseTrackProducers.at(i) != NULL)
781 m_baseTrackProducers[i]->set(name, data);
785 void DocClipBase::setProducerProperty(const char *name, double data)
787 QMutexLocker locker(&m_producerMutex);
788 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
789 if (m_baseTrackProducers.at(i) != NULL)
790 m_baseTrackProducers[i]->set(name, data);
794 void DocClipBase::setProducerProperty(const char *name, const char *data)
796 QMutexLocker locker(&m_producerMutex);
797 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
798 if (m_baseTrackProducers.at(i) != NULL)
799 m_baseTrackProducers[i]->set(name, data);
803 void DocClipBase::resetProducerProperty(const char *name)
805 QMutexLocker locker(&m_producerMutex);
806 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
807 if (m_baseTrackProducers.at(i) != NULL)
808 m_baseTrackProducers[i]->set(name, (const char*) NULL);
812 const char *DocClipBase::producerProperty(const char *name) const
814 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
815 if (m_baseTrackProducers.at(i) != NULL) {
816 return m_baseTrackProducers.at(i)->get(name);
823 void DocClipBase::slotRefreshProducer()
825 if (m_baseTrackProducers.count() == 0) return;
826 if (m_clipType == SLIDESHOW) {
827 setProducerProperty("ttl", getProperty("ttl").toInt());
828 //m_clipProducer->set("id", getProperty("id"));
829 if (!getProperty("animation").isEmpty()) {
830 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
832 Mlt::Filter *filter = clipService.filter(ct);
834 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
836 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
837 clipService.detach(*filter);
839 filter = clipService.filter(ct);
842 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
843 // filter does not exist, create it.
844 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
845 if (filter && filter->is_valid()) {
846 int cycle = getProperty("ttl").toInt();
847 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
848 if (!geometry.isEmpty()) {
849 if (getProperty("animation").contains("low-pass")) {
850 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
851 if (blur && blur->is_valid())
852 clipService.attach(*blur);
854 filter->set("transition.geometry", geometry.toUtf8().data());
855 filter->set("transition.cycle", cycle);
856 clipService.attach(*filter);
861 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
863 Mlt::Filter *filter = clipService.filter(0);
865 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
866 clipService.detach(*filter);
868 filter = clipService.filter(ct);
871 if (getProperty("fade") == "1") {
872 // we want a fade filter effect
873 kDebug() << "//////////// FADE WANTED";
874 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
876 Mlt::Filter *filter = clipService.filter(ct);
878 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
882 filter = clipService.filter(ct);
885 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
886 filter->set("cycle", getProperty("ttl").toInt());
887 filter->set("duration", getProperty("luma_duration").toInt());
888 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
889 if (!getProperty("softness").isEmpty()) {
890 int soft = getProperty("softness").toInt();
891 filter->set("luma.softness", (double) soft / 100.0);
894 // filter does not exist, create it...
895 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
896 filter->set("cycle", getProperty("ttl").toInt());
897 filter->set("duration", getProperty("luma_duration").toInt());
898 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
899 if (!getProperty("softness").isEmpty()) {
900 int soft = getProperty("softness").toInt();
901 filter->set("luma.softness", (double) soft / 100.0);
903 clipService.attach(*filter);
906 kDebug() << "//////////// FADE NOT WANTED!!!";
907 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
909 Mlt::Filter *filter = clipService.filter(0);
911 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
912 clipService.detach(*filter);
914 filter = clipService.filter(ct);
917 if (getProperty("crop") == "1") {
918 // we want a center crop filter effect
919 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
921 Mlt::Filter *filter = clipService.filter(ct);
923 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
927 filter = clipService.filter(ct);
930 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
931 // filter does not exist, create it...
932 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
933 filter->set("center", 1);
934 clipService.attach(*filter);
937 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
939 Mlt::Filter *filter = clipService.filter(0);
941 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
942 clipService.detach(*filter);
944 filter = clipService.filter(ct);
950 void DocClipBase::setProperties(QMap <QString, QString> properties)
952 // changing clip type is not allowed
953 properties.remove("type");
954 QMapIterator<QString, QString> i(properties);
955 bool refreshProducer = false;
957 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
958 QString oldProxy = m_properties.value("proxy");
959 while (i.hasNext()) {
961 setProperty(i.key(), i.value());
962 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
964 if (properties.contains("proxy")) {
965 QString value = properties.value("proxy");
966 // If value is "-", that means user manually disabled proxy on this clip
967 if (value.isEmpty() || value == "-") {
969 emit abortProxy(m_id, oldProxy);
972 emit createProxy(m_id);
975 if (refreshProducer) slotRefreshProducer();
978 void DocClipBase::setMetadata(QMap <QString, QString> properties)
980 QMapIterator<QString, QString> i(properties);
981 while (i.hasNext()) {
983 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
984 m_metadata.remove(i.key());
986 m_metadata.insert(i.key(), i.value());
991 QMap <QString, QString> DocClipBase::metadata() const
996 void DocClipBase::clearProperty(const QString &key)
998 m_properties.remove(key);
1001 void DocClipBase::getFileHash(const QString &url)
1003 if (m_clipType == SLIDESHOW) return;
1005 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1006 QByteArray fileData;
1007 QByteArray fileHash;
1008 //kDebug() << "SETTING HASH of" << value;
1009 m_properties.insert("file_size", QString::number(file.size()));
1011 * 1 MB = 1 second per 450 files (or faster)
1012 * 10 MB = 9 seconds per 450 files (or faster)
1014 if (file.size() > 1000000*2) {
1015 fileData = file.read(1000000);
1016 if (file.seek(file.size() - 1000000))
1017 fileData.append(file.readAll());
1019 fileData = file.readAll();
1021 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1022 m_properties.insert("file_hash", QString(fileHash.toHex()));
1026 bool DocClipBase::checkHash() const
1028 KUrl url = fileURL();
1029 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
1033 QString DocClipBase::getClipHash() const
1036 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
1037 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
1038 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
1040 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
1041 if (hash.isEmpty()) hash = getHash(fileURL().path());
1047 void DocClipBase::setPlaceHolder(bool place)
1049 m_placeHolder = place;
1053 QString DocClipBase::getHash(const QString &path)
1056 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1057 QByteArray fileData;
1058 QByteArray fileHash;
1060 * 1 MB = 1 second per 450 files (or faster)
1061 * 10 MB = 9 seconds per 450 files (or faster)
1063 if (file.size() > 1000000*2) {
1064 fileData = file.read(1000000);
1065 if (file.seek(file.size() - 1000000))
1066 fileData.append(file.readAll());
1068 fileData = file.readAll();
1070 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1075 void DocClipBase::refreshThumbUrl()
1077 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1080 void DocClipBase::setProperty(const QString &key, const QString &value)
1082 m_properties.insert(key, value);
1083 if (key == "resource") {
1085 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1086 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1087 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1088 else if (key == "colour") {
1089 setProducerProperty("colour", value.toUtf8().data());
1090 } else if (key == "templatetext") {
1091 setProducerProperty("templatetext", value.toUtf8().data());
1092 setProducerProperty("force_reload", 1);
1093 } else if (key == "xmldata") {
1094 setProducerProperty("xmldata", value.toUtf8().data());
1095 setProducerProperty("force_reload", 1);
1096 } else if (key == "force_aspect_num") {
1097 if (value.isEmpty()) {
1098 m_properties.remove("force_aspect_num");
1099 resetProducerProperty("force_aspect_ratio");
1100 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1101 } else if (key == "force_aspect_den") {
1102 if (value.isEmpty()) {
1103 m_properties.remove("force_aspect_den");
1104 resetProducerProperty("force_aspect_ratio");
1105 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1106 } else if (key == "force_fps") {
1107 if (value.isEmpty()) {
1108 m_properties.remove("force_fps");
1109 resetProducerProperty("force_fps");
1110 } else setProducerProperty("force_fps", value.toDouble());
1111 } else if (key == "force_progressive") {
1112 if (value.isEmpty()) {
1113 m_properties.remove("force_progressive");
1114 resetProducerProperty("force_progressive");
1115 } else setProducerProperty("force_progressive", value.toInt());
1116 } else if (key == "force_tff") {
1117 if (value.isEmpty()) {
1118 m_properties.remove("force_tff");
1119 resetProducerProperty("force_tff");
1120 } else setProducerProperty("force_tff", value.toInt());
1121 } else if (key == "threads") {
1122 if (value.isEmpty()) {
1123 m_properties.remove("threads");
1124 setProducerProperty("threads", 1);
1125 } else setProducerProperty("threads", value.toInt());
1126 } else if (key == "video_index") {
1127 if (value.isEmpty()) {
1128 m_properties.remove("video_index");
1129 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1130 } else setProducerProperty("video_index", value.toInt());
1131 } else if (key == "audio_index") {
1132 if (value.isEmpty()) {
1133 m_properties.remove("audio_index");
1134 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1135 } else setProducerProperty("audio_index", value.toInt());
1136 } else if (key == "force_colorspace") {
1137 if (value.isEmpty()) {
1138 m_properties.remove("force_colorspace");
1139 resetProducerProperty("force_colorspace");
1140 } else setProducerProperty("force_colorspace", value.toInt());
1141 } else if (key == "full_luma") {
1142 if (value.isEmpty()) {
1143 m_properties.remove("full_luma");
1144 resetProducerProperty("set.force_full_luma");
1145 } else setProducerProperty("set.force_full_luma", value.toInt());
1149 QMap <QString, QString> DocClipBase::properties() const
1151 return m_properties;
1154 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1156 QMap <QString, QString> currentProps;
1157 QMap<QString, QString>::const_iterator i = props.constBegin();
1158 while (i != props.constEnd()) {
1159 currentProps.insert(i.key(), m_properties.value(i.key()));
1162 return currentProps;
1165 bool DocClipBase::getAudioThumbs()
1167 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1168 if (m_audioThumbCreated) {
1171 m_audioTimer.start();
1175 bool DocClipBase::isPlaceHolder() const
1177 return m_placeHolder;
1180 void DocClipBase::addCutZone(int in, int out, QString desc)
1183 info.zone = QPoint(in, out);
1184 info.description = desc;
1185 for (int i = 0; i < m_cutZones.count(); i++)
1186 if (m_cutZones.at(i).zone == info.zone) {
1189 m_cutZones.append(info);
1192 bool DocClipBase::hasCutZone(QPoint p) const
1194 for (int i = 0; i < m_cutZones.count(); i++)
1195 if (m_cutZones.at(i).zone == p) return true;
1200 void DocClipBase::removeCutZone(int in, int out)
1203 for (int i = 0; i < m_cutZones.count(); i++) {
1204 if (m_cutZones.at(i).zone == p) {
1205 m_cutZones.removeAt(i);
1211 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1213 QPoint old(oldin, oldout);
1214 for (int i = 0; i < m_cutZones.size(); ++i) {
1215 if (m_cutZones.at(i).zone == old) {
1217 info.zone = QPoint(in, out);
1218 info.description = desc;
1219 m_cutZones.replace(i, info);
1225 QList <CutZoneInfo> DocClipBase::cutZones() const
1230 bool DocClipBase::hasVideoCodec(const QString &codec) const
1232 Mlt::Producer *prod = NULL;
1233 if (m_baseTrackProducers.count() == 0) return false;
1234 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1235 if (m_baseTrackProducers.at(i) != NULL) {
1236 prod = m_baseTrackProducers.at(i);
1241 if (!prod) return false;
1242 int default_video = prod->get_int("video_index");
1244 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1245 return prod->get(property) == codec;
1248 bool DocClipBase::hasAudioCodec(const QString &codec) const
1250 Mlt::Producer *prod = NULL;
1251 if (m_baseTrackProducers.count() == 0) return false;
1252 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1253 if (m_baseTrackProducers.at(i) != NULL) {
1254 prod = m_baseTrackProducers.at(i);
1258 if (!prod) return false;
1259 int default_video = prod->get_int("audio_index");
1261 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1262 return prod->get(property) == codec;
1266 void DocClipBase::slotExtractImage(QList <int> frames)
1268 if (m_thumbProd == NULL) return;
1269 m_thumbProd->extractImage(frames);
1272 QImage DocClipBase::extractImage(int frame, int width, int height)
1274 if (m_thumbProd == NULL) return QImage();
1275 QMutexLocker locker(&m_producerMutex);
1276 return m_thumbProd->extractImage(frame, width, height);
1279 void DocClipBase::setAnalysisData(const QString &name, const QString &data)
1281 if (data.isEmpty()) m_analysisdata.remove(name);
1283 if (m_analysisdata.contains(name)) {
1285 QString newname = name + " " + QString::number(i);
1286 while (m_analysisdata.contains(newname)) {
1288 newname = name + " " + QString::number(i);
1290 m_analysisdata.insert(newname, data);
1292 else m_analysisdata.insert(name, data);
1296 QMap <QString, QString> DocClipBase::analysisData() const
1298 return m_analysisdata;