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"));
90 if (m_clipType & (AV | AUDIO | PLAYLIST)) getAudioThumbs();
93 DocClipBase::~DocClipBase()
96 qDeleteAll(m_toDeleteProducers);
97 m_toDeleteProducers.clear();
98 qDeleteAll(m_baseTrackProducers);
99 m_baseTrackProducers.clear();
100 qDeleteAll(m_audioTrackProducers);
101 m_audioTrackProducers.clear();
102 delete m_videoOnlyProducer;
103 m_videoOnlyProducer = NULL;
106 void DocClipBase::setZone(QPoint zone)
108 m_properties.insert("zone_in", QString::number(zone.x()));
109 m_properties.insert("zone_out", QString::number(zone.y()));
112 QPoint DocClipBase::zone() const
114 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
119 void DocClipBase::slotClearAudioCache()
121 m_audioFrameCache.clear();
122 m_audioThumbCreated = false;
125 /*void DocClipBase::getClipMainThumb() {
126 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
129 KThumb *DocClipBase::thumbProducer()
134 bool DocClipBase::audioThumbCreated() const
136 return m_audioThumbCreated;
139 const QString DocClipBase::name() const
142 return m_properties.value("name");
145 const QString &DocClipBase::getId() const
150 const CLIPTYPE & DocClipBase::clipType() const
155 void DocClipBase::setClipType(CLIPTYPE type)
158 m_properties.insert("type", QString::number((int) type));
161 KUrl DocClipBase::fileURL() const
163 QString res = m_properties.value("resource");
164 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
168 void DocClipBase::setClipThumbFrame(const uint &ix)
170 m_properties.insert("thumbnail", QString::number((int) ix));
173 uint DocClipBase::getClipThumbFrame() const
175 return (uint) m_properties.value("thumbnail").toInt();
178 const QString DocClipBase::description() const
180 return m_properties.value("description");
183 bool DocClipBase::isTransparent() const
185 return (m_properties.value("transparency") == "1");
188 const QString DocClipBase::getProperty(const QString &prop) const
190 return m_properties.value(prop);
193 void DocClipBase::setDuration(GenTime dur)
196 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
199 const GenTime &DocClipBase::duration() const
204 const GenTime DocClipBase::maxDuration() const
206 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
207 /*const GenTime dur(15000, KdenliveSettings::project_fps());
214 bool DocClipBase::hasFileSize() const
219 qulonglong DocClipBase::fileSize() const
221 return m_properties.value("file_size").toULongLong();
225 QDomElement DocClipBase::toXML() const
228 QDomElement clip = doc.createElement("producer");
230 QMapIterator<QString, QString> i(m_properties);
231 while (i.hasNext()) {
233 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
235 doc.appendChild(clip);
236 if (!m_cutZones.isEmpty()) {
238 for (int i = 0; i < m_cutZones.size(); i++) {
239 CutZoneInfo info = m_cutZones.at(i);
240 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
242 clip.setAttribute("cutzones", cuts.join(";"));
244 //kDebug() << "/// CLIP XML: " << doc.toString();
245 return doc.documentElement();
249 void DocClipBase::setAudioThumbCreated(bool isDone)
251 m_audioThumbCreated = isDone;
254 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
256 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
257 m_audioFrameCache = data;
258 m_audioThumbCreated = true;
262 QList < GenTime > DocClipBase::snapMarkers() const
264 QList < GenTime > markers;
266 for (int count = 0; count < m_snapMarkers.count(); ++count) {
267 markers.append(m_snapMarkers.at(count).time());
273 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
275 return m_snapMarkers;
279 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
281 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
282 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
283 if ((*it).time() >= time)
287 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
288 (*it).setComment(comment);
289 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
291 CommentedTime t(time, comment);
292 m_snapMarkers.insert(it, t);
297 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
299 QList < CommentedTime >::Iterator it;
300 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
301 if ((*it).time() == time)
304 if (it != m_snapMarkers.end()) {
305 (*it).setComment(comment);
307 kError() << "trying to edit Snap Marker that does not already exists";
311 QString DocClipBase::deleteSnapMarker(const GenTime & time)
313 QString result = i18n("Marker");
314 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
316 while (itt != m_snapMarkers.end()) {
317 if ((*itt).time() == time)
322 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
323 result = (*itt).comment();
324 m_snapMarkers.erase(itt);
330 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
332 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
334 while (itt != m_snapMarkers.end()) {
335 if ((*itt).time() == time)
343 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
346 for (it = 0; it < m_snapMarkers.count(); it++) {
347 if (m_snapMarkers.at(it).time() >= currTime)
350 if (it == 0) return GenTime();
351 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
352 return m_snapMarkers.at(it).time();
353 else return m_snapMarkers.at(it - 1).time();
356 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
359 for (it = 0; it < m_snapMarkers.count(); it++) {
360 if (m_snapMarkers.at(it).time() > currTime)
363 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
367 QString DocClipBase::markerComment(GenTime t)
369 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
371 while (itt != m_snapMarkers.end()) {
372 if ((*itt).time() == t)
373 return (*itt).comment();
379 void DocClipBase::clearThumbProducer()
381 if (m_thumbProd) m_thumbProd->clearProducer();
384 void DocClipBase::reloadThumbProducer()
386 if (m_thumbProd && !m_thumbProd->hasProducer())
387 m_thumbProd->setProducer(getProducer());
390 void DocClipBase::deleteProducers()
392 if (m_thumbProd) m_thumbProd->clearProducer();
394 if (numReferences() > 0) {
395 // Clip is used in timeline, delay producers deletion
396 if (m_videoOnlyProducer) m_toDeleteProducers.append(m_videoOnlyProducer);
397 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
398 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
400 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
401 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
405 delete m_videoOnlyProducer;
406 qDeleteAll(m_baseTrackProducers);
407 qDeleteAll(m_audioTrackProducers);
408 m_replaceMutex.unlock();
410 m_videoOnlyProducer = NULL;
411 m_baseTrackProducers.clear();
412 m_audioTrackProducers.clear();
415 void DocClipBase::cleanupProducers()
419 kDebug()<<"----------------------------------------------------------------------------------";
420 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
421 if (m_toDeleteProducers.at(i) != NULL) {
422 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
423 if (props.ref_count() > 2) {
424 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
431 qDeleteAll(m_toDeleteProducers);
432 m_toDeleteProducers.clear();
433 m_replaceMutex.unlock();
436 bool DocClipBase::isClean() const
438 return m_toDeleteProducers.isEmpty();
441 void DocClipBase::setValid()
443 m_placeHolder = false;
446 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
448 if (producer == NULL) return;
450 QMutexLocker locker(&m_producerMutex);
451 m_replaceMutex.lock();
454 QString id = producer->get("id");
455 if (m_placeHolder || !producer->is_valid()) {
456 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
457 producer->set("markup", tmp);
458 producer->set("bgcolour", "0xff0000ff");
459 producer->set("pad", "10");
462 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
463 if (m_clipType != AUDIO) {
464 if (!id.endsWith("_audio"))
465 m_thumbProd->setProducer(producer);
467 else m_thumbProd->setProducer(producer);
469 bool updated = false;
470 if (id.contains('_')) {
471 // this is a subtrack producer, insert it at correct place
472 id = id.section('_', 1);
473 if (id.endsWith("audio")) {
474 int pos = id.section('_', 0, 0).toInt();
475 if (pos >= m_audioTrackProducers.count()) {
476 while (m_audioTrackProducers.count() - 1 < pos) {
477 m_audioTrackProducers.append(NULL);
480 if (m_audioTrackProducers.at(pos) == NULL) {
481 m_audioTrackProducers[pos] = producer;
484 else delete producer;
486 } else if (id.endsWith("video")) {
487 if (m_videoOnlyProducer == NULL) {
488 m_videoOnlyProducer = producer;
491 else delete producer;
494 int pos = id.toInt();
495 if (pos >= m_baseTrackProducers.count()) {
496 while (m_baseTrackProducers.count() - 1 < pos) {
497 m_baseTrackProducers.append(NULL);
500 if (m_baseTrackProducers.at(pos) == NULL) {
501 m_baseTrackProducers[pos] = producer;
504 else delete producer;
506 if (m_baseTrackProducers.isEmpty()) {
507 m_baseTrackProducers.append(producer);
510 else if (m_baseTrackProducers.at(0) == NULL) {
511 m_baseTrackProducers[0] = producer;
514 else delete producer;
516 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
517 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
520 static double getPixelAspect(QMap<QString, QString>& props) {
521 int width = props.value("frame_size").section('x', 0, 0).toInt();
522 int height = props.value("frame_size").section('x', 1, 1).toInt();
523 int aspectNumerator = props.value("force_aspect_num").toInt();
524 int aspectDenominator = props.value("force_aspect_den").toInt();
525 if (aspectDenominator != 0 && width != 0)
526 return double(height) * aspectNumerator / aspectDenominator / width;
531 Mlt::Producer *DocClipBase::audioProducer(int track)
533 QMutexLocker locker(&m_producerMutex);
534 if (m_audioTrackProducers.count() <= track) {
535 while (m_audioTrackProducers.count() - 1 < track) {
536 m_audioTrackProducers.append(NULL);
539 if (m_audioTrackProducers.at(track) == NULL) {
541 for (i = 0; i < m_audioTrackProducers.count(); i++)
542 if (m_audioTrackProducers.at(i) != NULL) break;
544 if (i >= m_audioTrackProducers.count()) {
545 // Could not find a valid producer for that clip
547 base = getProducer();
553 else base = m_audioTrackProducers.at(i);
554 m_audioTrackProducers[track] = cloneProducer(base);
555 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
557 return m_audioTrackProducers.at(track);
561 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
563 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
564 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
565 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
566 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
567 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
568 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
569 if (mute) prod->set("audio_index", -1);
570 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
571 if (blind) prod->set("video_index", -1);
572 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
573 prod->set("id", id.toUtf8().constData());
574 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
575 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
576 if (m_properties.contains("proxy_out")) {
577 // We have a proxy clip, make sure the proxy has same duration as original
578 prod->set("length", m_properties.value("duration").toInt());
579 prod->set("out", m_properties.value("proxy_out").toInt());
584 Mlt::Producer *DocClipBase::videoProducer()
586 QMutexLocker locker(&m_producerMutex);
587 if (m_videoOnlyProducer == NULL) {
589 for (i = 0; i < m_baseTrackProducers.count(); i++)
590 if (m_baseTrackProducers.at(i) != NULL) break;
591 if (i >= m_baseTrackProducers.count()) return NULL;
592 m_videoOnlyProducer = cloneProducer(m_baseTrackProducers.at(i));
593 adjustProducerProperties(m_videoOnlyProducer, QString(getId() + "_video"), true, false);
595 return m_videoOnlyProducer;
598 Mlt::Producer *DocClipBase::getCloneProducer()
600 Mlt::Producer *source = NULL;
601 Mlt::Producer *prod = NULL;
602 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
603 source = getProducer();
604 if (!source) return NULL;
606 if (m_clipType == COLOR) {
607 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
608 } else if (m_clipType == TEXT) {
609 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
610 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
611 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
615 QMutexLocker locker(&m_producerMutex);
616 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
617 if (m_baseTrackProducers.at(i) != NULL) {
618 source = m_baseTrackProducers.at(i);
622 if (!source) return NULL;
624 prod = cloneProducer(source);
627 adjustProducerProperties(prod, getId() + "_", false, false);
628 if (!m_properties.contains("proxy_out")) {
629 // Adjust length in case...
630 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
631 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
637 Mlt::Producer *DocClipBase::getProducer(int track)
639 QMutexLocker locker(&m_producerMutex);
640 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
641 if (m_baseTrackProducers.count() == 0) {
644 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
645 if (m_baseTrackProducers.at(i) != NULL) {
646 return m_baseTrackProducers.at(i);
651 if (track >= m_baseTrackProducers.count()) {
652 while (m_baseTrackProducers.count() - 1 < track) {
653 m_baseTrackProducers.append(NULL);
656 if (m_baseTrackProducers.at(track) == NULL) {
658 for (i = 0; i < m_baseTrackProducers.count(); i++)
659 if (m_baseTrackProducers.at(i) != NULL) break;
661 if (i >= m_baseTrackProducers.count()) {
662 // Could not find a valid producer for that clip, check in
665 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
666 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
667 m_baseTrackProducers[track] = prod;
669 return m_baseTrackProducers.at(track);
673 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
675 Mlt::Producer *result = NULL;
676 QString url = QString::fromUtf8(source->get("resource"));
677 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
678 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
680 if (result == NULL || !result->is_valid()) {
682 QString txt = "+" + i18n("Missing clip") + ".txt";
683 char *tmp = qstrdup(txt.toUtf8().constData());
684 result = new Mlt::Producer(*source->profile(), tmp);
686 if (result == NULL || !result->is_valid())
687 result = new Mlt::Producer(*(source->profile()), "colour:red");
689 result->set("bgcolour", "0xff0000ff");
690 result->set("pad", "10");
694 /*Mlt::Properties src_props(source->get_properties());
695 Mlt::Properties props(result->get_properties());
696 props.inherit(src_props);*/
700 void DocClipBase::setProducerProperty(const char *name, int data)
702 QMutexLocker locker(&m_producerMutex);
703 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
704 if (m_baseTrackProducers.at(i) != NULL)
705 m_baseTrackProducers[i]->set(name, data);
709 void DocClipBase::setProducerProperty(const char *name, double data)
711 QMutexLocker locker(&m_producerMutex);
712 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
713 if (m_baseTrackProducers.at(i) != NULL)
714 m_baseTrackProducers[i]->set(name, data);
718 void DocClipBase::setProducerProperty(const char *name, const char *data)
720 QMutexLocker locker(&m_producerMutex);
721 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
722 if (m_baseTrackProducers.at(i) != NULL)
723 m_baseTrackProducers[i]->set(name, data);
727 void DocClipBase::resetProducerProperty(const char *name)
729 QMutexLocker locker(&m_producerMutex);
730 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
731 if (m_baseTrackProducers.at(i) != NULL)
732 m_baseTrackProducers[i]->set(name, (const char*) NULL);
736 const char *DocClipBase::producerProperty(const char *name) const
738 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
739 if (m_baseTrackProducers.at(i) != NULL) {
740 return m_baseTrackProducers.at(i)->get(name);
747 void DocClipBase::slotRefreshProducer()
749 if (m_baseTrackProducers.count() == 0) return;
750 if (m_clipType == SLIDESHOW) {
751 setProducerProperty("ttl", getProperty("ttl").toInt());
752 //m_clipProducer->set("id", getProperty("id"));
753 if (!getProperty("animation").isEmpty()) {
754 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
756 Mlt::Filter *filter = clipService.filter(ct);
758 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
760 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
761 clipService.detach(*filter);
763 filter = clipService.filter(ct);
766 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
767 // filter does not exist, create it.
768 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
769 if (filter && filter->is_valid()) {
770 int cycle = getProperty("ttl").toInt();
771 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
772 if (!geometry.isEmpty()) {
773 if (getProperty("animation").contains("low-pass")) {
774 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
775 if (blur && blur->is_valid())
776 clipService.attach(*blur);
778 filter->set("transition.geometry", geometry.toUtf8().data());
779 filter->set("transition.cycle", cycle);
780 clipService.attach(*filter);
785 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
787 Mlt::Filter *filter = clipService.filter(0);
789 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
790 clipService.detach(*filter);
792 filter = clipService.filter(ct);
795 if (getProperty("fade") == "1") {
796 // we want a fade filter effect
797 kDebug() << "//////////// FADE WANTED";
798 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
800 Mlt::Filter *filter = clipService.filter(ct);
802 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
806 filter = clipService.filter(ct);
809 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
810 filter->set("cycle", getProperty("ttl").toInt());
811 filter->set("duration", getProperty("luma_duration").toInt());
812 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
813 if (!getProperty("softness").isEmpty()) {
814 int soft = getProperty("softness").toInt();
815 filter->set("luma.softness", (double) soft / 100.0);
818 // filter does not exist, create it...
819 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
820 filter->set("cycle", getProperty("ttl").toInt());
821 filter->set("duration", getProperty("luma_duration").toInt());
822 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
823 if (!getProperty("softness").isEmpty()) {
824 int soft = getProperty("softness").toInt();
825 filter->set("luma.softness", (double) soft / 100.0);
827 clipService.attach(*filter);
830 kDebug() << "//////////// FADE NOT WANTED!!!";
831 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
833 Mlt::Filter *filter = clipService.filter(0);
835 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
836 clipService.detach(*filter);
838 filter = clipService.filter(ct);
841 if (getProperty("crop") == "1") {
842 // we want a center crop filter effect
843 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
845 Mlt::Filter *filter = clipService.filter(ct);
847 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
851 filter = clipService.filter(ct);
854 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
855 // filter does not exist, create it...
856 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
857 filter->set("center", 1);
858 clipService.attach(*filter);
861 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
863 Mlt::Filter *filter = clipService.filter(0);
865 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
866 clipService.detach(*filter);
868 filter = clipService.filter(ct);
874 void DocClipBase::setProperties(QMap <QString, QString> properties)
876 // changing clip type is not allowed
877 properties.remove("type");
878 QMapIterator<QString, QString> i(properties);
879 bool refreshProducer = false;
881 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
882 QString oldProxy = m_properties.value("proxy");
883 while (i.hasNext()) {
885 setProperty(i.key(), i.value());
886 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
888 if (properties.contains("proxy")) {
889 QString value = properties.value("proxy");
890 // If value is "-", that means user manually disabled proxy on this clip
891 if (value.isEmpty() || value == "-") {
893 emit abortProxy(m_id, oldProxy);
896 emit createProxy(m_id);
899 if (refreshProducer) slotRefreshProducer();
902 void DocClipBase::setMetadata(QMap <QString, QString> properties)
904 QMapIterator<QString, QString> i(properties);
905 while (i.hasNext()) {
907 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
908 m_metadata.remove(i.key());
910 m_metadata.insert(i.key(), i.value());
915 QMap <QString, QString> DocClipBase::metadata() const
920 void DocClipBase::clearProperty(const QString &key)
922 m_properties.remove(key);
925 void DocClipBase::getFileHash(const QString &url)
927 if (m_clipType == SLIDESHOW) return;
929 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
932 //kDebug() << "SETTING HASH of" << value;
933 m_properties.insert("file_size", QString::number(file.size()));
935 * 1 MB = 1 second per 450 files (or faster)
936 * 10 MB = 9 seconds per 450 files (or faster)
938 if (file.size() > 1000000*2) {
939 fileData = file.read(1000000);
940 if (file.seek(file.size() - 1000000))
941 fileData.append(file.readAll());
943 fileData = file.readAll();
945 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
946 m_properties.insert("file_hash", QString(fileHash.toHex()));
950 bool DocClipBase::checkHash() const
952 KUrl url = fileURL();
953 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
957 QString DocClipBase::getClipHash() const
960 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
961 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
962 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
964 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
965 if (hash.isEmpty()) hash = getHash(fileURL().path());
971 void DocClipBase::setPlaceHolder(bool place)
973 m_placeHolder = place;
977 QString DocClipBase::getHash(const QString &path)
980 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
984 * 1 MB = 1 second per 450 files (or faster)
985 * 10 MB = 9 seconds per 450 files (or faster)
987 if (file.size() > 1000000*2) {
988 fileData = file.read(1000000);
989 if (file.seek(file.size() - 1000000))
990 fileData.append(file.readAll());
992 fileData = file.readAll();
994 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
999 void DocClipBase::refreshThumbUrl()
1001 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1004 void DocClipBase::setProperty(const QString &key, const QString &value)
1006 m_properties.insert(key, value);
1007 if (key == "resource") {
1009 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1010 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1011 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1012 else if (key == "colour") {
1013 setProducerProperty("colour", value.toUtf8().data());
1014 } else if (key == "templatetext") {
1015 setProducerProperty("templatetext", value.toUtf8().data());
1016 setProducerProperty("force_reload", 1);
1017 } else if (key == "xmldata") {
1018 setProducerProperty("xmldata", value.toUtf8().data());
1019 setProducerProperty("force_reload", 1);
1020 } else if (key == "force_aspect_num") {
1021 if (value.isEmpty()) {
1022 m_properties.remove("force_aspect_num");
1023 resetProducerProperty("force_aspect_ratio");
1024 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1025 } else if (key == "force_aspect_den") {
1026 if (value.isEmpty()) {
1027 m_properties.remove("force_aspect_den");
1028 resetProducerProperty("force_aspect_ratio");
1029 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1030 } else if (key == "force_fps") {
1031 if (value.isEmpty()) {
1032 m_properties.remove("force_fps");
1033 resetProducerProperty("force_fps");
1034 } else setProducerProperty("force_fps", value.toDouble());
1035 } else if (key == "force_progressive") {
1036 if (value.isEmpty()) {
1037 m_properties.remove("force_progressive");
1038 resetProducerProperty("force_progressive");
1039 } else setProducerProperty("force_progressive", value.toInt());
1040 } else if (key == "force_tff") {
1041 if (value.isEmpty()) {
1042 m_properties.remove("force_tff");
1043 resetProducerProperty("force_tff");
1044 } else setProducerProperty("force_tff", value.toInt());
1045 } else if (key == "threads") {
1046 if (value.isEmpty()) {
1047 m_properties.remove("threads");
1048 setProducerProperty("threads", 1);
1049 } else setProducerProperty("threads", value.toInt());
1050 } else if (key == "video_index") {
1051 if (value.isEmpty()) {
1052 m_properties.remove("video_index");
1053 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1054 } else setProducerProperty("video_index", value.toInt());
1055 } else if (key == "audio_index") {
1056 if (value.isEmpty()) {
1057 m_properties.remove("audio_index");
1058 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1059 } else setProducerProperty("audio_index", value.toInt());
1060 } else if (key == "force_colorspace") {
1061 if (value.isEmpty()) {
1062 m_properties.remove("force_colorspace");
1063 resetProducerProperty("force_colorspace");
1064 } else setProducerProperty("force_colorspace", value.toInt());
1065 } else if (key == "full_luma") {
1066 if (value.isEmpty()) {
1067 m_properties.remove("full_luma");
1068 resetProducerProperty("set.force_full_luma");
1069 } else setProducerProperty("set.force_full_luma", value.toInt());
1073 QMap <QString, QString> DocClipBase::properties() const
1075 return m_properties;
1078 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1080 QMap <QString, QString> currentProps;
1081 QMap<QString, QString>::const_iterator i = props.constBegin();
1082 while (i != props.constEnd()) {
1083 currentProps.insert(i.key(), m_properties.value(i.key()));
1086 return currentProps;
1089 bool DocClipBase::getAudioThumbs()
1091 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1092 if (m_audioThumbCreated) {
1095 QTimer::singleShot(800, m_thumbProd, SLOT(slotCreateAudioThumbs()));
1099 bool DocClipBase::isPlaceHolder() const
1101 return m_placeHolder;
1104 void DocClipBase::addCutZone(int in, int out, QString desc)
1107 info.zone = QPoint(in, out);
1108 info.description = desc;
1109 for (int i = 0; i < m_cutZones.count(); i++)
1110 if (m_cutZones.at(i).zone == info.zone) {
1113 m_cutZones.append(info);
1116 bool DocClipBase::hasCutZone(QPoint p) const
1118 for (int i = 0; i < m_cutZones.count(); i++)
1119 if (m_cutZones.at(i).zone == p) return true;
1124 void DocClipBase::removeCutZone(int in, int out)
1127 for (int i = 0; i < m_cutZones.count(); i++) {
1128 if (m_cutZones.at(i).zone == p) {
1129 m_cutZones.removeAt(i);
1135 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1137 QPoint old(oldin, oldout);
1138 for (int i = 0; i < m_cutZones.size(); ++i) {
1139 if (m_cutZones.at(i).zone == old) {
1141 info.zone = QPoint(in, out);
1142 info.description = desc;
1143 m_cutZones.replace(i, info);
1149 QList <CutZoneInfo> DocClipBase::cutZones() const
1154 bool DocClipBase::hasVideoCodec(const QString &codec) const
1156 Mlt::Producer *prod = NULL;
1157 if (m_baseTrackProducers.count() == 0) return false;
1158 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1159 if (m_baseTrackProducers.at(i) != NULL) {
1160 prod = m_baseTrackProducers.at(i);
1165 if (!prod) return false;
1166 int default_video = prod->get_int("video_index");
1168 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1169 return prod->get(property) == codec;
1172 bool DocClipBase::hasAudioCodec(const QString &codec) const
1174 Mlt::Producer *prod = NULL;
1175 if (m_baseTrackProducers.count() == 0) return false;
1176 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1177 if (m_baseTrackProducers.at(i) != NULL) {
1178 prod = m_baseTrackProducers.at(i);
1182 if (!prod) return false;
1183 int default_video = prod->get_int("audio_index");
1185 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1186 return prod->get(property) == codec;
1190 void DocClipBase::slotExtractImage(QList <int> frames)
1192 if (m_thumbProd == NULL) return;
1193 m_thumbProd->extractImage(frames);
1196 QImage DocClipBase::extractImage(int frame, int width, int height)
1198 if (m_thumbProd == NULL) return QImage();
1199 QMutexLocker locker(&m_producerMutex);
1200 return m_thumbProd->extractImage(frame, width, height);