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();
445 qDeleteAll(m_toDeleteProducers);
446 m_toDeleteProducers.clear();
447 m_replaceMutex.unlock();
451 bool DocClipBase::isClean() const
453 return m_toDeleteProducers.isEmpty();
456 void DocClipBase::setValid()
458 m_placeHolder = false;
461 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
463 if (producer == NULL) return;
465 QMutexLocker locker(&m_producerMutex);
466 m_replaceMutex.lock();
469 QString id = producer->get("id");
470 if (m_placeHolder || !producer->is_valid()) {
471 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
472 producer->set("markup", tmp);
473 producer->set("bgcolour", "0xff0000ff");
474 producer->set("pad", "10");
477 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
478 if (m_clipType != AUDIO) {
479 if (!id.endsWith("_audio"))
480 m_thumbProd->setProducer(producer);
482 else m_thumbProd->setProducer(producer);
485 bool updated = false;
486 if (id.contains('_')) {
487 // this is a subtrack producer, insert it at correct place
488 id = id.section('_', 1);
489 if (id.endsWith("audio")) {
490 int pos = id.section('_', 0, 0).toInt();
491 if (pos >= m_audioTrackProducers.count()) {
492 while (m_audioTrackProducers.count() - 1 < pos) {
493 m_audioTrackProducers.append(NULL);
496 if (m_audioTrackProducers.at(pos) == NULL) {
497 m_audioTrackProducers[pos] = producer;
500 else delete producer;
502 } else if (id.endsWith("video")) {
503 if (m_videoOnlyProducer == NULL) {
504 m_videoOnlyProducer = producer;
507 else delete producer;
510 int pos = id.toInt();
511 if (pos >= m_baseTrackProducers.count()) {
512 while (m_baseTrackProducers.count() - 1 < pos) {
513 m_baseTrackProducers.append(NULL);
516 if (m_baseTrackProducers.at(pos) == NULL) {
517 m_baseTrackProducers[pos] = producer;
520 else delete producer;
522 if (m_baseTrackProducers.isEmpty()) {
523 m_baseTrackProducers.append(producer);
526 else if (m_baseTrackProducers.at(0) == NULL) {
527 m_baseTrackProducers[0] = producer;
530 else delete producer;
532 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
533 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
536 static double getPixelAspect(QMap<QString, QString>& props) {
537 int width = props.value("frame_size").section('x', 0, 0).toInt();
538 int height = props.value("frame_size").section('x', 1, 1).toInt();
539 int aspectNumerator = props.value("force_aspect_num").toInt();
540 int aspectDenominator = props.value("force_aspect_den").toInt();
541 if (aspectDenominator != 0 && width != 0)
542 return double(height) * aspectNumerator / aspectDenominator / width;
547 Mlt::Producer *DocClipBase::audioProducer(int track)
549 QMutexLocker locker(&m_producerMutex);
550 if (m_audioTrackProducers.count() <= track) {
551 while (m_audioTrackProducers.count() - 1 < track) {
552 m_audioTrackProducers.append(NULL);
555 if (m_audioTrackProducers.at(track) == NULL) {
557 for (i = 0; i < m_audioTrackProducers.count(); i++)
558 if (m_audioTrackProducers.at(i) != NULL) break;
560 if (i >= m_audioTrackProducers.count()) {
561 // Could not find a valid producer for that clip
563 base = getProducer();
569 else base = m_audioTrackProducers.at(i);
570 m_audioTrackProducers[track] = cloneProducer(base);
571 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
573 return m_audioTrackProducers.at(track);
577 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
579 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
580 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
581 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
582 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
583 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
584 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
585 if (mute) prod->set("audio_index", -1);
586 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
587 if (blind) prod->set("video_index", -1);
588 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
589 prod->set("id", id.toUtf8().constData());
590 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
591 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
592 if (m_properties.contains("proxy_out")) {
593 // We have a proxy clip, make sure the proxy has same duration as original
594 prod->set("length", m_properties.value("duration").toInt());
595 prod->set("out", m_properties.value("proxy_out").toInt());
600 Mlt::Producer *DocClipBase::videoProducer()
602 QMutexLocker locker(&m_producerMutex);
603 if (m_videoOnlyProducer == NULL) {
605 for (i = 0; i < m_baseTrackProducers.count(); i++)
606 if (m_baseTrackProducers.at(i) != NULL) break;
607 if (i >= m_baseTrackProducers.count()) return NULL;
608 m_videoOnlyProducer = cloneProducer(m_baseTrackProducers.at(i));
609 adjustProducerProperties(m_videoOnlyProducer, QString(getId() + "_video"), true, false);
611 return m_videoOnlyProducer;
614 Mlt::Producer *DocClipBase::getCloneProducer()
616 Mlt::Producer *source = NULL;
617 Mlt::Producer *prod = NULL;
618 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
619 source = getProducer();
620 if (!source) return NULL;
622 if (m_clipType == COLOR) {
623 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
624 } else if (m_clipType == TEXT) {
625 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
626 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
627 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
631 QMutexLocker locker(&m_producerMutex);
632 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
633 if (m_baseTrackProducers.at(i) != NULL) {
634 source = m_baseTrackProducers.at(i);
638 if (!source) return NULL;
640 prod = cloneProducer(source);
643 adjustProducerProperties(prod, getId() + "_", false, false);
644 if (!m_properties.contains("proxy_out")) {
645 // Adjust length in case...
646 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
647 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
653 Mlt::Producer *DocClipBase::getProducer(int track)
655 QMutexLocker locker(&m_producerMutex);
656 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
657 if (m_baseTrackProducers.count() == 0) {
660 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
661 if (m_baseTrackProducers.at(i) != NULL) {
662 return m_baseTrackProducers.at(i);
667 if (track >= m_baseTrackProducers.count()) {
668 while (m_baseTrackProducers.count() - 1 < track) {
669 m_baseTrackProducers.append(NULL);
672 if (m_baseTrackProducers.at(track) == NULL) {
674 for (i = 0; i < m_baseTrackProducers.count(); i++)
675 if (m_baseTrackProducers.at(i) != NULL) break;
677 if (i >= m_baseTrackProducers.count()) {
678 // Could not find a valid producer for that clip, check in
681 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
682 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
683 m_baseTrackProducers[track] = prod;
685 return m_baseTrackProducers.at(track);
689 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
691 Mlt::Producer *result = NULL;
692 QString url = QString::fromUtf8(source->get("resource"));
693 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
694 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
696 if (result == NULL || !result->is_valid()) {
698 QString txt = "+" + i18n("Missing clip") + ".txt";
699 char *tmp = qstrdup(txt.toUtf8().constData());
700 result = new Mlt::Producer(*source->profile(), tmp);
702 if (result == NULL || !result->is_valid())
703 result = new Mlt::Producer(*(source->profile()), "colour:red");
705 result->set("bgcolour", "0xff0000ff");
706 result->set("pad", "10");
710 /*Mlt::Properties src_props(source->get_properties());
711 Mlt::Properties props(result->get_properties());
712 props.inherit(src_props);*/
716 void DocClipBase::setProducerProperty(const char *name, int data)
718 QMutexLocker locker(&m_producerMutex);
719 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
720 if (m_baseTrackProducers.at(i) != NULL)
721 m_baseTrackProducers[i]->set(name, data);
725 void DocClipBase::setProducerProperty(const char *name, double data)
727 QMutexLocker locker(&m_producerMutex);
728 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
729 if (m_baseTrackProducers.at(i) != NULL)
730 m_baseTrackProducers[i]->set(name, data);
734 void DocClipBase::setProducerProperty(const char *name, const char *data)
736 QMutexLocker locker(&m_producerMutex);
737 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
738 if (m_baseTrackProducers.at(i) != NULL)
739 m_baseTrackProducers[i]->set(name, data);
743 void DocClipBase::resetProducerProperty(const char *name)
745 QMutexLocker locker(&m_producerMutex);
746 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
747 if (m_baseTrackProducers.at(i) != NULL)
748 m_baseTrackProducers[i]->set(name, (const char*) NULL);
752 const char *DocClipBase::producerProperty(const char *name) const
754 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
755 if (m_baseTrackProducers.at(i) != NULL) {
756 return m_baseTrackProducers.at(i)->get(name);
763 void DocClipBase::slotRefreshProducer()
765 if (m_baseTrackProducers.count() == 0) return;
766 if (m_clipType == SLIDESHOW) {
767 setProducerProperty("ttl", getProperty("ttl").toInt());
768 //m_clipProducer->set("id", getProperty("id"));
769 if (!getProperty("animation").isEmpty()) {
770 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
772 Mlt::Filter *filter = clipService.filter(ct);
774 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
776 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
777 clipService.detach(*filter);
779 filter = clipService.filter(ct);
782 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
783 // filter does not exist, create it.
784 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
785 if (filter && filter->is_valid()) {
786 int cycle = getProperty("ttl").toInt();
787 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
788 if (!geometry.isEmpty()) {
789 if (getProperty("animation").contains("low-pass")) {
790 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
791 if (blur && blur->is_valid())
792 clipService.attach(*blur);
794 filter->set("transition.geometry", geometry.toUtf8().data());
795 filter->set("transition.cycle", cycle);
796 clipService.attach(*filter);
801 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
803 Mlt::Filter *filter = clipService.filter(0);
805 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
806 clipService.detach(*filter);
808 filter = clipService.filter(ct);
811 if (getProperty("fade") == "1") {
812 // we want a fade filter effect
813 kDebug() << "//////////// FADE WANTED";
814 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
816 Mlt::Filter *filter = clipService.filter(ct);
818 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
822 filter = clipService.filter(ct);
825 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
826 filter->set("cycle", getProperty("ttl").toInt());
827 filter->set("duration", getProperty("luma_duration").toInt());
828 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
829 if (!getProperty("softness").isEmpty()) {
830 int soft = getProperty("softness").toInt();
831 filter->set("luma.softness", (double) soft / 100.0);
834 // filter does not exist, create it...
835 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
836 filter->set("cycle", getProperty("ttl").toInt());
837 filter->set("duration", getProperty("luma_duration").toInt());
838 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
839 if (!getProperty("softness").isEmpty()) {
840 int soft = getProperty("softness").toInt();
841 filter->set("luma.softness", (double) soft / 100.0);
843 clipService.attach(*filter);
846 kDebug() << "//////////// FADE NOT WANTED!!!";
847 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
849 Mlt::Filter *filter = clipService.filter(0);
851 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
852 clipService.detach(*filter);
854 filter = clipService.filter(ct);
857 if (getProperty("crop") == "1") {
858 // we want a center crop filter effect
859 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
861 Mlt::Filter *filter = clipService.filter(ct);
863 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
867 filter = clipService.filter(ct);
870 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
871 // filter does not exist, create it...
872 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
873 filter->set("center", 1);
874 clipService.attach(*filter);
877 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
879 Mlt::Filter *filter = clipService.filter(0);
881 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
882 clipService.detach(*filter);
884 filter = clipService.filter(ct);
890 void DocClipBase::setProperties(QMap <QString, QString> properties)
892 // changing clip type is not allowed
893 properties.remove("type");
894 QMapIterator<QString, QString> i(properties);
895 bool refreshProducer = false;
897 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
898 QString oldProxy = m_properties.value("proxy");
899 while (i.hasNext()) {
901 setProperty(i.key(), i.value());
902 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
904 if (properties.contains("proxy")) {
905 QString value = properties.value("proxy");
906 // If value is "-", that means user manually disabled proxy on this clip
907 if (value.isEmpty() || value == "-") {
909 emit abortProxy(m_id, oldProxy);
912 emit createProxy(m_id);
915 if (refreshProducer) slotRefreshProducer();
918 void DocClipBase::setMetadata(QMap <QString, QString> properties)
920 QMapIterator<QString, QString> i(properties);
921 while (i.hasNext()) {
923 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
924 m_metadata.remove(i.key());
926 m_metadata.insert(i.key(), i.value());
931 QMap <QString, QString> DocClipBase::metadata() const
936 void DocClipBase::clearProperty(const QString &key)
938 m_properties.remove(key);
941 void DocClipBase::getFileHash(const QString &url)
943 if (m_clipType == SLIDESHOW) return;
945 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
948 //kDebug() << "SETTING HASH of" << value;
949 m_properties.insert("file_size", QString::number(file.size()));
951 * 1 MB = 1 second per 450 files (or faster)
952 * 10 MB = 9 seconds per 450 files (or faster)
954 if (file.size() > 1000000*2) {
955 fileData = file.read(1000000);
956 if (file.seek(file.size() - 1000000))
957 fileData.append(file.readAll());
959 fileData = file.readAll();
961 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
962 m_properties.insert("file_hash", QString(fileHash.toHex()));
966 bool DocClipBase::checkHash() const
968 KUrl url = fileURL();
969 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
973 QString DocClipBase::getClipHash() const
976 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
977 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
978 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
980 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
981 if (hash.isEmpty()) hash = getHash(fileURL().path());
987 void DocClipBase::setPlaceHolder(bool place)
989 m_placeHolder = place;
993 QString DocClipBase::getHash(const QString &path)
996 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1000 * 1 MB = 1 second per 450 files (or faster)
1001 * 10 MB = 9 seconds per 450 files (or faster)
1003 if (file.size() > 1000000*2) {
1004 fileData = file.read(1000000);
1005 if (file.seek(file.size() - 1000000))
1006 fileData.append(file.readAll());
1008 fileData = file.readAll();
1010 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1015 void DocClipBase::refreshThumbUrl()
1017 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1020 void DocClipBase::setProperty(const QString &key, const QString &value)
1022 m_properties.insert(key, value);
1023 if (key == "resource") {
1025 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1026 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1027 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1028 else if (key == "colour") {
1029 setProducerProperty("colour", value.toUtf8().data());
1030 } else if (key == "templatetext") {
1031 setProducerProperty("templatetext", value.toUtf8().data());
1032 setProducerProperty("force_reload", 1);
1033 } else if (key == "xmldata") {
1034 setProducerProperty("xmldata", value.toUtf8().data());
1035 setProducerProperty("force_reload", 1);
1036 } else if (key == "force_aspect_num") {
1037 if (value.isEmpty()) {
1038 m_properties.remove("force_aspect_num");
1039 resetProducerProperty("force_aspect_ratio");
1040 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1041 } else if (key == "force_aspect_den") {
1042 if (value.isEmpty()) {
1043 m_properties.remove("force_aspect_den");
1044 resetProducerProperty("force_aspect_ratio");
1045 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1046 } else if (key == "force_fps") {
1047 if (value.isEmpty()) {
1048 m_properties.remove("force_fps");
1049 resetProducerProperty("force_fps");
1050 } else setProducerProperty("force_fps", value.toDouble());
1051 } else if (key == "force_progressive") {
1052 if (value.isEmpty()) {
1053 m_properties.remove("force_progressive");
1054 resetProducerProperty("force_progressive");
1055 } else setProducerProperty("force_progressive", value.toInt());
1056 } else if (key == "force_tff") {
1057 if (value.isEmpty()) {
1058 m_properties.remove("force_tff");
1059 resetProducerProperty("force_tff");
1060 } else setProducerProperty("force_tff", value.toInt());
1061 } else if (key == "threads") {
1062 if (value.isEmpty()) {
1063 m_properties.remove("threads");
1064 setProducerProperty("threads", 1);
1065 } else setProducerProperty("threads", value.toInt());
1066 } else if (key == "video_index") {
1067 if (value.isEmpty()) {
1068 m_properties.remove("video_index");
1069 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1070 } else setProducerProperty("video_index", value.toInt());
1071 } else if (key == "audio_index") {
1072 if (value.isEmpty()) {
1073 m_properties.remove("audio_index");
1074 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1075 } else setProducerProperty("audio_index", value.toInt());
1076 } else if (key == "force_colorspace") {
1077 if (value.isEmpty()) {
1078 m_properties.remove("force_colorspace");
1079 resetProducerProperty("force_colorspace");
1080 } else setProducerProperty("force_colorspace", value.toInt());
1081 } else if (key == "full_luma") {
1082 if (value.isEmpty()) {
1083 m_properties.remove("full_luma");
1084 resetProducerProperty("set.force_full_luma");
1085 } else setProducerProperty("set.force_full_luma", value.toInt());
1089 QMap <QString, QString> DocClipBase::properties() const
1091 return m_properties;
1094 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1096 QMap <QString, QString> currentProps;
1097 QMap<QString, QString>::const_iterator i = props.constBegin();
1098 while (i != props.constEnd()) {
1099 currentProps.insert(i.key(), m_properties.value(i.key()));
1102 return currentProps;
1105 bool DocClipBase::getAudioThumbs()
1107 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1108 if (m_audioThumbCreated) {
1111 m_audioTimer.start();
1115 bool DocClipBase::isPlaceHolder() const
1117 return m_placeHolder;
1120 void DocClipBase::addCutZone(int in, int out, QString desc)
1123 info.zone = QPoint(in, out);
1124 info.description = desc;
1125 for (int i = 0; i < m_cutZones.count(); i++)
1126 if (m_cutZones.at(i).zone == info.zone) {
1129 m_cutZones.append(info);
1132 bool DocClipBase::hasCutZone(QPoint p) const
1134 for (int i = 0; i < m_cutZones.count(); i++)
1135 if (m_cutZones.at(i).zone == p) return true;
1140 void DocClipBase::removeCutZone(int in, int out)
1143 for (int i = 0; i < m_cutZones.count(); i++) {
1144 if (m_cutZones.at(i).zone == p) {
1145 m_cutZones.removeAt(i);
1151 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1153 QPoint old(oldin, oldout);
1154 for (int i = 0; i < m_cutZones.size(); ++i) {
1155 if (m_cutZones.at(i).zone == old) {
1157 info.zone = QPoint(in, out);
1158 info.description = desc;
1159 m_cutZones.replace(i, info);
1165 QList <CutZoneInfo> DocClipBase::cutZones() const
1170 bool DocClipBase::hasVideoCodec(const QString &codec) const
1172 Mlt::Producer *prod = NULL;
1173 if (m_baseTrackProducers.count() == 0) return false;
1174 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1175 if (m_baseTrackProducers.at(i) != NULL) {
1176 prod = m_baseTrackProducers.at(i);
1181 if (!prod) return false;
1182 int default_video = prod->get_int("video_index");
1184 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1185 return prod->get(property) == codec;
1188 bool DocClipBase::hasAudioCodec(const QString &codec) const
1190 Mlt::Producer *prod = NULL;
1191 if (m_baseTrackProducers.count() == 0) return false;
1192 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1193 if (m_baseTrackProducers.at(i) != NULL) {
1194 prod = m_baseTrackProducers.at(i);
1198 if (!prod) return false;
1199 int default_video = prod->get_int("audio_index");
1201 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1202 return prod->get(property) == codec;
1206 void DocClipBase::slotExtractImage(QList <int> frames)
1208 if (m_thumbProd == NULL) return;
1209 m_thumbProd->extractImage(frames);
1212 QImage DocClipBase::extractImage(int frame, int width, int height)
1214 if (m_thumbProd == NULL) return QImage();
1215 QMutexLocker locker(&m_producerMutex);
1216 return m_thumbProd->extractImage(frame, width, height);