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"));
92 DocClipBase::~DocClipBase()
95 qDeleteAll(m_toDeleteProducers);
96 m_toDeleteProducers.clear();
97 qDeleteAll(m_baseTrackProducers);
98 m_baseTrackProducers.clear();
99 qDeleteAll(m_audioTrackProducers);
100 m_audioTrackProducers.clear();
101 delete m_videoOnlyProducer;
102 m_videoOnlyProducer = NULL;
105 void DocClipBase::setZone(QPoint zone)
107 m_properties.insert("zone_in", QString::number(zone.x()));
108 m_properties.insert("zone_out", QString::number(zone.y()));
111 QPoint DocClipBase::zone() const
113 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
118 bool DocClipBase::hasAudioThumb() const
120 if (m_clipType == AUDIO || m_clipType == AV || m_clipType == PLAYLIST) return true;
124 void DocClipBase::slotClearAudioCache()
126 m_audioFrameCache.clear();
127 m_audioThumbCreated = false;
130 /*void DocClipBase::getClipMainThumb() {
131 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
134 KThumb *DocClipBase::thumbProducer()
139 bool DocClipBase::audioThumbCreated() const
141 return m_audioThumbCreated;
144 const QString DocClipBase::name() const
147 return m_properties.value("name");
150 const QString &DocClipBase::getId() const
155 const CLIPTYPE & DocClipBase::clipType() const
160 void DocClipBase::setClipType(CLIPTYPE type)
163 m_properties.insert("type", QString::number((int) type));
166 KUrl DocClipBase::fileURL() const
168 QString res = m_properties.value("resource");
169 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
173 void DocClipBase::setClipThumbFrame(const uint &ix)
175 m_properties.insert("thumbnail", QString::number((int) ix));
178 uint DocClipBase::getClipThumbFrame() const
180 return (uint) m_properties.value("thumbnail").toInt();
183 const QString DocClipBase::description() const
185 return m_properties.value("description");
188 bool DocClipBase::isTransparent() const
190 return (m_properties.value("transparency") == "1");
193 const QString DocClipBase::getProperty(const QString &prop) const
195 return m_properties.value(prop);
198 void DocClipBase::setDuration(GenTime dur)
201 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
204 const GenTime &DocClipBase::duration() const
209 const GenTime DocClipBase::maxDuration() const
211 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
212 /*const GenTime dur(15000, KdenliveSettings::project_fps());
219 bool DocClipBase::hasFileSize() const
224 qulonglong DocClipBase::fileSize() const
226 return m_properties.value("file_size").toULongLong();
230 QDomElement DocClipBase::toXML() const
233 QDomElement clip = doc.createElement("producer");
235 QMapIterator<QString, QString> i(m_properties);
236 while (i.hasNext()) {
238 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
240 doc.appendChild(clip);
241 if (!m_cutZones.isEmpty()) {
243 for (int i = 0; i < m_cutZones.size(); i++) {
244 CutZoneInfo info = m_cutZones.at(i);
245 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
247 clip.setAttribute("cutzones", cuts.join(";"));
249 //kDebug() << "/// CLIP XML: " << doc.toString();
250 return doc.documentElement();
254 void DocClipBase::setAudioThumbCreated(bool isDone)
256 m_audioThumbCreated = isDone;
259 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
261 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
262 m_audioFrameCache = data;
263 m_audioThumbCreated = true;
267 QList < GenTime > DocClipBase::snapMarkers() const
269 QList < GenTime > markers;
271 for (int count = 0; count < m_snapMarkers.count(); ++count) {
272 markers.append(m_snapMarkers.at(count).time());
278 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
280 return m_snapMarkers;
284 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
286 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
287 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
288 if ((*it).time() >= time)
292 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
293 (*it).setComment(comment);
294 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
296 CommentedTime t(time, comment);
297 m_snapMarkers.insert(it, t);
302 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
304 QList < CommentedTime >::Iterator it;
305 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
306 if ((*it).time() == time)
309 if (it != m_snapMarkers.end()) {
310 (*it).setComment(comment);
312 kError() << "trying to edit Snap Marker that does not already exists";
316 QString DocClipBase::deleteSnapMarker(const GenTime & time)
318 QString result = i18n("Marker");
319 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
321 while (itt != m_snapMarkers.end()) {
322 if ((*itt).time() == time)
327 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
328 result = (*itt).comment();
329 m_snapMarkers.erase(itt);
335 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
337 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
339 while (itt != m_snapMarkers.end()) {
340 if ((*itt).time() == time)
348 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
351 for (it = 0; it < m_snapMarkers.count(); it++) {
352 if (m_snapMarkers.at(it).time() >= currTime)
355 if (it == 0) return GenTime();
356 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
357 return m_snapMarkers.at(it).time();
358 else return m_snapMarkers.at(it - 1).time();
361 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
364 for (it = 0; it < m_snapMarkers.count(); it++) {
365 if (m_snapMarkers.at(it).time() > currTime)
368 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
372 QString DocClipBase::markerComment(GenTime t)
374 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
376 while (itt != m_snapMarkers.end()) {
377 if ((*itt).time() == t)
378 return (*itt).comment();
384 void DocClipBase::clearThumbProducer()
386 if (m_thumbProd) m_thumbProd->clearProducer();
389 void DocClipBase::reloadThumbProducer()
391 if (m_thumbProd && !m_thumbProd->hasProducer())
392 m_thumbProd->setProducer(getProducer());
395 void DocClipBase::deleteProducers()
397 if (m_thumbProd) m_thumbProd->clearProducer();
399 if (numReferences() > 0) {
400 // Clip is used in timeline, delay producers deletion
401 if (m_videoOnlyProducer) m_toDeleteProducers.append(m_videoOnlyProducer);
402 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
403 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
405 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
406 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
410 delete m_videoOnlyProducer;
411 qDeleteAll(m_baseTrackProducers);
412 qDeleteAll(m_audioTrackProducers);
413 m_replaceMutex.unlock();
415 m_videoOnlyProducer = NULL;
416 m_baseTrackProducers.clear();
417 m_audioTrackProducers.clear();
420 void DocClipBase::cleanupProducers()
424 kDebug()<<"----------------------------------------------------------------------------------";
425 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
426 if (m_toDeleteProducers.at(i) != NULL) {
427 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
428 if (props.ref_count() > 2) {
429 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
436 qDeleteAll(m_toDeleteProducers);
437 m_toDeleteProducers.clear();
438 m_replaceMutex.unlock();
441 bool DocClipBase::isClean() const
443 return m_toDeleteProducers.isEmpty();
446 void DocClipBase::setValid()
448 m_placeHolder = false;
451 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
453 if (producer == NULL) return;
455 QMutexLocker locker(&m_producerMutex);
456 m_replaceMutex.lock();
459 QString id = producer->get("id");
460 if (m_placeHolder || !producer->is_valid()) {
461 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
462 producer->set("markup", tmp);
463 producer->set("bgcolour", "0xff0000ff");
464 producer->set("pad", "10");
467 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
468 if (m_clipType != AUDIO) {
469 if (!id.endsWith("_audio"))
470 m_thumbProd->setProducer(producer);
472 else m_thumbProd->setProducer(producer);
475 bool updated = false;
476 if (id.contains('_')) {
477 // this is a subtrack producer, insert it at correct place
478 id = id.section('_', 1);
479 if (id.endsWith("audio")) {
480 int pos = id.section('_', 0, 0).toInt();
481 if (pos >= m_audioTrackProducers.count()) {
482 while (m_audioTrackProducers.count() - 1 < pos) {
483 m_audioTrackProducers.append(NULL);
486 if (m_audioTrackProducers.at(pos) == NULL) {
487 m_audioTrackProducers[pos] = producer;
490 else delete producer;
492 } else if (id.endsWith("video")) {
493 if (m_videoOnlyProducer == NULL) {
494 m_videoOnlyProducer = producer;
497 else delete producer;
500 int pos = id.toInt();
501 if (pos >= m_baseTrackProducers.count()) {
502 while (m_baseTrackProducers.count() - 1 < pos) {
503 m_baseTrackProducers.append(NULL);
506 if (m_baseTrackProducers.at(pos) == NULL) {
507 m_baseTrackProducers[pos] = producer;
510 else delete producer;
512 if (m_baseTrackProducers.isEmpty()) {
513 m_baseTrackProducers.append(producer);
516 else if (m_baseTrackProducers.at(0) == NULL) {
517 m_baseTrackProducers[0] = producer;
520 else delete producer;
522 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
523 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
526 static double getPixelAspect(QMap<QString, QString>& props) {
527 int width = props.value("frame_size").section('x', 0, 0).toInt();
528 int height = props.value("frame_size").section('x', 1, 1).toInt();
529 int aspectNumerator = props.value("force_aspect_num").toInt();
530 int aspectDenominator = props.value("force_aspect_den").toInt();
531 if (aspectDenominator != 0 && width != 0)
532 return double(height) * aspectNumerator / aspectDenominator / width;
537 Mlt::Producer *DocClipBase::audioProducer(int track)
539 QMutexLocker locker(&m_producerMutex);
540 if (m_audioTrackProducers.count() <= track) {
541 while (m_audioTrackProducers.count() - 1 < track) {
542 m_audioTrackProducers.append(NULL);
545 if (m_audioTrackProducers.at(track) == NULL) {
547 for (i = 0; i < m_audioTrackProducers.count(); i++)
548 if (m_audioTrackProducers.at(i) != NULL) break;
550 if (i >= m_audioTrackProducers.count()) {
551 // Could not find a valid producer for that clip
553 base = getProducer();
559 else base = m_audioTrackProducers.at(i);
560 m_audioTrackProducers[track] = cloneProducer(base);
561 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
563 return m_audioTrackProducers.at(track);
567 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
569 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
570 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
571 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
572 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
573 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
574 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
575 if (mute) prod->set("audio_index", -1);
576 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
577 if (blind) prod->set("video_index", -1);
578 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
579 prod->set("id", id.toUtf8().constData());
580 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
581 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
582 if (m_properties.contains("proxy_out")) {
583 // We have a proxy clip, make sure the proxy has same duration as original
584 prod->set("length", m_properties.value("duration").toInt());
585 prod->set("out", m_properties.value("proxy_out").toInt());
590 Mlt::Producer *DocClipBase::videoProducer()
592 QMutexLocker locker(&m_producerMutex);
593 if (m_videoOnlyProducer == NULL) {
595 for (i = 0; i < m_baseTrackProducers.count(); i++)
596 if (m_baseTrackProducers.at(i) != NULL) break;
597 if (i >= m_baseTrackProducers.count()) return NULL;
598 m_videoOnlyProducer = cloneProducer(m_baseTrackProducers.at(i));
599 adjustProducerProperties(m_videoOnlyProducer, QString(getId() + "_video"), true, false);
601 return m_videoOnlyProducer;
604 Mlt::Producer *DocClipBase::getCloneProducer()
606 Mlt::Producer *source = NULL;
607 Mlt::Producer *prod = NULL;
608 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
609 source = getProducer();
610 if (!source) return NULL;
612 if (m_clipType == COLOR) {
613 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
614 } else if (m_clipType == TEXT) {
615 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
616 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
617 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
621 QMutexLocker locker(&m_producerMutex);
622 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
623 if (m_baseTrackProducers.at(i) != NULL) {
624 source = m_baseTrackProducers.at(i);
628 if (!source) return NULL;
630 prod = cloneProducer(source);
633 adjustProducerProperties(prod, getId() + "_", false, false);
634 if (!m_properties.contains("proxy_out")) {
635 // Adjust length in case...
636 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
637 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
643 Mlt::Producer *DocClipBase::getProducer(int track)
645 QMutexLocker locker(&m_producerMutex);
646 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
647 if (m_baseTrackProducers.count() == 0) {
650 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
651 if (m_baseTrackProducers.at(i) != NULL) {
652 return m_baseTrackProducers.at(i);
657 if (track >= m_baseTrackProducers.count()) {
658 while (m_baseTrackProducers.count() - 1 < track) {
659 m_baseTrackProducers.append(NULL);
662 if (m_baseTrackProducers.at(track) == NULL) {
664 for (i = 0; i < m_baseTrackProducers.count(); i++)
665 if (m_baseTrackProducers.at(i) != NULL) break;
667 if (i >= m_baseTrackProducers.count()) {
668 // Could not find a valid producer for that clip, check in
671 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
672 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
673 m_baseTrackProducers[track] = prod;
675 return m_baseTrackProducers.at(track);
679 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
681 Mlt::Producer *result = NULL;
682 QString url = QString::fromUtf8(source->get("resource"));
683 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
684 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
686 if (result == NULL || !result->is_valid()) {
688 QString txt = "+" + i18n("Missing clip") + ".txt";
689 char *tmp = qstrdup(txt.toUtf8().constData());
690 result = new Mlt::Producer(*source->profile(), tmp);
692 if (result == NULL || !result->is_valid())
693 result = new Mlt::Producer(*(source->profile()), "colour:red");
695 result->set("bgcolour", "0xff0000ff");
696 result->set("pad", "10");
700 /*Mlt::Properties src_props(source->get_properties());
701 Mlt::Properties props(result->get_properties());
702 props.inherit(src_props);*/
706 void DocClipBase::setProducerProperty(const char *name, int data)
708 QMutexLocker locker(&m_producerMutex);
709 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
710 if (m_baseTrackProducers.at(i) != NULL)
711 m_baseTrackProducers[i]->set(name, data);
715 void DocClipBase::setProducerProperty(const char *name, double data)
717 QMutexLocker locker(&m_producerMutex);
718 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
719 if (m_baseTrackProducers.at(i) != NULL)
720 m_baseTrackProducers[i]->set(name, data);
724 void DocClipBase::setProducerProperty(const char *name, const char *data)
726 QMutexLocker locker(&m_producerMutex);
727 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
728 if (m_baseTrackProducers.at(i) != NULL)
729 m_baseTrackProducers[i]->set(name, data);
733 void DocClipBase::resetProducerProperty(const char *name)
735 QMutexLocker locker(&m_producerMutex);
736 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
737 if (m_baseTrackProducers.at(i) != NULL)
738 m_baseTrackProducers[i]->set(name, (const char*) NULL);
742 const char *DocClipBase::producerProperty(const char *name) const
744 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
745 if (m_baseTrackProducers.at(i) != NULL) {
746 return m_baseTrackProducers.at(i)->get(name);
753 void DocClipBase::slotRefreshProducer()
755 if (m_baseTrackProducers.count() == 0) return;
756 if (m_clipType == SLIDESHOW) {
757 setProducerProperty("ttl", getProperty("ttl").toInt());
758 //m_clipProducer->set("id", getProperty("id"));
759 if (!getProperty("animation").isEmpty()) {
760 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
762 Mlt::Filter *filter = clipService.filter(ct);
764 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
766 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
767 clipService.detach(*filter);
769 filter = clipService.filter(ct);
772 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
773 // filter does not exist, create it.
774 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
775 if (filter && filter->is_valid()) {
776 int cycle = getProperty("ttl").toInt();
777 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
778 if (!geometry.isEmpty()) {
779 if (getProperty("animation").contains("low-pass")) {
780 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
781 if (blur && blur->is_valid())
782 clipService.attach(*blur);
784 filter->set("transition.geometry", geometry.toUtf8().data());
785 filter->set("transition.cycle", cycle);
786 clipService.attach(*filter);
791 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
793 Mlt::Filter *filter = clipService.filter(0);
795 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
796 clipService.detach(*filter);
798 filter = clipService.filter(ct);
801 if (getProperty("fade") == "1") {
802 // we want a fade filter effect
803 kDebug() << "//////////// FADE WANTED";
804 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
806 Mlt::Filter *filter = clipService.filter(ct);
808 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
812 filter = clipService.filter(ct);
815 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
816 filter->set("cycle", getProperty("ttl").toInt());
817 filter->set("duration", getProperty("luma_duration").toInt());
818 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
819 if (!getProperty("softness").isEmpty()) {
820 int soft = getProperty("softness").toInt();
821 filter->set("luma.softness", (double) soft / 100.0);
824 // filter does not exist, create it...
825 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
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);
833 clipService.attach(*filter);
836 kDebug() << "//////////// FADE NOT WANTED!!!";
837 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
839 Mlt::Filter *filter = clipService.filter(0);
841 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
842 clipService.detach(*filter);
844 filter = clipService.filter(ct);
847 if (getProperty("crop") == "1") {
848 // we want a center crop filter effect
849 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
851 Mlt::Filter *filter = clipService.filter(ct);
853 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
857 filter = clipService.filter(ct);
860 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
861 // filter does not exist, create it...
862 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
863 filter->set("center", 1);
864 clipService.attach(*filter);
867 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
869 Mlt::Filter *filter = clipService.filter(0);
871 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
872 clipService.detach(*filter);
874 filter = clipService.filter(ct);
880 void DocClipBase::setProperties(QMap <QString, QString> properties)
882 // changing clip type is not allowed
883 properties.remove("type");
884 QMapIterator<QString, QString> i(properties);
885 bool refreshProducer = false;
887 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
888 QString oldProxy = m_properties.value("proxy");
889 while (i.hasNext()) {
891 setProperty(i.key(), i.value());
892 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
894 if (properties.contains("proxy")) {
895 QString value = properties.value("proxy");
896 // If value is "-", that means user manually disabled proxy on this clip
897 if (value.isEmpty() || value == "-") {
899 emit abortProxy(m_id, oldProxy);
902 emit createProxy(m_id);
905 if (refreshProducer) slotRefreshProducer();
908 void DocClipBase::setMetadata(QMap <QString, QString> properties)
910 QMapIterator<QString, QString> i(properties);
911 while (i.hasNext()) {
913 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
914 m_metadata.remove(i.key());
916 m_metadata.insert(i.key(), i.value());
921 QMap <QString, QString> DocClipBase::metadata() const
926 void DocClipBase::clearProperty(const QString &key)
928 m_properties.remove(key);
931 void DocClipBase::getFileHash(const QString &url)
933 if (m_clipType == SLIDESHOW) return;
935 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
938 //kDebug() << "SETTING HASH of" << value;
939 m_properties.insert("file_size", QString::number(file.size()));
941 * 1 MB = 1 second per 450 files (or faster)
942 * 10 MB = 9 seconds per 450 files (or faster)
944 if (file.size() > 1000000*2) {
945 fileData = file.read(1000000);
946 if (file.seek(file.size() - 1000000))
947 fileData.append(file.readAll());
949 fileData = file.readAll();
951 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
952 m_properties.insert("file_hash", QString(fileHash.toHex()));
956 bool DocClipBase::checkHash() const
958 KUrl url = fileURL();
959 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
963 QString DocClipBase::getClipHash() const
966 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
967 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
968 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
970 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
971 if (hash.isEmpty()) hash = getHash(fileURL().path());
977 void DocClipBase::setPlaceHolder(bool place)
979 m_placeHolder = place;
983 QString DocClipBase::getHash(const QString &path)
986 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
990 * 1 MB = 1 second per 450 files (or faster)
991 * 10 MB = 9 seconds per 450 files (or faster)
993 if (file.size() > 1000000*2) {
994 fileData = file.read(1000000);
995 if (file.seek(file.size() - 1000000))
996 fileData.append(file.readAll());
998 fileData = file.readAll();
1000 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1005 void DocClipBase::refreshThumbUrl()
1007 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1010 void DocClipBase::setProperty(const QString &key, const QString &value)
1012 m_properties.insert(key, value);
1013 if (key == "resource") {
1015 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1016 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1017 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1018 else if (key == "colour") {
1019 setProducerProperty("colour", value.toUtf8().data());
1020 } else if (key == "templatetext") {
1021 setProducerProperty("templatetext", value.toUtf8().data());
1022 setProducerProperty("force_reload", 1);
1023 } else if (key == "xmldata") {
1024 setProducerProperty("xmldata", value.toUtf8().data());
1025 setProducerProperty("force_reload", 1);
1026 } else if (key == "force_aspect_num") {
1027 if (value.isEmpty()) {
1028 m_properties.remove("force_aspect_num");
1029 resetProducerProperty("force_aspect_ratio");
1030 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1031 } else if (key == "force_aspect_den") {
1032 if (value.isEmpty()) {
1033 m_properties.remove("force_aspect_den");
1034 resetProducerProperty("force_aspect_ratio");
1035 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1036 } else if (key == "force_fps") {
1037 if (value.isEmpty()) {
1038 m_properties.remove("force_fps");
1039 resetProducerProperty("force_fps");
1040 } else setProducerProperty("force_fps", value.toDouble());
1041 } else if (key == "force_progressive") {
1042 if (value.isEmpty()) {
1043 m_properties.remove("force_progressive");
1044 resetProducerProperty("force_progressive");
1045 } else setProducerProperty("force_progressive", value.toInt());
1046 } else if (key == "force_tff") {
1047 if (value.isEmpty()) {
1048 m_properties.remove("force_tff");
1049 resetProducerProperty("force_tff");
1050 } else setProducerProperty("force_tff", value.toInt());
1051 } else if (key == "threads") {
1052 if (value.isEmpty()) {
1053 m_properties.remove("threads");
1054 setProducerProperty("threads", 1);
1055 } else setProducerProperty("threads", value.toInt());
1056 } else if (key == "video_index") {
1057 if (value.isEmpty()) {
1058 m_properties.remove("video_index");
1059 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1060 } else setProducerProperty("video_index", value.toInt());
1061 } else if (key == "audio_index") {
1062 if (value.isEmpty()) {
1063 m_properties.remove("audio_index");
1064 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1065 } else setProducerProperty("audio_index", value.toInt());
1066 } else if (key == "force_colorspace") {
1067 if (value.isEmpty()) {
1068 m_properties.remove("force_colorspace");
1069 resetProducerProperty("force_colorspace");
1070 } else setProducerProperty("force_colorspace", value.toInt());
1071 } else if (key == "full_luma") {
1072 if (value.isEmpty()) {
1073 m_properties.remove("full_luma");
1074 resetProducerProperty("set.force_full_luma");
1075 } else setProducerProperty("set.force_full_luma", value.toInt());
1079 QMap <QString, QString> DocClipBase::properties() const
1081 return m_properties;
1084 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1086 QMap <QString, QString> currentProps;
1087 QMap<QString, QString>::const_iterator i = props.constBegin();
1088 while (i != props.constEnd()) {
1089 currentProps.insert(i.key(), m_properties.value(i.key()));
1092 return currentProps;
1095 bool DocClipBase::getAudioThumbs()
1097 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1098 if (m_audioThumbCreated) {
1101 QTimer::singleShot(800, m_thumbProd, SLOT(slotCreateAudioThumbs()));
1105 bool DocClipBase::isPlaceHolder() const
1107 return m_placeHolder;
1110 void DocClipBase::addCutZone(int in, int out, QString desc)
1113 info.zone = QPoint(in, out);
1114 info.description = desc;
1115 for (int i = 0; i < m_cutZones.count(); i++)
1116 if (m_cutZones.at(i).zone == info.zone) {
1119 m_cutZones.append(info);
1122 bool DocClipBase::hasCutZone(QPoint p) const
1124 for (int i = 0; i < m_cutZones.count(); i++)
1125 if (m_cutZones.at(i).zone == p) return true;
1130 void DocClipBase::removeCutZone(int in, int out)
1133 for (int i = 0; i < m_cutZones.count(); i++) {
1134 if (m_cutZones.at(i).zone == p) {
1135 m_cutZones.removeAt(i);
1141 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1143 QPoint old(oldin, oldout);
1144 for (int i = 0; i < m_cutZones.size(); ++i) {
1145 if (m_cutZones.at(i).zone == old) {
1147 info.zone = QPoint(in, out);
1148 info.description = desc;
1149 m_cutZones.replace(i, info);
1155 QList <CutZoneInfo> DocClipBase::cutZones() const
1160 bool DocClipBase::hasVideoCodec(const QString &codec) const
1162 Mlt::Producer *prod = NULL;
1163 if (m_baseTrackProducers.count() == 0) return false;
1164 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1165 if (m_baseTrackProducers.at(i) != NULL) {
1166 prod = m_baseTrackProducers.at(i);
1171 if (!prod) return false;
1172 int default_video = prod->get_int("video_index");
1174 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1175 return prod->get(property) == codec;
1178 bool DocClipBase::hasAudioCodec(const QString &codec) const
1180 Mlt::Producer *prod = NULL;
1181 if (m_baseTrackProducers.count() == 0) return false;
1182 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1183 if (m_baseTrackProducers.at(i) != NULL) {
1184 prod = m_baseTrackProducers.at(i);
1188 if (!prod) return false;
1189 int default_video = prod->get_int("audio_index");
1191 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1192 return prod->get(property) == codec;
1196 void DocClipBase::slotExtractImage(QList <int> frames)
1198 if (m_thumbProd == NULL) return;
1199 m_thumbProd->extractImage(frames);
1202 QImage DocClipBase::extractImage(int frame, int width, int height)
1204 if (m_thumbProd == NULL) return QImage();
1205 QMutexLocker locker(&m_producerMutex);
1206 return m_thumbProd->extractImage(frame, width, height);