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;
299 for (int count = 0; count < m_snapMarkers.count(); ++count) {
300 markers.append(m_snapMarkers.at(count).time());
306 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
308 return m_snapMarkers;
312 void DocClipBase::addSnapMarker(const CommentedTime marker)
314 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
315 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
316 if ((*it).time() >= marker.time())
320 if ((it != m_snapMarkers.end()) && ((*it).time() == marker.time())) {
321 (*it).setComment(marker.comment());
322 (*it).setMarkerType(marker.markerType());
323 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
325 m_snapMarkers.insert(it, marker);
329 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
331 QList < CommentedTime >::Iterator it;
332 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
333 if ((*it).time() == time)
336 if (it != m_snapMarkers.end()) {
337 (*it).setComment(comment);
339 kError() << "trying to edit Snap Marker that does not already exists";
343 QString DocClipBase::deleteSnapMarker(const GenTime & time)
345 QString result = i18n("Marker");
346 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
348 while (itt != m_snapMarkers.end()) {
349 if ((*itt).time() == time)
354 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
355 result = (*itt).comment();
356 m_snapMarkers.erase(itt);
362 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
364 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
366 while (itt != m_snapMarkers.end()) {
367 if ((*itt).time() == time)
375 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
378 for (it = 0; it < m_snapMarkers.count(); it++) {
379 if (m_snapMarkers.at(it).time() >= currTime)
382 if (it == 0) return GenTime();
383 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
384 return m_snapMarkers.at(it).time();
385 else return m_snapMarkers.at(it - 1).time();
388 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
391 for (it = 0; it < m_snapMarkers.count(); it++) {
392 if (m_snapMarkers.at(it).time() > currTime)
395 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
399 QString DocClipBase::markerComment(GenTime t) const
401 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
402 while (itt != m_snapMarkers.end()) {
403 if ((*itt).time() == t)
404 return (*itt).comment();
410 CommentedTime DocClipBase::markerAt(GenTime t) const
412 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
413 while (itt != m_snapMarkers.end()) {
414 if ((*itt).time() == t)
418 return CommentedTime();
421 void DocClipBase::clearThumbProducer()
423 if (m_thumbProd) m_thumbProd->clearProducer();
426 void DocClipBase::reloadThumbProducer()
428 if (m_thumbProd && !m_thumbProd->hasProducer())
429 m_thumbProd->setProducer(getProducer());
432 void DocClipBase::deleteProducers()
434 if (m_thumbProd) m_thumbProd->clearProducer();
436 if (numReferences() > 0 && (!m_baseTrackProducers.isEmpty() || !m_videoTrackProducers.isEmpty() || !m_audioTrackProducers.isEmpty())) {
437 // Clip is used in timeline, delay producers deletion
438 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
439 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
441 for (int i = 0; i < m_videoTrackProducers.count(); i++) {
442 m_toDeleteProducers.append(m_videoTrackProducers.at(i));
444 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
445 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
449 qDeleteAll(m_baseTrackProducers);
450 qDeleteAll(m_videoTrackProducers);
451 qDeleteAll(m_audioTrackProducers);
452 m_replaceMutex.unlock();
454 m_baseTrackProducers.clear();
455 m_videoTrackProducers.clear();
456 m_audioTrackProducers.clear();
459 void DocClipBase::cleanupProducers()
463 kDebug()<<"----------------------------------------------------------------------------------";
464 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
465 if (m_toDeleteProducers.at(i) != NULL) {
466 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
467 if (props.ref_count() > 2) {
468 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
476 qDeleteAll(m_toDeleteProducers);
477 m_toDeleteProducers.clear();
478 m_replaceMutex.unlock();
482 bool DocClipBase::isClean() const
484 return m_toDeleteProducers.isEmpty();
487 void DocClipBase::setValid()
489 m_placeHolder = false;
492 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
494 if (producer == NULL) return;
496 QMutexLocker locker(&m_producerMutex);
497 m_replaceMutex.lock();
500 QString id = producer->get("id");
501 if (m_placeHolder || !producer->is_valid()) {
502 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
503 producer->set("markup", tmp);
504 producer->set("bgcolour", "0xff0000ff");
505 producer->set("pad", "10");
508 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
509 if (m_clipType != AUDIO) {
510 if (!id.endsWith("_audio"))
511 m_thumbProd->setProducer(producer);
513 else m_thumbProd->setProducer(producer);
516 bool updated = false;
517 if (id.contains('_')) {
518 // this is a subtrack producer, insert it at correct place
519 id = id.section('_', 1);
520 if (id.endsWith("audio")) {
521 int pos = id.section('_', 0, 0).toInt();
522 if (pos >= m_audioTrackProducers.count()) {
523 while (m_audioTrackProducers.count() - 1 < pos) {
524 m_audioTrackProducers.append(NULL);
527 if (m_audioTrackProducers.at(pos) == NULL) {
528 m_audioTrackProducers[pos] = producer;
531 else delete producer;
533 } else if (id.endsWith("video")) {
535 // Keep compatibility with older projects where video only producers were not track specific
536 if (id.contains('_')) pos = id.section('_', 0, 0).toInt();
537 if (pos >= m_videoTrackProducers.count()) {
538 while (m_videoTrackProducers.count() - 1 < pos) {
539 m_videoTrackProducers.append(NULL);
542 if (m_videoTrackProducers.at(pos) == NULL) {
543 m_videoTrackProducers[pos] = producer;
546 else delete producer;
549 int pos = id.toInt();
550 if (pos >= m_baseTrackProducers.count()) {
551 while (m_baseTrackProducers.count() - 1 < pos) {
552 m_baseTrackProducers.append(NULL);
555 if (m_baseTrackProducers.at(pos) == NULL) {
556 m_baseTrackProducers[pos] = producer;
559 else delete producer;
561 if (m_baseTrackProducers.isEmpty()) {
562 m_baseTrackProducers.append(producer);
565 else if (m_baseTrackProducers.at(0) == NULL) {
566 m_baseTrackProducers[0] = producer;
569 else delete producer;
571 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
572 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
575 static double getPixelAspect(QMap<QString, QString>& props) {
576 int width = props.value("frame_size").section('x', 0, 0).toInt();
577 int height = props.value("frame_size").section('x', 1, 1).toInt();
578 int aspectNumerator = props.value("force_aspect_num").toInt();
579 int aspectDenominator = props.value("force_aspect_den").toInt();
580 if (aspectDenominator != 0 && width != 0)
581 return double(height) * aspectNumerator / aspectDenominator / width;
586 Mlt::Producer *DocClipBase::audioProducer(int track)
588 QMutexLocker locker(&m_producerMutex);
589 if (m_audioTrackProducers.count() <= track) {
590 while (m_audioTrackProducers.count() - 1 < track) {
591 m_audioTrackProducers.append(NULL);
594 if (m_audioTrackProducers.at(track) == NULL) {
596 for (i = 0; i < m_audioTrackProducers.count(); i++)
597 if (m_audioTrackProducers.at(i) != NULL) break;
599 if (i >= m_audioTrackProducers.count()) {
600 // Could not find a valid producer for that clip
602 base = getProducer();
608 else base = m_audioTrackProducers.at(i);
609 m_audioTrackProducers[track] = cloneProducer(base);
610 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
612 return m_audioTrackProducers.at(track);
616 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
618 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
619 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
620 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
621 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
622 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
623 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
624 if (mute) prod->set("audio_index", -1);
625 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
626 if (blind) prod->set("video_index", -1);
627 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
628 prod->set("id", id.toUtf8().constData());
629 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
630 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
631 if (m_properties.contains("proxy_out")) {
632 // We have a proxy clip, make sure the proxy has same duration as original
633 prod->set("length", m_properties.value("duration").toInt());
634 prod->set("out", m_properties.value("proxy_out").toInt());
639 Mlt::Producer *DocClipBase::videoProducer(int track)
641 QMutexLocker locker(&m_producerMutex);
642 if (m_videoTrackProducers.count() <= track) {
643 while (m_videoTrackProducers.count() - 1 < track) {
644 m_videoTrackProducers.append(NULL);
647 if (m_videoTrackProducers.at(track) == NULL) {
649 for (i = 0; i < m_videoTrackProducers.count(); i++)
650 if (m_videoTrackProducers.at(i) != NULL) break;
652 if (i >= m_videoTrackProducers.count()) {
653 // Could not find a valid producer for that clip
655 base = getProducer();
661 else base = m_videoTrackProducers.at(i);
662 m_videoTrackProducers[track] = cloneProducer(base);
663 adjustProducerProperties(m_videoTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_video"), true, false);
665 return m_videoTrackProducers.at(track);
668 Mlt::Producer *DocClipBase::getCloneProducer()
670 Mlt::Producer *source = NULL;
671 Mlt::Producer *prod = NULL;
672 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
673 source = getProducer();
674 if (!source) return NULL;
676 if (m_clipType == COLOR) {
677 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
678 } else if (m_clipType == TEXT) {
679 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
680 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
681 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
685 QMutexLocker locker(&m_producerMutex);
686 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
687 if (m_baseTrackProducers.at(i) != NULL) {
688 source = m_baseTrackProducers.at(i);
692 if (!source) return NULL;
694 prod = cloneProducer(source);
697 adjustProducerProperties(prod, getId() + "_", false, false);
698 if (!m_properties.contains("proxy_out")) {
699 // Adjust length in case...
700 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
701 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
703 if (m_clipType == AUDIO) {
704 prod->set("_audioclip", 1);
711 Mlt::Producer *DocClipBase::getProducer(int track)
713 QMutexLocker locker(&m_producerMutex);
714 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
715 if (m_baseTrackProducers.count() == 0) {
718 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
719 if (m_baseTrackProducers.at(i) != NULL) {
720 return m_baseTrackProducers.at(i);
725 if (track >= m_baseTrackProducers.count()) {
726 while (m_baseTrackProducers.count() - 1 < track) {
727 m_baseTrackProducers.append(NULL);
730 if (m_baseTrackProducers.at(track) == NULL) {
732 for (i = 0; i < m_baseTrackProducers.count(); i++)
733 if (m_baseTrackProducers.at(i) != NULL) break;
735 if (i >= m_baseTrackProducers.count()) {
736 // Could not find a valid producer for that clip, check in
739 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
740 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
741 m_baseTrackProducers[track] = prod;
743 return m_baseTrackProducers.at(track);
747 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
749 Mlt::Producer *result = NULL;
750 QString url = QString::fromUtf8(source->get("resource"));
751 if (url == "<playlist>" || url == "<tractor>" || url == "<producer>") {
752 // Xml producer sometimes loses the correct url
753 url = m_properties.value("resource");
755 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
756 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
758 if (result == NULL || !result->is_valid()) {
760 QString txt = "+" + i18n("Missing clip") + ".txt";
761 char *tmp = qstrdup(txt.toUtf8().constData());
762 result = new Mlt::Producer(*source->profile(), tmp);
764 if (result == NULL || !result->is_valid())
765 result = new Mlt::Producer(*(source->profile()), "colour:red");
767 result->set("bgcolour", "0xff0000ff");
768 result->set("pad", "10");
772 /*Mlt::Properties src_props(source->get_properties());
773 Mlt::Properties props(result->get_properties());
774 props.inherit(src_props);*/
778 void DocClipBase::setProducerProperty(const char *name, int data)
780 QMutexLocker locker(&m_producerMutex);
781 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
782 if (m_baseTrackProducers.at(i) != NULL)
783 m_baseTrackProducers[i]->set(name, data);
787 void DocClipBase::setProducerProperty(const char *name, double data)
789 QMutexLocker locker(&m_producerMutex);
790 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
791 if (m_baseTrackProducers.at(i) != NULL)
792 m_baseTrackProducers[i]->set(name, data);
796 void DocClipBase::setProducerProperty(const char *name, const char *data)
798 QMutexLocker locker(&m_producerMutex);
799 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
800 if (m_baseTrackProducers.at(i) != NULL)
801 m_baseTrackProducers[i]->set(name, data);
805 void DocClipBase::resetProducerProperty(const char *name)
807 QMutexLocker locker(&m_producerMutex);
808 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
809 if (m_baseTrackProducers.at(i) != NULL)
810 m_baseTrackProducers[i]->set(name, (const char*) NULL);
814 const char *DocClipBase::producerProperty(const char *name) const
816 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
817 if (m_baseTrackProducers.at(i) != NULL) {
818 return m_baseTrackProducers.at(i)->get(name);
825 void DocClipBase::slotRefreshProducer()
827 if (m_baseTrackProducers.count() == 0) return;
828 if (m_clipType == SLIDESHOW) {
829 setProducerProperty("ttl", getProperty("ttl").toInt());
830 //m_clipProducer->set("id", getProperty("id"));
831 if (!getProperty("animation").isEmpty()) {
832 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
834 Mlt::Filter *filter = clipService.filter(ct);
836 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
838 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
839 clipService.detach(*filter);
841 filter = clipService.filter(ct);
844 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
845 // filter does not exist, create it.
846 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
847 if (filter && filter->is_valid()) {
848 int cycle = getProperty("ttl").toInt();
849 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
850 if (!geometry.isEmpty()) {
851 if (getProperty("animation").contains("low-pass")) {
852 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
853 if (blur && blur->is_valid())
854 clipService.attach(*blur);
856 filter->set("transition.geometry", geometry.toUtf8().data());
857 filter->set("transition.cycle", cycle);
858 clipService.attach(*filter);
863 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
865 Mlt::Filter *filter = clipService.filter(0);
867 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
868 clipService.detach(*filter);
870 filter = clipService.filter(ct);
873 if (getProperty("fade") == "1") {
874 // we want a fade filter effect
875 kDebug() << "//////////// FADE WANTED";
876 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
878 Mlt::Filter *filter = clipService.filter(ct);
880 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
884 filter = clipService.filter(ct);
887 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
888 filter->set("cycle", getProperty("ttl").toInt());
889 filter->set("duration", getProperty("luma_duration").toInt());
890 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
891 if (!getProperty("softness").isEmpty()) {
892 int soft = getProperty("softness").toInt();
893 filter->set("luma.softness", (double) soft / 100.0);
896 // filter does not exist, create it...
897 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
898 filter->set("cycle", getProperty("ttl").toInt());
899 filter->set("duration", getProperty("luma_duration").toInt());
900 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
901 if (!getProperty("softness").isEmpty()) {
902 int soft = getProperty("softness").toInt();
903 filter->set("luma.softness", (double) soft / 100.0);
905 clipService.attach(*filter);
908 kDebug() << "//////////// FADE NOT WANTED!!!";
909 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
911 Mlt::Filter *filter = clipService.filter(0);
913 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
914 clipService.detach(*filter);
916 filter = clipService.filter(ct);
919 if (getProperty("crop") == "1") {
920 // we want a center crop filter effect
921 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
923 Mlt::Filter *filter = clipService.filter(ct);
925 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
929 filter = clipService.filter(ct);
932 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
933 // filter does not exist, create it...
934 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
935 filter->set("center", 1);
936 clipService.attach(*filter);
939 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
941 Mlt::Filter *filter = clipService.filter(0);
943 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
944 clipService.detach(*filter);
946 filter = clipService.filter(ct);
952 void DocClipBase::setProperties(QMap <QString, QString> properties)
954 // changing clip type is not allowed
955 properties.remove("type");
956 QMapIterator<QString, QString> i(properties);
957 bool refreshProducer = false;
959 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
960 QString oldProxy = m_properties.value("proxy");
961 while (i.hasNext()) {
963 setProperty(i.key(), i.value());
964 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
966 if (properties.contains("proxy")) {
967 QString value = properties.value("proxy");
968 // If value is "-", that means user manually disabled proxy on this clip
969 if (value.isEmpty() || value == "-") {
971 emit abortProxy(m_id, oldProxy);
974 emit createProxy(m_id);
977 if (refreshProducer) slotRefreshProducer();
980 void DocClipBase::setMetadata(QMap <QString, QString> properties)
982 QMapIterator<QString, QString> i(properties);
983 while (i.hasNext()) {
985 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
986 m_metadata.remove(i.key());
988 m_metadata.insert(i.key(), i.value());
993 QMap <QString, QString> DocClipBase::metadata() const
998 void DocClipBase::clearProperty(const QString &key)
1000 m_properties.remove(key);
1003 void DocClipBase::getFileHash(const QString &url)
1005 if (m_clipType == SLIDESHOW) return;
1007 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1008 QByteArray fileData;
1009 QByteArray fileHash;
1010 //kDebug() << "SETTING HASH of" << value;
1011 m_properties.insert("file_size", QString::number(file.size()));
1013 * 1 MB = 1 second per 450 files (or faster)
1014 * 10 MB = 9 seconds per 450 files (or faster)
1016 if (file.size() > 1000000*2) {
1017 fileData = file.read(1000000);
1018 if (file.seek(file.size() - 1000000))
1019 fileData.append(file.readAll());
1021 fileData = file.readAll();
1023 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1024 m_properties.insert("file_hash", QString(fileHash.toHex()));
1028 bool DocClipBase::checkHash() const
1030 KUrl url = fileURL();
1031 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
1035 QString DocClipBase::getClipHash() const
1038 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
1039 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
1040 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
1042 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
1043 if (hash.isEmpty()) hash = getHash(fileURL().path());
1049 void DocClipBase::setPlaceHolder(bool place)
1051 m_placeHolder = place;
1055 QString DocClipBase::getHash(const QString &path)
1058 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1059 QByteArray fileData;
1060 QByteArray fileHash;
1062 * 1 MB = 1 second per 450 files (or faster)
1063 * 10 MB = 9 seconds per 450 files (or faster)
1065 if (file.size() > 1000000*2) {
1066 fileData = file.read(1000000);
1067 if (file.seek(file.size() - 1000000))
1068 fileData.append(file.readAll());
1070 fileData = file.readAll();
1072 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1077 void DocClipBase::refreshThumbUrl()
1079 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1082 void DocClipBase::setProperty(const QString &key, const QString &value)
1084 m_properties.insert(key, value);
1085 if (key == "resource") {
1087 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1088 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1089 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1090 else if (key == "colour") {
1091 setProducerProperty("colour", value.toUtf8().data());
1092 } else if (key == "templatetext") {
1093 setProducerProperty("templatetext", value.toUtf8().data());
1094 setProducerProperty("force_reload", 1);
1095 } else if (key == "xmldata") {
1096 setProducerProperty("xmldata", value.toUtf8().data());
1097 setProducerProperty("force_reload", 1);
1098 } else if (key == "force_aspect_num") {
1099 if (value.isEmpty()) {
1100 m_properties.remove("force_aspect_num");
1101 resetProducerProperty("force_aspect_ratio");
1102 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1103 } else if (key == "force_aspect_den") {
1104 if (value.isEmpty()) {
1105 m_properties.remove("force_aspect_den");
1106 resetProducerProperty("force_aspect_ratio");
1107 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1108 } else if (key == "force_fps") {
1109 if (value.isEmpty()) {
1110 m_properties.remove("force_fps");
1111 resetProducerProperty("force_fps");
1112 } else setProducerProperty("force_fps", value.toDouble());
1113 } else if (key == "force_progressive") {
1114 if (value.isEmpty()) {
1115 m_properties.remove("force_progressive");
1116 resetProducerProperty("force_progressive");
1117 } else setProducerProperty("force_progressive", value.toInt());
1118 } else if (key == "force_tff") {
1119 if (value.isEmpty()) {
1120 m_properties.remove("force_tff");
1121 resetProducerProperty("force_tff");
1122 } else setProducerProperty("force_tff", value.toInt());
1123 } else if (key == "threads") {
1124 if (value.isEmpty()) {
1125 m_properties.remove("threads");
1126 setProducerProperty("threads", 1);
1127 } else setProducerProperty("threads", value.toInt());
1128 } else if (key == "video_index") {
1129 if (value.isEmpty()) {
1130 m_properties.remove("video_index");
1131 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1132 } else setProducerProperty("video_index", value.toInt());
1133 } else if (key == "audio_index") {
1134 if (value.isEmpty()) {
1135 m_properties.remove("audio_index");
1136 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1137 } else setProducerProperty("audio_index", value.toInt());
1138 } else if (key == "force_colorspace") {
1139 if (value.isEmpty()) {
1140 m_properties.remove("force_colorspace");
1141 resetProducerProperty("force_colorspace");
1142 } else setProducerProperty("force_colorspace", value.toInt());
1143 } else if (key == "full_luma") {
1144 if (value.isEmpty()) {
1145 m_properties.remove("full_luma");
1146 resetProducerProperty("set.force_full_luma");
1147 } else setProducerProperty("set.force_full_luma", value.toInt());
1151 QMap <QString, QString> DocClipBase::properties() const
1153 return m_properties;
1156 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1158 QMap <QString, QString> currentProps;
1159 QMap<QString, QString>::const_iterator i = props.constBegin();
1160 while (i != props.constEnd()) {
1161 currentProps.insert(i.key(), m_properties.value(i.key()));
1164 return currentProps;
1167 bool DocClipBase::getAudioThumbs()
1169 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1170 if (m_audioThumbCreated) {
1173 m_audioTimer.start();
1177 bool DocClipBase::isPlaceHolder() const
1179 return m_placeHolder;
1182 void DocClipBase::addCutZone(int in, int out, QString desc)
1185 info.zone = QPoint(in, out);
1186 info.description = desc;
1187 for (int i = 0; i < m_cutZones.count(); i++)
1188 if (m_cutZones.at(i).zone == info.zone) {
1191 m_cutZones.append(info);
1194 bool DocClipBase::hasCutZone(QPoint p) const
1196 for (int i = 0; i < m_cutZones.count(); i++)
1197 if (m_cutZones.at(i).zone == p) return true;
1202 void DocClipBase::removeCutZone(int in, int out)
1205 for (int i = 0; i < m_cutZones.count(); i++) {
1206 if (m_cutZones.at(i).zone == p) {
1207 m_cutZones.removeAt(i);
1213 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1215 QPoint old(oldin, oldout);
1216 for (int i = 0; i < m_cutZones.size(); ++i) {
1217 if (m_cutZones.at(i).zone == old) {
1219 info.zone = QPoint(in, out);
1220 info.description = desc;
1221 m_cutZones.replace(i, info);
1227 QList <CutZoneInfo> DocClipBase::cutZones() const
1232 bool DocClipBase::hasVideoCodec(const QString &codec) const
1234 Mlt::Producer *prod = NULL;
1235 if (m_baseTrackProducers.count() == 0) return false;
1236 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1237 if (m_baseTrackProducers.at(i) != NULL) {
1238 prod = m_baseTrackProducers.at(i);
1243 if (!prod) return false;
1244 int default_video = prod->get_int("video_index");
1246 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1247 return prod->get(property) == codec;
1250 bool DocClipBase::hasAudioCodec(const QString &codec) const
1252 Mlt::Producer *prod = NULL;
1253 if (m_baseTrackProducers.count() == 0) return false;
1254 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1255 if (m_baseTrackProducers.at(i) != NULL) {
1256 prod = m_baseTrackProducers.at(i);
1260 if (!prod) return false;
1261 int default_video = prod->get_int("audio_index");
1263 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1264 return prod->get(property) == codec;
1268 void DocClipBase::slotExtractImage(QList <int> frames)
1270 if (m_thumbProd == NULL) return;
1271 m_thumbProd->extractImage(frames);
1274 QImage DocClipBase::extractImage(int frame, int width, int height)
1276 if (m_thumbProd == NULL) return QImage();
1277 QMutexLocker locker(&m_producerMutex);
1278 return m_thumbProd->extractImage(frame, width, height);
1281 void DocClipBase::setAnalysisData(const QString &name, const QString &data, int offset)
1283 if (data.isEmpty()) m_analysisdata.remove(name);
1285 if (m_analysisdata.contains(name)) {
1286 if (KMessageBox::questionYesNo(kapp->activeWindow(), i18n("Clip already contains analysis data %1", name), QString(), KGuiItem(i18n("Merge")), KGuiItem(i18n("Add"))) == KMessageBox::Yes) {
1288 Mlt::Profile *profile = m_baseTrackProducers.at(0)->profile();
1289 Mlt::Geometry geometry(m_analysisdata.value(name).toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1290 Mlt::Geometry newGeometry(data.toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1291 Mlt::GeometryItem item;
1293 while (!newGeometry.next_key(&item, pos)) {
1295 item.frame(pos + offset);
1297 geometry.insert(item);
1299 m_analysisdata.insert(name, geometry.serialise());
1302 // Add data with another name
1304 QString newname = name + " " + QString::number(i);
1305 while (m_analysisdata.contains(newname)) {
1307 newname = name + " " + QString::number(i);
1309 m_analysisdata.insert(newname, geometryWithOffset(data, offset));
1312 else m_analysisdata.insert(name, geometryWithOffset(data, offset));
1316 const QString DocClipBase::geometryWithOffset(QString data, int offset)
1318 if (offset == 0) return data;
1319 Mlt::Profile *profile = m_baseTrackProducers.at(0)->profile();
1320 Mlt::Geometry geometry(data.toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1321 Mlt::Geometry newgeometry("", m_properties.value("duration").toInt(), profile->width(), profile->height());
1322 Mlt::GeometryItem item;
1324 while (!geometry.next_key(&item, pos)) {
1326 item.frame(pos + offset);
1328 newgeometry.insert(item);
1330 return newgeometry.serialise();
1333 QMap <QString, QString> DocClipBase::analysisData() const
1335 return m_analysisdata;