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 >()),
53 m_audioThumbCreated(false),
55 m_placeHolder(xml.hasAttribute("placeholder")),
58 int type = xml.attribute("type").toInt();
59 m_clipType = (CLIPTYPE) type;
60 if (m_placeHolder) xml.removeAttribute("placeholder");
61 QDomNamedNodeMap attributes = xml.attributes();
62 for (int i = 0; i < attributes.count(); i++) {
63 QString name = attributes.item(i).nodeName();
64 if (name.startsWith("meta.attr.")) {
65 m_metadata.insert(name.section('.', 2, 3), attributes.item(i).nodeValue());
66 } else m_properties.insert(name, attributes.item(i).nodeValue());
69 if (xml.hasAttribute("cutzones")) {
70 QStringList cuts = xml.attribute("cutzones").split(";", QString::SkipEmptyParts);
71 for (int i = 0; i < cuts.count(); i++) {
72 QString z = cuts.at(i);
73 addCutZone(z.section('-', 0, 0).toInt(), z.section('-', 1, 1).toInt(), z.section('-', 2, 2));
77 KUrl url = KUrl(xml.attribute("resource"));
78 if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
80 if (xml.hasAttribute("duration")) {
81 setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
83 int out = xml.attribute("out").toInt();
84 int in = xml.attribute("in").toInt();
85 setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
88 if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
90 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
91 if (m_clipType == AV || m_clipType == AUDIO || m_clipType == PLAYLIST) slotCreateAudioTimer();
94 DocClipBase::~DocClipBase()
101 qDeleteAll(m_baseTrackProducers);
102 m_baseTrackProducers.clear();
103 qDeleteAll(m_audioTrackProducers);
104 m_audioTrackProducers.clear();
105 delete m_videoOnlyProducer;
106 m_videoOnlyProducer = NULL;
109 void DocClipBase::setZone(QPoint zone)
111 m_properties.insert("zone_in", QString::number(zone.x()));
112 m_properties.insert("zone_out", QString::number(zone.y()));
115 QPoint DocClipBase::zone() const
117 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
121 void DocClipBase::slotCreateAudioTimer()
123 connect(m_thumbProd, SIGNAL(audioThumbReady(const audioByteArray&)), this , SLOT(updateAudioThumbnail(const audioByteArray&)));
124 m_audioTimer = new QTimer(this);
125 connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
128 void DocClipBase::askForAudioThumbs()
130 if (m_thumbProd && m_audioTimer) m_thumbProd->askForAudioThumbs(getId());
133 void DocClipBase::slotClearAudioCache()
135 if (m_thumbProd) m_thumbProd->stopAudioThumbs();
136 if (m_audioTimer != NULL) m_audioTimer->stop();
137 m_audioFrameCache.clear();
138 m_audioThumbCreated = false;
141 /*void DocClipBase::getClipMainThumb() {
142 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
145 KThumb *DocClipBase::thumbProducer()
150 bool DocClipBase::audioThumbCreated() const
152 return m_audioThumbCreated;
155 const QString DocClipBase::name() const
158 return m_properties.value("name");
161 const QString &DocClipBase::getId() const
166 void DocClipBase::setId(const QString &newId)
171 const CLIPTYPE & DocClipBase::clipType() const
176 void DocClipBase::setClipType(CLIPTYPE type)
180 m_properties.insert("type", QString::number((int) type));
181 if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO || m_clipType == PLAYLIST))
182 slotCreateAudioTimer();
185 KUrl DocClipBase::fileURL() const
187 QString res = m_properties.value("resource");
188 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
192 void DocClipBase::setClipThumbFrame(const uint &ix)
194 m_properties.insert("thumbnail", QString::number((int) ix));
197 uint DocClipBase::getClipThumbFrame() const
199 return (uint) m_properties.value("thumbnail").toInt();
202 const QString DocClipBase::description() const
204 return m_properties.value("description");
207 bool DocClipBase::isTransparent() const
209 return (m_properties.value("transparency") == "1");
212 const QString DocClipBase::getProperty(const QString &prop) const
214 return m_properties.value(prop);
217 void DocClipBase::setDuration(GenTime dur)
220 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
223 const GenTime &DocClipBase::duration() const
228 const GenTime DocClipBase::maxDuration() const
230 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
231 /*const GenTime dur(15000, KdenliveSettings::project_fps());
238 bool DocClipBase::hasFileSize() const
243 qulonglong DocClipBase::fileSize() const
245 return m_properties.value("file_size").toULongLong();
249 QDomElement DocClipBase::toXML() const
252 QDomElement clip = doc.createElement("producer");
254 QMapIterator<QString, QString> i(m_properties);
255 while (i.hasNext()) {
257 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
259 doc.appendChild(clip);
260 if (!m_cutZones.isEmpty()) {
262 for (int i = 0; i < m_cutZones.size(); i++) {
263 CutZoneInfo info = m_cutZones.at(i);
264 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
266 clip.setAttribute("cutzones", cuts.join(";"));
268 //kDebug() << "/// CLIP XML: " << doc.toString();
269 return doc.documentElement();
273 void DocClipBase::setAudioThumbCreated(bool isDone)
275 m_audioThumbCreated = isDone;
279 void DocClipBase::setThumbnail(const QPixmap & pixmap)
281 m_thumbnail = pixmap;
284 const QPixmap & DocClipBase::thumbnail() const
289 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
291 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
292 m_audioFrameCache = data;
293 m_audioThumbCreated = true;
297 QList < GenTime > DocClipBase::snapMarkers() const
299 QList < GenTime > markers;
301 for (int count = 0; count < m_snapMarkers.count(); ++count) {
302 markers.append(m_snapMarkers.at(count).time());
308 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
310 return m_snapMarkers;
314 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
316 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
317 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
318 if ((*it).time() >= time)
322 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
323 (*it).setComment(comment);
324 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
326 CommentedTime t(time, comment);
327 m_snapMarkers.insert(it, t);
332 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
334 QList < CommentedTime >::Iterator it;
335 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
336 if ((*it).time() == time)
339 if (it != m_snapMarkers.end()) {
340 (*it).setComment(comment);
342 kError() << "trying to edit Snap Marker that does not already exists";
346 QString DocClipBase::deleteSnapMarker(const GenTime & time)
348 QString result = i18n("Marker");
349 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
351 while (itt != m_snapMarkers.end()) {
352 if ((*itt).time() == time)
357 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
358 result = (*itt).comment();
359 m_snapMarkers.erase(itt);
365 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
367 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
369 while (itt != m_snapMarkers.end()) {
370 if ((*itt).time() == time)
378 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
381 for (it = 0; it < m_snapMarkers.count(); it++) {
382 if (m_snapMarkers.at(it).time() >= currTime)
385 if (it == 0) return GenTime();
386 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
387 return m_snapMarkers.at(it).time();
388 else return m_snapMarkers.at(it - 1).time();
391 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
394 for (it = 0; it < m_snapMarkers.count(); it++) {
395 if (m_snapMarkers.at(it).time() > currTime)
398 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
402 QString DocClipBase::markerComment(GenTime t)
404 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
406 while (itt != m_snapMarkers.end()) {
407 if ((*itt).time() == t)
408 return (*itt).comment();
414 void DocClipBase::clearThumbProducer()
416 if (m_thumbProd) m_thumbProd->clearProducer();
419 void DocClipBase::deleteProducers()
421 m_thumbProd->clearProducer();
423 if (numReferences() > 0) {
424 // Clip is used in timeline, delay producers deletion
425 if (m_videoOnlyProducer) m_toDeleteProducers.append(m_videoOnlyProducer);
426 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
427 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
429 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
430 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
434 delete m_videoOnlyProducer;
435 qDeleteAll(m_baseTrackProducers);
436 qDeleteAll(m_audioTrackProducers);
437 m_replaceMutex.unlock();
439 m_videoOnlyProducer = NULL;
440 m_baseTrackProducers.clear();
441 m_audioTrackProducers.clear();
444 void DocClipBase::cleanupProducers()
448 kDebug()<<"----------------------------------------------------------------------------------";
449 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
450 if (m_toDeleteProducers.at(i) != NULL) {
451 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
452 if (props.ref_count() > 2) {
453 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
460 qDeleteAll(m_toDeleteProducers);
461 m_toDeleteProducers.clear();
462 m_replaceMutex.unlock();
465 bool DocClipBase::isClean() const
467 return m_toDeleteProducers.isEmpty();
470 void DocClipBase::setValid()
472 m_placeHolder = false;
475 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
477 if (producer == NULL) return;
479 QMutexLocker locker(&m_producerMutex);
480 m_replaceMutex.lock();
483 QString id = producer->get("id");
484 if (m_placeHolder || !producer->is_valid()) {
485 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
486 producer->set("markup", tmp);
487 producer->set("bgcolour", "0xff0000ff");
488 producer->set("pad", "10");
491 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
492 if (m_clipType != AUDIO) {
493 if (!id.endsWith("_audio"))
494 m_thumbProd->setProducer(producer);
496 else m_thumbProd->setProducer(producer);
498 bool updated = false;
499 if (id.contains('_')) {
500 // this is a subtrack producer, insert it at correct place
501 id = id.section('_', 1);
502 if (id.endsWith("audio")) {
503 int pos = id.section('_', 0, 0).toInt();
504 if (pos >= m_audioTrackProducers.count()) {
505 while (m_audioTrackProducers.count() - 1 < pos) {
506 m_audioTrackProducers.append(NULL);
509 if (m_audioTrackProducers.at(pos) == NULL) {
510 m_audioTrackProducers[pos] = producer;
513 else delete producer;
515 } else if (id.endsWith("video")) {
516 if (m_videoOnlyProducer == NULL) {
517 m_videoOnlyProducer = producer;
520 else delete producer;
523 int pos = id.toInt();
524 if (pos >= m_baseTrackProducers.count()) {
525 while (m_baseTrackProducers.count() - 1 < pos) {
526 m_baseTrackProducers.append(NULL);
529 if (m_baseTrackProducers.at(pos) == NULL) {
530 m_baseTrackProducers[pos] = producer;
533 else delete producer;
535 if (m_baseTrackProducers.isEmpty()) {
536 m_baseTrackProducers.append(producer);
539 else if (m_baseTrackProducers.at(0) == NULL) {
540 m_baseTrackProducers[0] = producer;
543 else delete producer;
545 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
546 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
549 static double getPixelAspect(QMap<QString, QString>& props) {
550 int width = props.value("frame_size").section('x', 0, 0).toInt();
551 int height = props.value("frame_size").section('x', 1, 1).toInt();
552 int aspectNumerator = props.value("force_aspect_num").toInt();
553 int aspectDenominator = props.value("force_aspect_den").toInt();
554 if (aspectDenominator != 0 && width != 0)
555 return double(height) * aspectNumerator / aspectDenominator / width;
560 Mlt::Producer *DocClipBase::audioProducer(int track)
562 QMutexLocker locker(&m_producerMutex);
563 if (m_audioTrackProducers.count() <= track) {
564 while (m_audioTrackProducers.count() - 1 < track) {
565 m_audioTrackProducers.append(NULL);
568 if (m_audioTrackProducers.at(track) == NULL) {
570 for (i = 0; i < m_audioTrackProducers.count(); i++)
571 if (m_audioTrackProducers.at(i) != NULL) break;
573 if (i >= m_audioTrackProducers.count()) {
574 // Could not find a valid producer for that clip
576 base = getProducer();
582 else base = m_audioTrackProducers.at(i);
583 m_audioTrackProducers[track] = cloneProducer(base);
584 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
586 return m_audioTrackProducers.at(track);
590 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
592 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
593 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
594 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
595 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
596 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
597 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
598 if (mute) prod->set("audio_index", -1);
599 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
600 if (blind) prod->set("video_index", -1);
601 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
602 prod->set("id", id.toUtf8().constData());
603 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
604 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
605 if (m_properties.contains("proxy_out")) {
606 // We have a proxy clip, make sure the proxy has same duration as original
607 prod->set("length", m_properties.value("duration").toInt());
608 prod->set("out", m_properties.value("proxy_out").toInt());
613 Mlt::Producer *DocClipBase::videoProducer()
615 QMutexLocker locker(&m_producerMutex);
616 if (m_videoOnlyProducer == NULL) {
618 for (i = 0; i < m_baseTrackProducers.count(); i++)
619 if (m_baseTrackProducers.at(i) != NULL) break;
620 if (i >= m_baseTrackProducers.count()) return NULL;
621 m_videoOnlyProducer = cloneProducer(m_baseTrackProducers.at(i));
622 adjustProducerProperties(m_videoOnlyProducer, QString(getId() + "_video"), true, false);
624 return m_videoOnlyProducer;
627 Mlt::Producer *DocClipBase::getCloneProducer()
629 Mlt::Producer *source = NULL;
630 Mlt::Producer *prod = NULL;
631 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
632 source = getProducer();
633 if (!source) return NULL;
635 if (m_clipType == COLOR) {
636 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
637 } else if (m_clipType == TEXT) {
638 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
639 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
640 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
644 QMutexLocker locker(&m_producerMutex);
645 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
646 if (m_baseTrackProducers.at(i) != NULL) {
647 source = m_baseTrackProducers.at(i);
651 if (!source) return NULL;
653 prod = cloneProducer(source);
656 adjustProducerProperties(prod, getId() + "_monitor", false, false);
661 Mlt::Producer *DocClipBase::getProducer(int track)
663 QMutexLocker locker(&m_producerMutex);
664 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
665 if (m_baseTrackProducers.count() == 0) {
668 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
669 if (m_baseTrackProducers.at(i) != NULL) {
670 return m_baseTrackProducers.at(i);
675 if (track >= m_baseTrackProducers.count()) {
676 while (m_baseTrackProducers.count() - 1 < track) {
677 m_baseTrackProducers.append(NULL);
680 if (m_baseTrackProducers.at(track) == NULL) {
682 for (i = 0; i < m_baseTrackProducers.count(); i++)
683 if (m_baseTrackProducers.at(i) != NULL) break;
685 if (i >= m_baseTrackProducers.count()) {
686 // Could not find a valid producer for that clip, check in
689 m_baseTrackProducers[track] = cloneProducer(m_baseTrackProducers.at(i));
690 adjustProducerProperties(m_baseTrackProducers.at(track), QString(getId() + '_' + QString::number(track)), false, false);
692 return m_baseTrackProducers.at(track);
696 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
698 Mlt::Producer *result = NULL;
699 QString url = QString::fromUtf8(source->get("resource"));
700 if (KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
701 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
703 if (result == NULL || !result->is_valid()) {
705 QString txt = "+" + i18n("Missing clip") + ".txt";
706 char *tmp = qstrdup(txt.toUtf8().constData());
707 result = new Mlt::Producer(*source->profile(), tmp);
709 if (result == NULL || !result->is_valid())
710 result = new Mlt::Producer(*(source->profile()), "colour:red");
712 result->set("bgcolour", "0xff0000ff");
713 result->set("pad", "10");
717 /*Mlt::Properties src_props(source->get_properties());
718 Mlt::Properties props(result->get_properties());
719 props.inherit(src_props);*/
723 void DocClipBase::setProducerProperty(const char *name, int data)
725 QMutexLocker locker(&m_producerMutex);
726 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
727 if (m_baseTrackProducers.at(i) != NULL)
728 m_baseTrackProducers[i]->set(name, data);
732 void DocClipBase::setProducerProperty(const char *name, double data)
734 QMutexLocker locker(&m_producerMutex);
735 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
736 if (m_baseTrackProducers.at(i) != NULL)
737 m_baseTrackProducers[i]->set(name, data);
741 void DocClipBase::setProducerProperty(const char *name, const char *data)
743 QMutexLocker locker(&m_producerMutex);
744 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
745 if (m_baseTrackProducers.at(i) != NULL)
746 m_baseTrackProducers[i]->set(name, data);
750 void DocClipBase::resetProducerProperty(const char *name)
752 QMutexLocker locker(&m_producerMutex);
753 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
754 if (m_baseTrackProducers.at(i) != NULL)
755 m_baseTrackProducers[i]->set(name, (const char*) NULL);
759 const char *DocClipBase::producerProperty(const char *name) const
761 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
762 if (m_baseTrackProducers.at(i) != NULL) {
763 return m_baseTrackProducers.at(i)->get(name);
770 void DocClipBase::slotRefreshProducer()
772 if (m_baseTrackProducers.count() == 0) return;
773 if (m_clipType == SLIDESHOW) {
774 setProducerProperty("ttl", getProperty("ttl").toInt());
775 //m_clipProducer->set("id", getProperty("id"));
776 if (!getProperty("animation").isEmpty()) {
777 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
779 Mlt::Filter *filter = clipService.filter(ct);
781 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
783 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
784 clipService.detach(*filter);
786 filter = clipService.filter(ct);
789 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
790 // filter does not exist, create it.
791 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
792 if (filter && filter->is_valid()) {
793 int cycle = getProperty("ttl").toInt();
794 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
795 if (!geometry.isEmpty()) {
796 if (getProperty("animation").contains("low-pass")) {
797 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
798 if (blur && blur->is_valid())
799 clipService.attach(*blur);
801 filter->set("transition.geometry", geometry.toUtf8().data());
802 filter->set("transition.cycle", cycle);
803 clipService.attach(*filter);
808 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
810 Mlt::Filter *filter = clipService.filter(0);
812 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
813 clipService.detach(*filter);
815 filter = clipService.filter(ct);
818 if (getProperty("fade") == "1") {
819 // we want a fade filter effect
820 kDebug() << "//////////// FADE WANTED";
821 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
823 Mlt::Filter *filter = clipService.filter(ct);
825 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
829 filter = clipService.filter(ct);
832 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
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);
841 // filter does not exist, create it...
842 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
843 filter->set("cycle", getProperty("ttl").toInt());
844 filter->set("duration", getProperty("luma_duration").toInt());
845 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
846 if (!getProperty("softness").isEmpty()) {
847 int soft = getProperty("softness").toInt();
848 filter->set("luma.softness", (double) soft / 100.0);
850 clipService.attach(*filter);
853 kDebug() << "//////////// FADE NOT WANTED!!!";
854 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
856 Mlt::Filter *filter = clipService.filter(0);
858 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
859 clipService.detach(*filter);
861 filter = clipService.filter(ct);
864 if (getProperty("crop") == "1") {
865 // we want a center crop filter effect
866 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
868 Mlt::Filter *filter = clipService.filter(ct);
870 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
874 filter = clipService.filter(ct);
877 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
878 // filter does not exist, create it...
879 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
880 filter->set("center", 1);
881 clipService.attach(*filter);
884 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
886 Mlt::Filter *filter = clipService.filter(0);
888 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
889 clipService.detach(*filter);
891 filter = clipService.filter(ct);
897 void DocClipBase::setProperties(QMap <QString, QString> properties)
899 // changing clip type is not allowed
900 properties.remove("type");
901 QMapIterator<QString, QString> i(properties);
902 bool refreshProducer = false;
904 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
905 QString oldProxy = m_properties.value("proxy");
906 while (i.hasNext()) {
908 setProperty(i.key(), i.value());
909 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
911 if (properties.contains("proxy")) {
912 QString value = properties.value("proxy");
913 // If value is "-", that means user manually disabled proxy on this clip
914 if (value.isEmpty() || value == "-") {
916 emit abortProxy(m_id, oldProxy);
919 emit createProxy(m_id);
922 if (refreshProducer) slotRefreshProducer();
925 void DocClipBase::setMetadata(QMap <QString, QString> properties)
927 QMapIterator<QString, QString> i(properties);
928 while (i.hasNext()) {
930 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
931 m_metadata.remove(i.key());
933 m_metadata.insert(i.key(), i.value());
938 QMap <QString, QString> DocClipBase::metadata() const
943 void DocClipBase::clearProperty(const QString &key)
945 m_properties.remove(key);
948 void DocClipBase::getFileHash(const QString &url)
950 if (m_clipType == SLIDESHOW) return;
952 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
955 //kDebug() << "SETTING HASH of" << value;
956 m_properties.insert("file_size", QString::number(file.size()));
958 * 1 MB = 1 second per 450 files (or faster)
959 * 10 MB = 9 seconds per 450 files (or faster)
961 if (file.size() > 1000000*2) {
962 fileData = file.read(1000000);
963 if (file.seek(file.size() - 1000000))
964 fileData.append(file.readAll());
966 fileData = file.readAll();
968 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
969 m_properties.insert("file_hash", QString(fileHash.toHex()));
973 bool DocClipBase::checkHash() const
975 KUrl url = fileURL();
976 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
980 QString DocClipBase::getClipHash() const
983 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
984 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
985 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
987 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
988 if (hash.isEmpty()) hash = getHash(fileURL().path());
994 void DocClipBase::setPlaceHolder(bool place)
996 m_placeHolder = place;
1000 QString DocClipBase::getHash(const QString &path)
1003 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1004 QByteArray fileData;
1005 QByteArray fileHash;
1007 * 1 MB = 1 second per 450 files (or faster)
1008 * 10 MB = 9 seconds per 450 files (or faster)
1010 if (file.size() > 1000000*2) {
1011 fileData = file.read(1000000);
1012 if (file.seek(file.size() - 1000000))
1013 fileData.append(file.readAll());
1015 fileData = file.readAll();
1017 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1022 void DocClipBase::refreshThumbUrl()
1024 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1027 void DocClipBase::setProperty(const QString &key, const QString &value)
1029 m_properties.insert(key, value);
1030 if (key == "resource") {
1032 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1033 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1034 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1035 else if (key == "colour") {
1036 setProducerProperty("colour", value.toUtf8().data());
1037 } else if (key == "templatetext") {
1038 setProducerProperty("templatetext", value.toUtf8().data());
1039 setProducerProperty("force_reload", 1);
1040 } else if (key == "xmldata") {
1041 setProducerProperty("xmldata", value.toUtf8().data());
1042 setProducerProperty("force_reload", 1);
1043 } else if (key == "force_aspect_num") {
1044 if (value.isEmpty()) {
1045 m_properties.remove("force_aspect_num");
1046 resetProducerProperty("force_aspect_ratio");
1047 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1048 } else if (key == "force_aspect_den") {
1049 if (value.isEmpty()) {
1050 m_properties.remove("force_aspect_den");
1051 resetProducerProperty("force_aspect_ratio");
1052 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1053 } else if (key == "force_fps") {
1054 if (value.isEmpty()) {
1055 m_properties.remove("force_fps");
1056 resetProducerProperty("force_fps");
1057 } else setProducerProperty("force_fps", value.toDouble());
1058 } else if (key == "force_progressive") {
1059 if (value.isEmpty()) {
1060 m_properties.remove("force_progressive");
1061 resetProducerProperty("force_progressive");
1062 } else setProducerProperty("force_progressive", value.toInt());
1063 } else if (key == "force_tff") {
1064 if (value.isEmpty()) {
1065 m_properties.remove("force_tff");
1066 resetProducerProperty("force_tff");
1067 } else setProducerProperty("force_tff", value.toInt());
1068 } else if (key == "threads") {
1069 if (value.isEmpty()) {
1070 m_properties.remove("threads");
1071 setProducerProperty("threads", 1);
1072 } else setProducerProperty("threads", value.toInt());
1073 } else if (key == "video_index") {
1074 if (value.isEmpty()) {
1075 m_properties.remove("video_index");
1076 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1077 } else setProducerProperty("video_index", value.toInt());
1078 } else if (key == "audio_index") {
1079 if (value.isEmpty()) {
1080 m_properties.remove("audio_index");
1081 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1082 } else setProducerProperty("audio_index", value.toInt());
1083 } else if (key == "force_colorspace") {
1084 if (value.isEmpty()) {
1085 m_properties.remove("force_colorspace");
1086 resetProducerProperty("force_colorspace");
1087 } else setProducerProperty("force_colorspace", value.toInt());
1088 } else if (key == "full_luma") {
1089 if (value.isEmpty()) {
1090 m_properties.remove("full_luma");
1091 resetProducerProperty("set.force_full_luma");
1092 } else setProducerProperty("set.force_full_luma", value.toInt());
1096 QMap <QString, QString> DocClipBase::properties() const
1098 return m_properties;
1101 bool DocClipBase::slotGetAudioThumbs()
1103 if (m_thumbProd == NULL || isPlaceHolder()) return false;
1104 if (!KdenliveSettings::audiothumbnails() || m_audioTimer == NULL) {
1105 if (m_audioTimer != NULL) m_audioTimer->stop();
1108 if (m_audioThumbCreated) {
1109 m_audioTimer->stop();
1112 m_audioTimer->start(1500);
1113 double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
1114 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
1118 bool DocClipBase::isPlaceHolder() const
1120 return m_placeHolder;
1123 void DocClipBase::addCutZone(int in, int out, QString desc)
1126 info.zone = QPoint(in, out);
1127 info.description = desc;
1128 for (int i = 0; i < m_cutZones.count(); i++)
1129 if (m_cutZones.at(i).zone == info.zone) {
1132 m_cutZones.append(info);
1135 bool DocClipBase::hasCutZone(QPoint p) const
1137 for (int i = 0; i < m_cutZones.count(); i++)
1138 if (m_cutZones.at(i).zone == p) return true;
1143 void DocClipBase::removeCutZone(int in, int out)
1146 for (int i = 0; i < m_cutZones.count(); i++) {
1147 if (m_cutZones.at(i).zone == p) {
1148 m_cutZones.removeAt(i);
1154 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1156 QPoint old(oldin, oldout);
1157 for (int i = 0; i < m_cutZones.size(); ++i) {
1158 if (m_cutZones.at(i).zone == old) {
1160 info.zone = QPoint(in, out);
1161 info.description = desc;
1162 m_cutZones.replace(i, info);
1168 QList <CutZoneInfo> DocClipBase::cutZones() const
1173 bool DocClipBase::hasVideoCodec(const QString &codec) const
1175 Mlt::Producer *prod = NULL;
1176 if (m_baseTrackProducers.count() == 0) return false;
1177 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1178 if (m_baseTrackProducers.at(i) != NULL) {
1179 prod = m_baseTrackProducers.at(i);
1184 if (!prod) return false;
1185 int default_video = prod->get_int("video_index");
1187 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1188 return prod->get(property) == codec;
1191 bool DocClipBase::hasAudioCodec(const QString &codec) const
1193 Mlt::Producer *prod = NULL;
1194 if (m_baseTrackProducers.count() == 0) return false;
1195 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1196 if (m_baseTrackProducers.at(i) != NULL) {
1197 prod = m_baseTrackProducers.at(i);
1201 if (!prod) return false;
1202 int default_video = prod->get_int("audio_index");
1204 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1205 return prod->get(property) == codec;
1209 void DocClipBase::slotExtractImage(int frame, int frame2)
1211 if (m_thumbProd == NULL) return;
1212 m_thumbProd->extractImage(frame, frame2);
1215 QPixmap DocClipBase::extractImage(int frame, int width, int height)
1217 if (m_thumbProd == NULL) return QPixmap(width, height);
1218 QMutexLocker locker(&m_producerMutex);
1219 QPixmap p = m_thumbProd->extractImage(frame, width, height);