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()
102 qDeleteAll(m_toDeleteProducers);
103 m_toDeleteProducers.clear();
104 qDeleteAll(m_baseTrackProducers);
105 m_baseTrackProducers.clear();
106 qDeleteAll(m_audioTrackProducers);
107 m_audioTrackProducers.clear();
108 delete m_videoOnlyProducer;
109 m_videoOnlyProducer = NULL;
112 void DocClipBase::setZone(QPoint zone)
114 m_properties.insert("zone_in", QString::number(zone.x()));
115 m_properties.insert("zone_out", QString::number(zone.y()));
118 QPoint DocClipBase::zone() const
120 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
125 bool DocClipBase::hasAudioThumb() const
127 if (m_clipType == AUDIO || m_clipType == AV || m_clipType == PLAYLIST) return true;
131 void DocClipBase::slotClearAudioCache()
133 m_audioFrameCache.clear();
134 m_audioThumbCreated = false;
137 /*void DocClipBase::getClipMainThumb() {
138 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
141 KThumb *DocClipBase::thumbProducer()
146 bool DocClipBase::audioThumbCreated() const
148 return m_audioThumbCreated;
151 const QString DocClipBase::name() const
154 return m_properties.value("name");
157 const QString &DocClipBase::getId() const
162 const CLIPTYPE & DocClipBase::clipType() const
167 void DocClipBase::setClipType(CLIPTYPE type)
170 m_properties.insert("type", QString::number((int) type));
173 KUrl DocClipBase::fileURL() const
175 QString res = m_properties.value("resource");
176 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
180 void DocClipBase::setClipThumbFrame(const uint &ix)
182 m_properties.insert("thumbnail", QString::number((int) ix));
185 uint DocClipBase::getClipThumbFrame() const
187 return (uint) m_properties.value("thumbnail").toInt();
190 const QString DocClipBase::description() const
192 return m_properties.value("description");
195 bool DocClipBase::isTransparent() const
197 return (m_properties.value("transparency") == "1");
200 const QString DocClipBase::getProperty(const QString &prop) const
202 return m_properties.value(prop);
205 void DocClipBase::setDuration(GenTime dur)
208 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
211 const GenTime &DocClipBase::duration() const
216 const GenTime DocClipBase::maxDuration() const
218 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
219 /*const GenTime dur(15000, KdenliveSettings::project_fps());
226 bool DocClipBase::hasFileSize() const
231 qulonglong DocClipBase::fileSize() const
233 return m_properties.value("file_size").toULongLong();
237 QDomElement DocClipBase::toXML() const
240 QDomElement clip = doc.createElement("producer");
242 QMapIterator<QString, QString> i(m_properties);
243 while (i.hasNext()) {
245 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
247 doc.appendChild(clip);
248 if (!m_cutZones.isEmpty()) {
250 for (int i = 0; i < m_cutZones.size(); i++) {
251 CutZoneInfo info = m_cutZones.at(i);
252 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
254 clip.setAttribute("cutzones", cuts.join(";"));
256 //kDebug() << "/// CLIP XML: " << doc.toString();
257 return doc.documentElement();
261 void DocClipBase::setAudioThumbCreated(bool isDone)
263 m_audioThumbCreated = isDone;
266 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
268 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
269 m_audioFrameCache = data;
270 m_audioThumbCreated = true;
274 QList < GenTime > DocClipBase::snapMarkers() const
276 QList < GenTime > markers;
278 for (int count = 0; count < m_snapMarkers.count(); ++count) {
279 markers.append(m_snapMarkers.at(count).time());
285 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
287 return m_snapMarkers;
291 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
293 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
294 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
295 if ((*it).time() >= time)
299 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
300 (*it).setComment(comment);
301 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
303 CommentedTime t(time, comment);
304 m_snapMarkers.insert(it, t);
309 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
311 QList < CommentedTime >::Iterator it;
312 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
313 if ((*it).time() == time)
316 if (it != m_snapMarkers.end()) {
317 (*it).setComment(comment);
319 kError() << "trying to edit Snap Marker that does not already exists";
323 QString DocClipBase::deleteSnapMarker(const GenTime & time)
325 QString result = i18n("Marker");
326 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
328 while (itt != m_snapMarkers.end()) {
329 if ((*itt).time() == time)
334 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
335 result = (*itt).comment();
336 m_snapMarkers.erase(itt);
342 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
344 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
346 while (itt != m_snapMarkers.end()) {
347 if ((*itt).time() == time)
355 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
358 for (it = 0; it < m_snapMarkers.count(); it++) {
359 if (m_snapMarkers.at(it).time() >= currTime)
362 if (it == 0) return GenTime();
363 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
364 return m_snapMarkers.at(it).time();
365 else return m_snapMarkers.at(it - 1).time();
368 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
371 for (it = 0; it < m_snapMarkers.count(); it++) {
372 if (m_snapMarkers.at(it).time() > currTime)
375 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
379 QString DocClipBase::markerComment(GenTime t)
381 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
383 while (itt != m_snapMarkers.end()) {
384 if ((*itt).time() == t)
385 return (*itt).comment();
391 void DocClipBase::clearThumbProducer()
393 if (m_thumbProd) m_thumbProd->clearProducer();
396 void DocClipBase::reloadThumbProducer()
398 if (m_thumbProd && !m_thumbProd->hasProducer())
399 m_thumbProd->setProducer(getProducer());
402 void DocClipBase::deleteProducers()
404 if (m_thumbProd) m_thumbProd->clearProducer();
406 if (numReferences() > 0) {
407 // Clip is used in timeline, delay producers deletion
408 if (m_videoOnlyProducer) m_toDeleteProducers.append(m_videoOnlyProducer);
409 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
410 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
412 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
413 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
417 delete m_videoOnlyProducer;
418 qDeleteAll(m_baseTrackProducers);
419 qDeleteAll(m_audioTrackProducers);
420 m_replaceMutex.unlock();
422 m_videoOnlyProducer = NULL;
423 m_baseTrackProducers.clear();
424 m_audioTrackProducers.clear();
427 void DocClipBase::cleanupProducers()
431 kDebug()<<"----------------------------------------------------------------------------------";
432 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
433 if (m_toDeleteProducers.at(i) != NULL) {
434 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
435 if (props.ref_count() > 2) {
436 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
443 qDeleteAll(m_toDeleteProducers);
444 m_toDeleteProducers.clear();
445 m_replaceMutex.unlock();
448 bool DocClipBase::isClean() const
450 return m_toDeleteProducers.isEmpty();
453 void DocClipBase::setValid()
455 m_placeHolder = false;
458 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
460 if (producer == NULL) return;
462 QMutexLocker locker(&m_producerMutex);
463 m_replaceMutex.lock();
466 QString id = producer->get("id");
467 if (m_placeHolder || !producer->is_valid()) {
468 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
469 producer->set("markup", tmp);
470 producer->set("bgcolour", "0xff0000ff");
471 producer->set("pad", "10");
474 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
475 if (m_clipType != AUDIO) {
476 if (!id.endsWith("_audio"))
477 m_thumbProd->setProducer(producer);
479 else m_thumbProd->setProducer(producer);
482 bool updated = false;
483 if (id.contains('_')) {
484 // this is a subtrack producer, insert it at correct place
485 id = id.section('_', 1);
486 if (id.endsWith("audio")) {
487 int pos = id.section('_', 0, 0).toInt();
488 if (pos >= m_audioTrackProducers.count()) {
489 while (m_audioTrackProducers.count() - 1 < pos) {
490 m_audioTrackProducers.append(NULL);
493 if (m_audioTrackProducers.at(pos) == NULL) {
494 m_audioTrackProducers[pos] = producer;
497 else delete producer;
499 } else if (id.endsWith("video")) {
500 if (m_videoOnlyProducer == NULL) {
501 m_videoOnlyProducer = producer;
504 else delete producer;
507 int pos = id.toInt();
508 if (pos >= m_baseTrackProducers.count()) {
509 while (m_baseTrackProducers.count() - 1 < pos) {
510 m_baseTrackProducers.append(NULL);
513 if (m_baseTrackProducers.at(pos) == NULL) {
514 m_baseTrackProducers[pos] = producer;
517 else delete producer;
519 if (m_baseTrackProducers.isEmpty()) {
520 m_baseTrackProducers.append(producer);
523 else if (m_baseTrackProducers.at(0) == NULL) {
524 m_baseTrackProducers[0] = producer;
527 else delete producer;
529 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
530 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
533 static double getPixelAspect(QMap<QString, QString>& props) {
534 int width = props.value("frame_size").section('x', 0, 0).toInt();
535 int height = props.value("frame_size").section('x', 1, 1).toInt();
536 int aspectNumerator = props.value("force_aspect_num").toInt();
537 int aspectDenominator = props.value("force_aspect_den").toInt();
538 if (aspectDenominator != 0 && width != 0)
539 return double(height) * aspectNumerator / aspectDenominator / width;
544 Mlt::Producer *DocClipBase::audioProducer(int track)
546 QMutexLocker locker(&m_producerMutex);
547 if (m_audioTrackProducers.count() <= track) {
548 while (m_audioTrackProducers.count() - 1 < track) {
549 m_audioTrackProducers.append(NULL);
552 if (m_audioTrackProducers.at(track) == NULL) {
554 for (i = 0; i < m_audioTrackProducers.count(); i++)
555 if (m_audioTrackProducers.at(i) != NULL) break;
557 if (i >= m_audioTrackProducers.count()) {
558 // Could not find a valid producer for that clip
560 base = getProducer();
566 else base = m_audioTrackProducers.at(i);
567 m_audioTrackProducers[track] = cloneProducer(base);
568 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
570 return m_audioTrackProducers.at(track);
574 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
576 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
577 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
578 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
579 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
580 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
581 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
582 if (mute) prod->set("audio_index", -1);
583 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
584 if (blind) prod->set("video_index", -1);
585 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
586 prod->set("id", id.toUtf8().constData());
587 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
588 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
589 if (m_properties.contains("proxy_out")) {
590 // We have a proxy clip, make sure the proxy has same duration as original
591 prod->set("length", m_properties.value("duration").toInt());
592 prod->set("out", m_properties.value("proxy_out").toInt());
597 Mlt::Producer *DocClipBase::videoProducer()
599 QMutexLocker locker(&m_producerMutex);
600 if (m_videoOnlyProducer == NULL) {
602 for (i = 0; i < m_baseTrackProducers.count(); i++)
603 if (m_baseTrackProducers.at(i) != NULL) break;
604 if (i >= m_baseTrackProducers.count()) return NULL;
605 m_videoOnlyProducer = cloneProducer(m_baseTrackProducers.at(i));
606 adjustProducerProperties(m_videoOnlyProducer, QString(getId() + "_video"), true, false);
608 return m_videoOnlyProducer;
611 Mlt::Producer *DocClipBase::getCloneProducer()
613 Mlt::Producer *source = NULL;
614 Mlt::Producer *prod = NULL;
615 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
616 source = getProducer();
617 if (!source) return NULL;
619 if (m_clipType == COLOR) {
620 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
621 } else if (m_clipType == TEXT) {
622 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
623 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
624 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
628 QMutexLocker locker(&m_producerMutex);
629 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
630 if (m_baseTrackProducers.at(i) != NULL) {
631 source = m_baseTrackProducers.at(i);
635 if (!source) return NULL;
637 prod = cloneProducer(source);
640 adjustProducerProperties(prod, getId() + "_", false, false);
641 if (!m_properties.contains("proxy_out")) {
642 // Adjust length in case...
643 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
644 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
650 Mlt::Producer *DocClipBase::getProducer(int track)
652 QMutexLocker locker(&m_producerMutex);
653 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
654 if (m_baseTrackProducers.count() == 0) {
657 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
658 if (m_baseTrackProducers.at(i) != NULL) {
659 return m_baseTrackProducers.at(i);
664 if (track >= m_baseTrackProducers.count()) {
665 while (m_baseTrackProducers.count() - 1 < track) {
666 m_baseTrackProducers.append(NULL);
669 if (m_baseTrackProducers.at(track) == NULL) {
671 for (i = 0; i < m_baseTrackProducers.count(); i++)
672 if (m_baseTrackProducers.at(i) != NULL) break;
674 if (i >= m_baseTrackProducers.count()) {
675 // Could not find a valid producer for that clip, check in
678 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
679 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
680 m_baseTrackProducers[track] = prod;
682 return m_baseTrackProducers.at(track);
686 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
688 Mlt::Producer *result = NULL;
689 QString url = QString::fromUtf8(source->get("resource"));
690 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
691 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
693 if (result == NULL || !result->is_valid()) {
695 QString txt = "+" + i18n("Missing clip") + ".txt";
696 char *tmp = qstrdup(txt.toUtf8().constData());
697 result = new Mlt::Producer(*source->profile(), tmp);
699 if (result == NULL || !result->is_valid())
700 result = new Mlt::Producer(*(source->profile()), "colour:red");
702 result->set("bgcolour", "0xff0000ff");
703 result->set("pad", "10");
707 /*Mlt::Properties src_props(source->get_properties());
708 Mlt::Properties props(result->get_properties());
709 props.inherit(src_props);*/
713 void DocClipBase::setProducerProperty(const char *name, int data)
715 QMutexLocker locker(&m_producerMutex);
716 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
717 if (m_baseTrackProducers.at(i) != NULL)
718 m_baseTrackProducers[i]->set(name, data);
722 void DocClipBase::setProducerProperty(const char *name, double data)
724 QMutexLocker locker(&m_producerMutex);
725 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
726 if (m_baseTrackProducers.at(i) != NULL)
727 m_baseTrackProducers[i]->set(name, data);
731 void DocClipBase::setProducerProperty(const char *name, const char *data)
733 QMutexLocker locker(&m_producerMutex);
734 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
735 if (m_baseTrackProducers.at(i) != NULL)
736 m_baseTrackProducers[i]->set(name, data);
740 void DocClipBase::resetProducerProperty(const char *name)
742 QMutexLocker locker(&m_producerMutex);
743 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
744 if (m_baseTrackProducers.at(i) != NULL)
745 m_baseTrackProducers[i]->set(name, (const char*) NULL);
749 const char *DocClipBase::producerProperty(const char *name) const
751 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
752 if (m_baseTrackProducers.at(i) != NULL) {
753 return m_baseTrackProducers.at(i)->get(name);
760 void DocClipBase::slotRefreshProducer()
762 if (m_baseTrackProducers.count() == 0) return;
763 if (m_clipType == SLIDESHOW) {
764 setProducerProperty("ttl", getProperty("ttl").toInt());
765 //m_clipProducer->set("id", getProperty("id"));
766 if (!getProperty("animation").isEmpty()) {
767 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
769 Mlt::Filter *filter = clipService.filter(ct);
771 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
773 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
774 clipService.detach(*filter);
776 filter = clipService.filter(ct);
779 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
780 // filter does not exist, create it.
781 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
782 if (filter && filter->is_valid()) {
783 int cycle = getProperty("ttl").toInt();
784 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
785 if (!geometry.isEmpty()) {
786 if (getProperty("animation").contains("low-pass")) {
787 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
788 if (blur && blur->is_valid())
789 clipService.attach(*blur);
791 filter->set("transition.geometry", geometry.toUtf8().data());
792 filter->set("transition.cycle", cycle);
793 clipService.attach(*filter);
798 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
800 Mlt::Filter *filter = clipService.filter(0);
802 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
803 clipService.detach(*filter);
805 filter = clipService.filter(ct);
808 if (getProperty("fade") == "1") {
809 // we want a fade filter effect
810 kDebug() << "//////////// FADE WANTED";
811 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
813 Mlt::Filter *filter = clipService.filter(ct);
815 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
819 filter = clipService.filter(ct);
822 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
823 filter->set("cycle", getProperty("ttl").toInt());
824 filter->set("duration", getProperty("luma_duration").toInt());
825 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
826 if (!getProperty("softness").isEmpty()) {
827 int soft = getProperty("softness").toInt();
828 filter->set("luma.softness", (double) soft / 100.0);
831 // filter does not exist, create it...
832 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
833 filter->set("cycle", getProperty("ttl").toInt());
834 filter->set("duration", getProperty("luma_duration").toInt());
835 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
836 if (!getProperty("softness").isEmpty()) {
837 int soft = getProperty("softness").toInt();
838 filter->set("luma.softness", (double) soft / 100.0);
840 clipService.attach(*filter);
843 kDebug() << "//////////// FADE NOT WANTED!!!";
844 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
846 Mlt::Filter *filter = clipService.filter(0);
848 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
849 clipService.detach(*filter);
851 filter = clipService.filter(ct);
854 if (getProperty("crop") == "1") {
855 // we want a center crop filter effect
856 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
858 Mlt::Filter *filter = clipService.filter(ct);
860 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
864 filter = clipService.filter(ct);
867 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
868 // filter does not exist, create it...
869 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
870 filter->set("center", 1);
871 clipService.attach(*filter);
874 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
876 Mlt::Filter *filter = clipService.filter(0);
878 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
879 clipService.detach(*filter);
881 filter = clipService.filter(ct);
887 void DocClipBase::setProperties(QMap <QString, QString> properties)
889 // changing clip type is not allowed
890 properties.remove("type");
891 QMapIterator<QString, QString> i(properties);
892 bool refreshProducer = false;
894 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
895 QString oldProxy = m_properties.value("proxy");
896 while (i.hasNext()) {
898 setProperty(i.key(), i.value());
899 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
901 if (properties.contains("proxy")) {
902 QString value = properties.value("proxy");
903 // If value is "-", that means user manually disabled proxy on this clip
904 if (value.isEmpty() || value == "-") {
906 emit abortProxy(m_id, oldProxy);
909 emit createProxy(m_id);
912 if (refreshProducer) slotRefreshProducer();
915 void DocClipBase::setMetadata(QMap <QString, QString> properties)
917 QMapIterator<QString, QString> i(properties);
918 while (i.hasNext()) {
920 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
921 m_metadata.remove(i.key());
923 m_metadata.insert(i.key(), i.value());
928 QMap <QString, QString> DocClipBase::metadata() const
933 void DocClipBase::clearProperty(const QString &key)
935 m_properties.remove(key);
938 void DocClipBase::getFileHash(const QString &url)
940 if (m_clipType == SLIDESHOW) return;
942 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
945 //kDebug() << "SETTING HASH of" << value;
946 m_properties.insert("file_size", QString::number(file.size()));
948 * 1 MB = 1 second per 450 files (or faster)
949 * 10 MB = 9 seconds per 450 files (or faster)
951 if (file.size() > 1000000*2) {
952 fileData = file.read(1000000);
953 if (file.seek(file.size() - 1000000))
954 fileData.append(file.readAll());
956 fileData = file.readAll();
958 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
959 m_properties.insert("file_hash", QString(fileHash.toHex()));
963 bool DocClipBase::checkHash() const
965 KUrl url = fileURL();
966 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
970 QString DocClipBase::getClipHash() const
973 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
974 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
975 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
977 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
978 if (hash.isEmpty()) hash = getHash(fileURL().path());
984 void DocClipBase::setPlaceHolder(bool place)
986 m_placeHolder = place;
990 QString DocClipBase::getHash(const QString &path)
993 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
997 * 1 MB = 1 second per 450 files (or faster)
998 * 10 MB = 9 seconds per 450 files (or faster)
1000 if (file.size() > 1000000*2) {
1001 fileData = file.read(1000000);
1002 if (file.seek(file.size() - 1000000))
1003 fileData.append(file.readAll());
1005 fileData = file.readAll();
1007 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1012 void DocClipBase::refreshThumbUrl()
1014 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1017 void DocClipBase::setProperty(const QString &key, const QString &value)
1019 m_properties.insert(key, value);
1020 if (key == "resource") {
1022 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1023 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1024 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1025 else if (key == "colour") {
1026 setProducerProperty("colour", value.toUtf8().data());
1027 } else if (key == "templatetext") {
1028 setProducerProperty("templatetext", value.toUtf8().data());
1029 setProducerProperty("force_reload", 1);
1030 } else if (key == "xmldata") {
1031 setProducerProperty("xmldata", value.toUtf8().data());
1032 setProducerProperty("force_reload", 1);
1033 } else if (key == "force_aspect_num") {
1034 if (value.isEmpty()) {
1035 m_properties.remove("force_aspect_num");
1036 resetProducerProperty("force_aspect_ratio");
1037 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1038 } else if (key == "force_aspect_den") {
1039 if (value.isEmpty()) {
1040 m_properties.remove("force_aspect_den");
1041 resetProducerProperty("force_aspect_ratio");
1042 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1043 } else if (key == "force_fps") {
1044 if (value.isEmpty()) {
1045 m_properties.remove("force_fps");
1046 resetProducerProperty("force_fps");
1047 } else setProducerProperty("force_fps", value.toDouble());
1048 } else if (key == "force_progressive") {
1049 if (value.isEmpty()) {
1050 m_properties.remove("force_progressive");
1051 resetProducerProperty("force_progressive");
1052 } else setProducerProperty("force_progressive", value.toInt());
1053 } else if (key == "force_tff") {
1054 if (value.isEmpty()) {
1055 m_properties.remove("force_tff");
1056 resetProducerProperty("force_tff");
1057 } else setProducerProperty("force_tff", value.toInt());
1058 } else if (key == "threads") {
1059 if (value.isEmpty()) {
1060 m_properties.remove("threads");
1061 setProducerProperty("threads", 1);
1062 } else setProducerProperty("threads", value.toInt());
1063 } else if (key == "video_index") {
1064 if (value.isEmpty()) {
1065 m_properties.remove("video_index");
1066 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1067 } else setProducerProperty("video_index", value.toInt());
1068 } else if (key == "audio_index") {
1069 if (value.isEmpty()) {
1070 m_properties.remove("audio_index");
1071 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1072 } else setProducerProperty("audio_index", value.toInt());
1073 } else if (key == "force_colorspace") {
1074 if (value.isEmpty()) {
1075 m_properties.remove("force_colorspace");
1076 resetProducerProperty("force_colorspace");
1077 } else setProducerProperty("force_colorspace", value.toInt());
1078 } else if (key == "full_luma") {
1079 if (value.isEmpty()) {
1080 m_properties.remove("full_luma");
1081 resetProducerProperty("set.force_full_luma");
1082 } else setProducerProperty("set.force_full_luma", value.toInt());
1086 QMap <QString, QString> DocClipBase::properties() const
1088 return m_properties;
1091 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1093 QMap <QString, QString> currentProps;
1094 QMap<QString, QString>::const_iterator i = props.constBegin();
1095 while (i != props.constEnd()) {
1096 currentProps.insert(i.key(), m_properties.value(i.key()));
1099 return currentProps;
1102 bool DocClipBase::getAudioThumbs()
1104 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1105 if (m_audioThumbCreated) {
1108 m_audioTimer.start();
1112 bool DocClipBase::isPlaceHolder() const
1114 return m_placeHolder;
1117 void DocClipBase::addCutZone(int in, int out, QString desc)
1120 info.zone = QPoint(in, out);
1121 info.description = desc;
1122 for (int i = 0; i < m_cutZones.count(); i++)
1123 if (m_cutZones.at(i).zone == info.zone) {
1126 m_cutZones.append(info);
1129 bool DocClipBase::hasCutZone(QPoint p) const
1131 for (int i = 0; i < m_cutZones.count(); i++)
1132 if (m_cutZones.at(i).zone == p) return true;
1137 void DocClipBase::removeCutZone(int in, int out)
1140 for (int i = 0; i < m_cutZones.count(); i++) {
1141 if (m_cutZones.at(i).zone == p) {
1142 m_cutZones.removeAt(i);
1148 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1150 QPoint old(oldin, oldout);
1151 for (int i = 0; i < m_cutZones.size(); ++i) {
1152 if (m_cutZones.at(i).zone == old) {
1154 info.zone = QPoint(in, out);
1155 info.description = desc;
1156 m_cutZones.replace(i, info);
1162 QList <CutZoneInfo> DocClipBase::cutZones() const
1167 bool DocClipBase::hasVideoCodec(const QString &codec) const
1169 Mlt::Producer *prod = NULL;
1170 if (m_baseTrackProducers.count() == 0) return false;
1171 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1172 if (m_baseTrackProducers.at(i) != NULL) {
1173 prod = m_baseTrackProducers.at(i);
1178 if (!prod) return false;
1179 int default_video = prod->get_int("video_index");
1181 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1182 return prod->get(property) == codec;
1185 bool DocClipBase::hasAudioCodec(const QString &codec) const
1187 Mlt::Producer *prod = NULL;
1188 if (m_baseTrackProducers.count() == 0) return false;
1189 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1190 if (m_baseTrackProducers.at(i) != NULL) {
1191 prod = m_baseTrackProducers.at(i);
1195 if (!prod) return false;
1196 int default_video = prod->get_int("audio_index");
1198 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1199 return prod->get(property) == codec;
1203 void DocClipBase::slotExtractImage(QList <int> frames)
1205 if (m_thumbProd == NULL) return;
1206 m_thumbProd->extractImage(frames);
1209 QImage DocClipBase::extractImage(int frame, int width, int height)
1211 if (m_thumbProd == NULL) return QImage();
1212 QMutexLocker locker(&m_producerMutex);
1213 return m_thumbProd->extractImage(frame, width, height);