1 /***************************************************************************
2 * DocClipBase.cpp - description *
3 * ------------------- *
4 * begin : Fri Apr 12 2002 *
5 * Copyright (C) 2002 by Jason Wood (jasonwood@blueyonder.co.uk) *
6 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
22 ***************************************************************************/
27 #include "docclipbase.h"
28 #include "kdenlivesettings.h"
30 #include "clipmanager.h"
31 #include "slideshowclip.h"
33 #include <KIO/NetAccess>
34 #include <KStandardDirs>
37 #include <QCryptographicHash>
38 #include <QtConcurrentRun>
42 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id) :
46 m_baseTrackProducers(),
47 m_audioTrackProducers(),
48 m_videoOnlyProducer(NULL),
49 m_snapMarkers(QList < CommentedTime >()),
52 m_audioThumbCreated(false),
54 m_placeHolder(xml.hasAttribute("placeholder")),
57 int type = xml.attribute("type").toInt();
58 m_clipType = (CLIPTYPE) type;
59 if (m_placeHolder) xml.removeAttribute("placeholder");
60 QDomNamedNodeMap attributes = xml.attributes();
61 for (int i = 0; i < attributes.count(); i++) {
62 QString name = attributes.item(i).nodeName();
63 if (name.startsWith("meta.attr.")) {
64 m_metadata.insert(name.section('.', 2, 3), attributes.item(i).nodeValue());
65 } else m_properties.insert(name, attributes.item(i).nodeValue());
68 if (xml.hasAttribute("cutzones")) {
69 QStringList cuts = xml.attribute("cutzones").split(";", QString::SkipEmptyParts);
70 for (int i = 0; i < cuts.count(); i++) {
71 QString z = cuts.at(i);
72 addCutZone(z.section('-', 0, 0).toInt(), z.section('-', 1, 1).toInt(), z.section('-', 2, 2));
76 KUrl url = KUrl(xml.attribute("resource"));
77 if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
79 if (xml.hasAttribute("duration")) {
80 setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
82 int out = xml.attribute("out").toInt();
83 int in = xml.attribute("in").toInt();
84 setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
87 if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
89 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
91 // Setup timer to trigger audio thumbs creation
92 m_audioTimer.setSingleShot(true);
93 m_audioTimer.setInterval(800);
94 connect(&m_audioTimer, SIGNAL(timeout()), m_thumbProd, SLOT(slotCreateAudioThumbs()));
98 DocClipBase::~DocClipBase()
103 qDeleteAll(m_toDeleteProducers);
104 m_toDeleteProducers.clear();
105 qDeleteAll(m_baseTrackProducers);
106 m_baseTrackProducers.clear();
107 qDeleteAll(m_audioTrackProducers);
108 m_audioTrackProducers.clear();
109 delete m_videoOnlyProducer;
110 m_videoOnlyProducer = NULL;
113 void DocClipBase::setZone(QPoint zone)
115 m_properties.insert("zone_in", QString::number(zone.x()));
116 m_properties.insert("zone_out", QString::number(zone.y()));
119 QPoint DocClipBase::zone() const
121 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
126 bool DocClipBase::hasAudioThumb() const
128 if (m_clipType == AUDIO || m_clipType == AV || m_clipType == PLAYLIST) return true;
132 void DocClipBase::slotClearAudioCache()
134 m_audioFrameCache.clear();
135 m_audioThumbCreated = false;
138 /*void DocClipBase::getClipMainThumb() {
139 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
142 KThumb *DocClipBase::thumbProducer()
147 bool DocClipBase::audioThumbCreated() const
149 return m_audioThumbCreated;
152 const QString DocClipBase::name() const
155 return m_properties.value("name");
158 const QString &DocClipBase::getId() const
163 const CLIPTYPE & DocClipBase::clipType() const
168 void DocClipBase::setClipType(CLIPTYPE type)
171 m_properties.insert("type", QString::number((int) type));
174 KUrl DocClipBase::fileURL() const
176 QString res = m_properties.value("resource");
177 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
181 void DocClipBase::setClipThumbFrame(const uint &ix)
183 m_properties.insert("thumbnail", QString::number((int) ix));
186 uint DocClipBase::getClipThumbFrame() const
188 return (uint) m_properties.value("thumbnail").toInt();
191 const QString DocClipBase::description() const
193 return m_properties.value("description");
196 bool DocClipBase::isTransparent() const
198 return (m_properties.value("transparency") == "1");
201 const QString DocClipBase::getProperty(const QString &prop) const
203 return m_properties.value(prop);
206 void DocClipBase::setDuration(GenTime dur)
209 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
212 const GenTime &DocClipBase::duration() const
217 const GenTime DocClipBase::maxDuration() const
219 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
220 /*const GenTime dur(15000, KdenliveSettings::project_fps());
227 bool DocClipBase::hasFileSize() const
232 qulonglong DocClipBase::fileSize() const
234 return m_properties.value("file_size").toULongLong();
238 QDomElement DocClipBase::toXML() const
241 QDomElement clip = doc.createElement("producer");
243 QMapIterator<QString, QString> i(m_properties);
244 while (i.hasNext()) {
246 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
248 doc.appendChild(clip);
249 if (!m_cutZones.isEmpty()) {
251 for (int i = 0; i < m_cutZones.size(); i++) {
252 CutZoneInfo info = m_cutZones.at(i);
253 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
255 clip.setAttribute("cutzones", cuts.join(";"));
257 //kDebug() << "/// CLIP XML: " << doc.toString();
258 return doc.documentElement();
262 void DocClipBase::setAudioThumbCreated(bool isDone)
264 m_audioThumbCreated = isDone;
267 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
269 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
270 m_audioFrameCache = data;
271 m_audioThumbCreated = true;
275 QList < GenTime > DocClipBase::snapMarkers() const
277 QList < GenTime > markers;
279 for (int count = 0; count < m_snapMarkers.count(); ++count) {
280 markers.append(m_snapMarkers.at(count).time());
286 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
288 return m_snapMarkers;
292 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
294 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
295 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
296 if ((*it).time() >= time)
300 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
301 (*it).setComment(comment);
302 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
304 CommentedTime t(time, comment);
305 m_snapMarkers.insert(it, t);
310 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
312 QList < CommentedTime >::Iterator it;
313 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
314 if ((*it).time() == time)
317 if (it != m_snapMarkers.end()) {
318 (*it).setComment(comment);
320 kError() << "trying to edit Snap Marker that does not already exists";
324 QString DocClipBase::deleteSnapMarker(const GenTime & time)
326 QString result = i18n("Marker");
327 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
329 while (itt != m_snapMarkers.end()) {
330 if ((*itt).time() == time)
335 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
336 result = (*itt).comment();
337 m_snapMarkers.erase(itt);
343 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
345 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
347 while (itt != m_snapMarkers.end()) {
348 if ((*itt).time() == time)
356 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
359 for (it = 0; it < m_snapMarkers.count(); it++) {
360 if (m_snapMarkers.at(it).time() >= currTime)
363 if (it == 0) return GenTime();
364 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
365 return m_snapMarkers.at(it).time();
366 else return m_snapMarkers.at(it - 1).time();
369 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
372 for (it = 0; it < m_snapMarkers.count(); it++) {
373 if (m_snapMarkers.at(it).time() > currTime)
376 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
380 QString DocClipBase::markerComment(GenTime t)
382 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
384 while (itt != m_snapMarkers.end()) {
385 if ((*itt).time() == t)
386 return (*itt).comment();
392 void DocClipBase::clearThumbProducer()
394 if (m_thumbProd) m_thumbProd->clearProducer();
397 void DocClipBase::reloadThumbProducer()
399 if (m_thumbProd && !m_thumbProd->hasProducer())
400 m_thumbProd->setProducer(getProducer());
403 void DocClipBase::deleteProducers()
405 if (m_thumbProd) m_thumbProd->clearProducer();
407 if (numReferences() > 0) {
408 // Clip is used in timeline, delay producers deletion
409 if (m_videoOnlyProducer) m_toDeleteProducers.append(m_videoOnlyProducer);
410 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
411 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
413 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
414 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
418 delete m_videoOnlyProducer;
419 qDeleteAll(m_baseTrackProducers);
420 qDeleteAll(m_audioTrackProducers);
421 m_replaceMutex.unlock();
423 m_videoOnlyProducer = NULL;
424 m_baseTrackProducers.clear();
425 m_audioTrackProducers.clear();
428 void DocClipBase::cleanupProducers()
432 kDebug()<<"----------------------------------------------------------------------------------";
433 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
434 if (m_toDeleteProducers.at(i) != NULL) {
435 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
436 if (props.ref_count() > 2) {
437 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
444 qDeleteAll(m_toDeleteProducers);
445 m_toDeleteProducers.clear();
446 m_replaceMutex.unlock();
449 bool DocClipBase::isClean() const
451 return m_toDeleteProducers.isEmpty();
454 void DocClipBase::setValid()
456 m_placeHolder = false;
459 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
461 if (producer == NULL) return;
463 QMutexLocker locker(&m_producerMutex);
464 m_replaceMutex.lock();
467 QString id = producer->get("id");
468 if (m_placeHolder || !producer->is_valid()) {
469 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
470 producer->set("markup", tmp);
471 producer->set("bgcolour", "0xff0000ff");
472 producer->set("pad", "10");
475 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
476 if (m_clipType != AUDIO) {
477 if (!id.endsWith("_audio"))
478 m_thumbProd->setProducer(producer);
480 else m_thumbProd->setProducer(producer);
483 bool updated = false;
484 if (id.contains('_')) {
485 // this is a subtrack producer, insert it at correct place
486 id = id.section('_', 1);
487 if (id.endsWith("audio")) {
488 int pos = id.section('_', 0, 0).toInt();
489 if (pos >= m_audioTrackProducers.count()) {
490 while (m_audioTrackProducers.count() - 1 < pos) {
491 m_audioTrackProducers.append(NULL);
494 if (m_audioTrackProducers.at(pos) == NULL) {
495 m_audioTrackProducers[pos] = producer;
498 else delete producer;
500 } else if (id.endsWith("video")) {
501 if (m_videoOnlyProducer == NULL) {
502 m_videoOnlyProducer = producer;
505 else delete producer;
508 int pos = id.toInt();
509 if (pos >= m_baseTrackProducers.count()) {
510 while (m_baseTrackProducers.count() - 1 < pos) {
511 m_baseTrackProducers.append(NULL);
514 if (m_baseTrackProducers.at(pos) == NULL) {
515 m_baseTrackProducers[pos] = producer;
518 else delete producer;
520 if (m_baseTrackProducers.isEmpty()) {
521 m_baseTrackProducers.append(producer);
524 else if (m_baseTrackProducers.at(0) == NULL) {
525 m_baseTrackProducers[0] = producer;
528 else delete producer;
530 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
531 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
534 static double getPixelAspect(QMap<QString, QString>& props) {
535 int width = props.value("frame_size").section('x', 0, 0).toInt();
536 int height = props.value("frame_size").section('x', 1, 1).toInt();
537 int aspectNumerator = props.value("force_aspect_num").toInt();
538 int aspectDenominator = props.value("force_aspect_den").toInt();
539 if (aspectDenominator != 0 && width != 0)
540 return double(height) * aspectNumerator / aspectDenominator / width;
545 Mlt::Producer *DocClipBase::audioProducer(int track)
547 QMutexLocker locker(&m_producerMutex);
548 if (m_audioTrackProducers.count() <= track) {
549 while (m_audioTrackProducers.count() - 1 < track) {
550 m_audioTrackProducers.append(NULL);
553 if (m_audioTrackProducers.at(track) == NULL) {
555 for (i = 0; i < m_audioTrackProducers.count(); i++)
556 if (m_audioTrackProducers.at(i) != NULL) break;
558 if (i >= m_audioTrackProducers.count()) {
559 // Could not find a valid producer for that clip
561 base = getProducer();
567 else base = m_audioTrackProducers.at(i);
568 m_audioTrackProducers[track] = cloneProducer(base);
569 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
571 return m_audioTrackProducers.at(track);
575 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
577 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
578 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
579 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
580 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
581 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
582 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
583 if (mute) prod->set("audio_index", -1);
584 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
585 if (blind) prod->set("video_index", -1);
586 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
587 prod->set("id", id.toUtf8().constData());
588 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
589 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
590 if (m_properties.contains("proxy_out")) {
591 // We have a proxy clip, make sure the proxy has same duration as original
592 prod->set("length", m_properties.value("duration").toInt());
593 prod->set("out", m_properties.value("proxy_out").toInt());
598 Mlt::Producer *DocClipBase::videoProducer()
600 QMutexLocker locker(&m_producerMutex);
601 if (m_videoOnlyProducer == NULL) {
603 for (i = 0; i < m_baseTrackProducers.count(); i++)
604 if (m_baseTrackProducers.at(i) != NULL) break;
605 if (i >= m_baseTrackProducers.count()) return NULL;
606 m_videoOnlyProducer = cloneProducer(m_baseTrackProducers.at(i));
607 adjustProducerProperties(m_videoOnlyProducer, QString(getId() + "_video"), true, false);
609 return m_videoOnlyProducer;
612 Mlt::Producer *DocClipBase::getCloneProducer()
614 Mlt::Producer *source = NULL;
615 Mlt::Producer *prod = NULL;
616 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
617 source = getProducer();
618 if (!source) return NULL;
620 if (m_clipType == COLOR) {
621 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
622 } else if (m_clipType == TEXT) {
623 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
624 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
625 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
629 QMutexLocker locker(&m_producerMutex);
630 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
631 if (m_baseTrackProducers.at(i) != NULL) {
632 source = m_baseTrackProducers.at(i);
636 if (!source) return NULL;
638 prod = cloneProducer(source);
641 adjustProducerProperties(prod, getId() + "_", false, false);
642 if (!m_properties.contains("proxy_out")) {
643 // Adjust length in case...
644 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
645 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
651 Mlt::Producer *DocClipBase::getProducer(int track)
653 QMutexLocker locker(&m_producerMutex);
654 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
655 if (m_baseTrackProducers.count() == 0) {
658 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
659 if (m_baseTrackProducers.at(i) != NULL) {
660 return m_baseTrackProducers.at(i);
665 if (track >= m_baseTrackProducers.count()) {
666 while (m_baseTrackProducers.count() - 1 < track) {
667 m_baseTrackProducers.append(NULL);
670 if (m_baseTrackProducers.at(track) == NULL) {
672 for (i = 0; i < m_baseTrackProducers.count(); i++)
673 if (m_baseTrackProducers.at(i) != NULL) break;
675 if (i >= m_baseTrackProducers.count()) {
676 // Could not find a valid producer for that clip, check in
679 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
680 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
681 m_baseTrackProducers[track] = prod;
683 return m_baseTrackProducers.at(track);
687 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
689 Mlt::Producer *result = NULL;
690 QString url = QString::fromUtf8(source->get("resource"));
691 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
692 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
694 if (result == NULL || !result->is_valid()) {
696 QString txt = "+" + i18n("Missing clip") + ".txt";
697 char *tmp = qstrdup(txt.toUtf8().constData());
698 result = new Mlt::Producer(*source->profile(), tmp);
700 if (result == NULL || !result->is_valid())
701 result = new Mlt::Producer(*(source->profile()), "colour:red");
703 result->set("bgcolour", "0xff0000ff");
704 result->set("pad", "10");
708 /*Mlt::Properties src_props(source->get_properties());
709 Mlt::Properties props(result->get_properties());
710 props.inherit(src_props);*/
714 void DocClipBase::setProducerProperty(const char *name, int data)
716 QMutexLocker locker(&m_producerMutex);
717 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
718 if (m_baseTrackProducers.at(i) != NULL)
719 m_baseTrackProducers[i]->set(name, data);
723 void DocClipBase::setProducerProperty(const char *name, double data)
725 QMutexLocker locker(&m_producerMutex);
726 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
727 if (m_baseTrackProducers.at(i) != NULL)
728 m_baseTrackProducers[i]->set(name, data);
732 void DocClipBase::setProducerProperty(const char *name, const char *data)
734 QMutexLocker locker(&m_producerMutex);
735 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
736 if (m_baseTrackProducers.at(i) != NULL)
737 m_baseTrackProducers[i]->set(name, data);
741 void DocClipBase::resetProducerProperty(const char *name)
743 QMutexLocker locker(&m_producerMutex);
744 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
745 if (m_baseTrackProducers.at(i) != NULL)
746 m_baseTrackProducers[i]->set(name, (const char*) NULL);
750 const char *DocClipBase::producerProperty(const char *name) const
752 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
753 if (m_baseTrackProducers.at(i) != NULL) {
754 return m_baseTrackProducers.at(i)->get(name);
761 void DocClipBase::slotRefreshProducer()
763 if (m_baseTrackProducers.count() == 0) return;
764 if (m_clipType == SLIDESHOW) {
765 setProducerProperty("ttl", getProperty("ttl").toInt());
766 //m_clipProducer->set("id", getProperty("id"));
767 if (!getProperty("animation").isEmpty()) {
768 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
770 Mlt::Filter *filter = clipService.filter(ct);
772 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
774 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
775 clipService.detach(*filter);
777 filter = clipService.filter(ct);
780 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
781 // filter does not exist, create it.
782 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
783 if (filter && filter->is_valid()) {
784 int cycle = getProperty("ttl").toInt();
785 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
786 if (!geometry.isEmpty()) {
787 if (getProperty("animation").contains("low-pass")) {
788 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
789 if (blur && blur->is_valid())
790 clipService.attach(*blur);
792 filter->set("transition.geometry", geometry.toUtf8().data());
793 filter->set("transition.cycle", cycle);
794 clipService.attach(*filter);
799 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
801 Mlt::Filter *filter = clipService.filter(0);
803 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
804 clipService.detach(*filter);
806 filter = clipService.filter(ct);
809 if (getProperty("fade") == "1") {
810 // we want a fade filter effect
811 kDebug() << "//////////// FADE WANTED";
812 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
814 Mlt::Filter *filter = clipService.filter(ct);
816 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
820 filter = clipService.filter(ct);
823 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
824 filter->set("cycle", getProperty("ttl").toInt());
825 filter->set("duration", getProperty("luma_duration").toInt());
826 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
827 if (!getProperty("softness").isEmpty()) {
828 int soft = getProperty("softness").toInt();
829 filter->set("luma.softness", (double) soft / 100.0);
832 // filter does not exist, create it...
833 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
834 filter->set("cycle", getProperty("ttl").toInt());
835 filter->set("duration", getProperty("luma_duration").toInt());
836 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
837 if (!getProperty("softness").isEmpty()) {
838 int soft = getProperty("softness").toInt();
839 filter->set("luma.softness", (double) soft / 100.0);
841 clipService.attach(*filter);
844 kDebug() << "//////////// FADE NOT WANTED!!!";
845 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
847 Mlt::Filter *filter = clipService.filter(0);
849 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
850 clipService.detach(*filter);
852 filter = clipService.filter(ct);
855 if (getProperty("crop") == "1") {
856 // we want a center crop filter effect
857 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
859 Mlt::Filter *filter = clipService.filter(ct);
861 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
865 filter = clipService.filter(ct);
868 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
869 // filter does not exist, create it...
870 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
871 filter->set("center", 1);
872 clipService.attach(*filter);
875 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
877 Mlt::Filter *filter = clipService.filter(0);
879 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
880 clipService.detach(*filter);
882 filter = clipService.filter(ct);
888 void DocClipBase::setProperties(QMap <QString, QString> properties)
890 // changing clip type is not allowed
891 properties.remove("type");
892 QMapIterator<QString, QString> i(properties);
893 bool refreshProducer = false;
895 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
896 QString oldProxy = m_properties.value("proxy");
897 while (i.hasNext()) {
899 setProperty(i.key(), i.value());
900 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
902 if (properties.contains("proxy")) {
903 QString value = properties.value("proxy");
904 // If value is "-", that means user manually disabled proxy on this clip
905 if (value.isEmpty() || value == "-") {
907 emit abortProxy(m_id, oldProxy);
910 emit createProxy(m_id);
913 if (refreshProducer) slotRefreshProducer();
916 void DocClipBase::setMetadata(QMap <QString, QString> properties)
918 QMapIterator<QString, QString> i(properties);
919 while (i.hasNext()) {
921 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
922 m_metadata.remove(i.key());
924 m_metadata.insert(i.key(), i.value());
929 QMap <QString, QString> DocClipBase::metadata() const
934 void DocClipBase::clearProperty(const QString &key)
936 m_properties.remove(key);
939 void DocClipBase::getFileHash(const QString &url)
941 if (m_clipType == SLIDESHOW) return;
943 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
946 //kDebug() << "SETTING HASH of" << value;
947 m_properties.insert("file_size", QString::number(file.size()));
949 * 1 MB = 1 second per 450 files (or faster)
950 * 10 MB = 9 seconds per 450 files (or faster)
952 if (file.size() > 1000000*2) {
953 fileData = file.read(1000000);
954 if (file.seek(file.size() - 1000000))
955 fileData.append(file.readAll());
957 fileData = file.readAll();
959 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
960 m_properties.insert("file_hash", QString(fileHash.toHex()));
964 bool DocClipBase::checkHash() const
966 KUrl url = fileURL();
967 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
971 QString DocClipBase::getClipHash() const
974 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
975 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
976 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
978 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
979 if (hash.isEmpty()) hash = getHash(fileURL().path());
985 void DocClipBase::setPlaceHolder(bool place)
987 m_placeHolder = place;
991 QString DocClipBase::getHash(const QString &path)
994 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
998 * 1 MB = 1 second per 450 files (or faster)
999 * 10 MB = 9 seconds per 450 files (or faster)
1001 if (file.size() > 1000000*2) {
1002 fileData = file.read(1000000);
1003 if (file.seek(file.size() - 1000000))
1004 fileData.append(file.readAll());
1006 fileData = file.readAll();
1008 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1013 void DocClipBase::refreshThumbUrl()
1015 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1018 void DocClipBase::setProperty(const QString &key, const QString &value)
1020 m_properties.insert(key, value);
1021 if (key == "resource") {
1023 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1024 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1025 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1026 else if (key == "colour") {
1027 setProducerProperty("colour", value.toUtf8().data());
1028 } else if (key == "templatetext") {
1029 setProducerProperty("templatetext", value.toUtf8().data());
1030 setProducerProperty("force_reload", 1);
1031 } else if (key == "xmldata") {
1032 setProducerProperty("xmldata", value.toUtf8().data());
1033 setProducerProperty("force_reload", 1);
1034 } else if (key == "force_aspect_num") {
1035 if (value.isEmpty()) {
1036 m_properties.remove("force_aspect_num");
1037 resetProducerProperty("force_aspect_ratio");
1038 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1039 } else if (key == "force_aspect_den") {
1040 if (value.isEmpty()) {
1041 m_properties.remove("force_aspect_den");
1042 resetProducerProperty("force_aspect_ratio");
1043 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1044 } else if (key == "force_fps") {
1045 if (value.isEmpty()) {
1046 m_properties.remove("force_fps");
1047 resetProducerProperty("force_fps");
1048 } else setProducerProperty("force_fps", value.toDouble());
1049 } else if (key == "force_progressive") {
1050 if (value.isEmpty()) {
1051 m_properties.remove("force_progressive");
1052 resetProducerProperty("force_progressive");
1053 } else setProducerProperty("force_progressive", value.toInt());
1054 } else if (key == "force_tff") {
1055 if (value.isEmpty()) {
1056 m_properties.remove("force_tff");
1057 resetProducerProperty("force_tff");
1058 } else setProducerProperty("force_tff", value.toInt());
1059 } else if (key == "threads") {
1060 if (value.isEmpty()) {
1061 m_properties.remove("threads");
1062 setProducerProperty("threads", 1);
1063 } else setProducerProperty("threads", value.toInt());
1064 } else if (key == "video_index") {
1065 if (value.isEmpty()) {
1066 m_properties.remove("video_index");
1067 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1068 } else setProducerProperty("video_index", value.toInt());
1069 } else if (key == "audio_index") {
1070 if (value.isEmpty()) {
1071 m_properties.remove("audio_index");
1072 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1073 } else setProducerProperty("audio_index", value.toInt());
1074 } else if (key == "force_colorspace") {
1075 if (value.isEmpty()) {
1076 m_properties.remove("force_colorspace");
1077 resetProducerProperty("force_colorspace");
1078 } else setProducerProperty("force_colorspace", value.toInt());
1079 } else if (key == "full_luma") {
1080 if (value.isEmpty()) {
1081 m_properties.remove("full_luma");
1082 resetProducerProperty("set.force_full_luma");
1083 } else setProducerProperty("set.force_full_luma", value.toInt());
1087 QMap <QString, QString> DocClipBase::properties() const
1089 return m_properties;
1092 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1094 QMap <QString, QString> currentProps;
1095 QMap<QString, QString>::const_iterator i = props.constBegin();
1096 while (i != props.constEnd()) {
1097 currentProps.insert(i.key(), m_properties.value(i.key()));
1100 return currentProps;
1103 bool DocClipBase::getAudioThumbs()
1105 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1106 if (m_audioThumbCreated) {
1109 m_audioTimer.start();
1113 bool DocClipBase::isPlaceHolder() const
1115 return m_placeHolder;
1118 void DocClipBase::addCutZone(int in, int out, QString desc)
1121 info.zone = QPoint(in, out);
1122 info.description = desc;
1123 for (int i = 0; i < m_cutZones.count(); i++)
1124 if (m_cutZones.at(i).zone == info.zone) {
1127 m_cutZones.append(info);
1130 bool DocClipBase::hasCutZone(QPoint p) const
1132 for (int i = 0; i < m_cutZones.count(); i++)
1133 if (m_cutZones.at(i).zone == p) return true;
1138 void DocClipBase::removeCutZone(int in, int out)
1141 for (int i = 0; i < m_cutZones.count(); i++) {
1142 if (m_cutZones.at(i).zone == p) {
1143 m_cutZones.removeAt(i);
1149 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1151 QPoint old(oldin, oldout);
1152 for (int i = 0; i < m_cutZones.size(); ++i) {
1153 if (m_cutZones.at(i).zone == old) {
1155 info.zone = QPoint(in, out);
1156 info.description = desc;
1157 m_cutZones.replace(i, info);
1163 QList <CutZoneInfo> DocClipBase::cutZones() const
1168 bool DocClipBase::hasVideoCodec(const QString &codec) const
1170 Mlt::Producer *prod = NULL;
1171 if (m_baseTrackProducers.count() == 0) return false;
1172 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1173 if (m_baseTrackProducers.at(i) != NULL) {
1174 prod = m_baseTrackProducers.at(i);
1179 if (!prod) return false;
1180 int default_video = prod->get_int("video_index");
1182 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1183 return prod->get(property) == codec;
1186 bool DocClipBase::hasAudioCodec(const QString &codec) const
1188 Mlt::Producer *prod = NULL;
1189 if (m_baseTrackProducers.count() == 0) return false;
1190 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1191 if (m_baseTrackProducers.at(i) != NULL) {
1192 prod = m_baseTrackProducers.at(i);
1196 if (!prod) return false;
1197 int default_video = prod->get_int("audio_index");
1199 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1200 return prod->get(property) == codec;
1204 void DocClipBase::slotExtractImage(QList <int> frames)
1206 if (m_thumbProd == NULL) return;
1207 m_thumbProd->extractImage(frames);
1210 QImage DocClipBase::extractImage(int frame, int width, int height)
1212 if (m_thumbProd == NULL) return QImage();
1213 QMutexLocker locker(&m_producerMutex);
1214 return m_thumbProd->extractImage(frame, width, height);