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_toDeleteProducers);
102 m_toDeleteProducers.clear();
103 qDeleteAll(m_baseTrackProducers);
104 m_baseTrackProducers.clear();
105 qDeleteAll(m_audioTrackProducers);
106 m_audioTrackProducers.clear();
107 delete m_videoOnlyProducer;
108 m_videoOnlyProducer = NULL;
111 void DocClipBase::setZone(QPoint zone)
113 m_properties.insert("zone_in", QString::number(zone.x()));
114 m_properties.insert("zone_out", QString::number(zone.y()));
117 QPoint DocClipBase::zone() const
119 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
123 void DocClipBase::slotCreateAudioTimer()
125 connect(m_thumbProd, SIGNAL(audioThumbReady(const audioByteArray&)), this , SLOT(updateAudioThumbnail(const audioByteArray&)));
126 m_audioTimer = new QTimer(this);
127 connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
130 void DocClipBase::askForAudioThumbs()
132 if (m_thumbProd && m_audioTimer) m_thumbProd->askForAudioThumbs(getId());
135 void DocClipBase::slotClearAudioCache()
137 if (m_thumbProd) m_thumbProd->stopAudioThumbs();
138 if (m_audioTimer != NULL) m_audioTimer->stop();
139 m_audioFrameCache.clear();
140 m_audioThumbCreated = false;
143 /*void DocClipBase::getClipMainThumb() {
144 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
147 KThumb *DocClipBase::thumbProducer()
152 bool DocClipBase::audioThumbCreated() const
154 return m_audioThumbCreated;
157 const QString DocClipBase::name() const
160 return m_properties.value("name");
163 const QString &DocClipBase::getId() const
168 const CLIPTYPE & DocClipBase::clipType() const
173 void DocClipBase::setClipType(CLIPTYPE type)
177 m_properties.insert("type", QString::number((int) type));
178 if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO || m_clipType == PLAYLIST))
179 slotCreateAudioTimer();
182 KUrl DocClipBase::fileURL() const
184 QString res = m_properties.value("resource");
185 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
189 void DocClipBase::setClipThumbFrame(const uint &ix)
191 m_properties.insert("thumbnail", QString::number((int) ix));
194 uint DocClipBase::getClipThumbFrame() const
196 return (uint) m_properties.value("thumbnail").toInt();
199 const QString DocClipBase::description() const
201 return m_properties.value("description");
204 bool DocClipBase::isTransparent() const
206 return (m_properties.value("transparency") == "1");
209 const QString DocClipBase::getProperty(const QString &prop) const
211 return m_properties.value(prop);
214 void DocClipBase::setDuration(GenTime dur)
217 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
220 const GenTime &DocClipBase::duration() const
225 const GenTime DocClipBase::maxDuration() const
227 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
228 /*const GenTime dur(15000, KdenliveSettings::project_fps());
235 bool DocClipBase::hasFileSize() const
240 qulonglong DocClipBase::fileSize() const
242 return m_properties.value("file_size").toULongLong();
246 QDomElement DocClipBase::toXML() const
249 QDomElement clip = doc.createElement("producer");
251 QMapIterator<QString, QString> i(m_properties);
252 while (i.hasNext()) {
254 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
256 doc.appendChild(clip);
257 if (!m_cutZones.isEmpty()) {
259 for (int i = 0; i < m_cutZones.size(); i++) {
260 CutZoneInfo info = m_cutZones.at(i);
261 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
263 clip.setAttribute("cutzones", cuts.join(";"));
265 //kDebug() << "/// CLIP XML: " << doc.toString();
266 return doc.documentElement();
270 void DocClipBase::setAudioThumbCreated(bool isDone)
272 m_audioThumbCreated = isDone;
276 void DocClipBase::setThumbnail(const QPixmap & pixmap)
278 m_thumbnail = pixmap;
281 const QPixmap & DocClipBase::thumbnail() const
286 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
288 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
289 m_audioFrameCache = data;
290 m_audioThumbCreated = true;
294 QList < GenTime > DocClipBase::snapMarkers() const
296 QList < GenTime > markers;
298 for (int count = 0; count < m_snapMarkers.count(); ++count) {
299 markers.append(m_snapMarkers.at(count).time());
305 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
307 return m_snapMarkers;
311 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
313 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
314 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
315 if ((*it).time() >= time)
319 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
320 (*it).setComment(comment);
321 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
323 CommentedTime t(time, comment);
324 m_snapMarkers.insert(it, t);
329 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
331 QList < CommentedTime >::Iterator it;
332 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
333 if ((*it).time() == time)
336 if (it != m_snapMarkers.end()) {
337 (*it).setComment(comment);
339 kError() << "trying to edit Snap Marker that does not already exists";
343 QString DocClipBase::deleteSnapMarker(const GenTime & time)
345 QString result = i18n("Marker");
346 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
348 while (itt != m_snapMarkers.end()) {
349 if ((*itt).time() == time)
354 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
355 result = (*itt).comment();
356 m_snapMarkers.erase(itt);
362 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
364 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
366 while (itt != m_snapMarkers.end()) {
367 if ((*itt).time() == time)
375 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
378 for (it = 0; it < m_snapMarkers.count(); it++) {
379 if (m_snapMarkers.at(it).time() >= currTime)
382 if (it == 0) return GenTime();
383 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
384 return m_snapMarkers.at(it).time();
385 else return m_snapMarkers.at(it - 1).time();
388 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
391 for (it = 0; it < m_snapMarkers.count(); it++) {
392 if (m_snapMarkers.at(it).time() > currTime)
395 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
399 QString DocClipBase::markerComment(GenTime t)
401 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
403 while (itt != m_snapMarkers.end()) {
404 if ((*itt).time() == t)
405 return (*itt).comment();
411 void DocClipBase::clearThumbProducer()
413 if (m_thumbProd) m_thumbProd->clearProducer();
416 void DocClipBase::deleteProducers()
418 if (m_thumbProd) m_thumbProd->clearProducer();
420 if (numReferences() > 0) {
421 // Clip is used in timeline, delay producers deletion
422 if (m_videoOnlyProducer) m_toDeleteProducers.append(m_videoOnlyProducer);
423 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
424 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
426 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
427 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
431 delete m_videoOnlyProducer;
432 qDeleteAll(m_baseTrackProducers);
433 qDeleteAll(m_audioTrackProducers);
434 m_replaceMutex.unlock();
436 m_videoOnlyProducer = NULL;
437 m_baseTrackProducers.clear();
438 m_audioTrackProducers.clear();
441 void DocClipBase::cleanupProducers()
445 kDebug()<<"----------------------------------------------------------------------------------";
446 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
447 if (m_toDeleteProducers.at(i) != NULL) {
448 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
449 if (props.ref_count() > 2) {
450 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
457 qDeleteAll(m_toDeleteProducers);
458 m_toDeleteProducers.clear();
459 m_replaceMutex.unlock();
462 bool DocClipBase::isClean() const
464 return m_toDeleteProducers.isEmpty();
467 void DocClipBase::setValid()
469 m_placeHolder = false;
472 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
474 if (producer == NULL) return;
476 QMutexLocker locker(&m_producerMutex);
477 m_replaceMutex.lock();
480 QString id = producer->get("id");
481 if (m_placeHolder || !producer->is_valid()) {
482 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
483 producer->set("markup", tmp);
484 producer->set("bgcolour", "0xff0000ff");
485 producer->set("pad", "10");
488 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
489 if (m_clipType != AUDIO) {
490 if (!id.endsWith("_audio"))
491 m_thumbProd->setProducer(producer);
493 else m_thumbProd->setProducer(producer);
495 bool updated = false;
496 if (id.contains('_')) {
497 // this is a subtrack producer, insert it at correct place
498 id = id.section('_', 1);
499 if (id.endsWith("audio")) {
500 int pos = id.section('_', 0, 0).toInt();
501 if (pos >= m_audioTrackProducers.count()) {
502 while (m_audioTrackProducers.count() - 1 < pos) {
503 m_audioTrackProducers.append(NULL);
506 if (m_audioTrackProducers.at(pos) == NULL) {
507 m_audioTrackProducers[pos] = producer;
510 else delete producer;
512 } else if (id.endsWith("video")) {
513 if (m_videoOnlyProducer == NULL) {
514 m_videoOnlyProducer = producer;
517 else delete producer;
520 int pos = id.toInt();
521 if (pos >= m_baseTrackProducers.count()) {
522 while (m_baseTrackProducers.count() - 1 < pos) {
523 m_baseTrackProducers.append(NULL);
526 if (m_baseTrackProducers.at(pos) == NULL) {
527 m_baseTrackProducers[pos] = producer;
530 else delete producer;
532 if (m_baseTrackProducers.isEmpty()) {
533 m_baseTrackProducers.append(producer);
536 else if (m_baseTrackProducers.at(0) == NULL) {
537 m_baseTrackProducers[0] = producer;
540 else delete producer;
542 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
543 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
546 static double getPixelAspect(QMap<QString, QString>& props) {
547 int width = props.value("frame_size").section('x', 0, 0).toInt();
548 int height = props.value("frame_size").section('x', 1, 1).toInt();
549 int aspectNumerator = props.value("force_aspect_num").toInt();
550 int aspectDenominator = props.value("force_aspect_den").toInt();
551 if (aspectDenominator != 0 && width != 0)
552 return double(height) * aspectNumerator / aspectDenominator / width;
557 Mlt::Producer *DocClipBase::audioProducer(int track)
559 QMutexLocker locker(&m_producerMutex);
560 if (m_audioTrackProducers.count() <= track) {
561 while (m_audioTrackProducers.count() - 1 < track) {
562 m_audioTrackProducers.append(NULL);
565 if (m_audioTrackProducers.at(track) == NULL) {
567 for (i = 0; i < m_audioTrackProducers.count(); i++)
568 if (m_audioTrackProducers.at(i) != NULL) break;
570 if (i >= m_audioTrackProducers.count()) {
571 // Could not find a valid producer for that clip
573 base = getProducer();
579 else base = m_audioTrackProducers.at(i);
580 m_audioTrackProducers[track] = cloneProducer(base);
581 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
583 return m_audioTrackProducers.at(track);
587 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
589 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
590 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
591 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
592 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
593 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
594 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
595 if (mute) prod->set("audio_index", -1);
596 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
597 if (blind) prod->set("video_index", -1);
598 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
599 prod->set("id", id.toUtf8().constData());
600 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
601 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
602 if (m_properties.contains("proxy_out")) {
603 // We have a proxy clip, make sure the proxy has same duration as original
604 prod->set("length", m_properties.value("duration").toInt());
605 prod->set("out", m_properties.value("proxy_out").toInt());
610 Mlt::Producer *DocClipBase::videoProducer()
612 QMutexLocker locker(&m_producerMutex);
613 if (m_videoOnlyProducer == NULL) {
615 for (i = 0; i < m_baseTrackProducers.count(); i++)
616 if (m_baseTrackProducers.at(i) != NULL) break;
617 if (i >= m_baseTrackProducers.count()) return NULL;
618 m_videoOnlyProducer = cloneProducer(m_baseTrackProducers.at(i));
619 adjustProducerProperties(m_videoOnlyProducer, QString(getId() + "_video"), true, false);
621 return m_videoOnlyProducer;
624 Mlt::Producer *DocClipBase::getCloneProducer()
626 Mlt::Producer *source = NULL;
627 Mlt::Producer *prod = NULL;
628 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
629 source = getProducer();
630 if (!source) return NULL;
632 if (m_clipType == COLOR) {
633 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
634 } else if (m_clipType == TEXT) {
635 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
636 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
637 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
641 QMutexLocker locker(&m_producerMutex);
642 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
643 if (m_baseTrackProducers.at(i) != NULL) {
644 source = m_baseTrackProducers.at(i);
648 if (!source) return NULL;
650 prod = cloneProducer(source);
653 adjustProducerProperties(prod, getId() + "_", false, false);
654 if (!m_properties.contains("proxy_out")) {
655 // Adjust length in case...
656 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
657 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
663 Mlt::Producer *DocClipBase::getProducer(int track)
665 QMutexLocker locker(&m_producerMutex);
666 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
667 if (m_baseTrackProducers.count() == 0) {
670 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
671 if (m_baseTrackProducers.at(i) != NULL) {
672 return m_baseTrackProducers.at(i);
677 if (track >= m_baseTrackProducers.count()) {
678 while (m_baseTrackProducers.count() - 1 < track) {
679 m_baseTrackProducers.append(NULL);
682 if (m_baseTrackProducers.at(track) == NULL) {
684 for (i = 0; i < m_baseTrackProducers.count(); i++)
685 if (m_baseTrackProducers.at(i) != NULL) break;
687 if (i >= m_baseTrackProducers.count()) {
688 // Could not find a valid producer for that clip, check in
691 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
692 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
693 m_baseTrackProducers[track] = prod;
695 return m_baseTrackProducers.at(track);
699 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
701 Mlt::Producer *result = NULL;
702 QString url = QString::fromUtf8(source->get("resource"));
703 if (KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
704 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
706 if (result == NULL || !result->is_valid()) {
708 QString txt = "+" + i18n("Missing clip") + ".txt";
709 char *tmp = qstrdup(txt.toUtf8().constData());
710 result = new Mlt::Producer(*source->profile(), tmp);
712 if (result == NULL || !result->is_valid())
713 result = new Mlt::Producer(*(source->profile()), "colour:red");
715 result->set("bgcolour", "0xff0000ff");
716 result->set("pad", "10");
720 /*Mlt::Properties src_props(source->get_properties());
721 Mlt::Properties props(result->get_properties());
722 props.inherit(src_props);*/
726 void DocClipBase::setProducerProperty(const char *name, int data)
728 QMutexLocker locker(&m_producerMutex);
729 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
730 if (m_baseTrackProducers.at(i) != NULL)
731 m_baseTrackProducers[i]->set(name, data);
735 void DocClipBase::setProducerProperty(const char *name, double data)
737 QMutexLocker locker(&m_producerMutex);
738 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
739 if (m_baseTrackProducers.at(i) != NULL)
740 m_baseTrackProducers[i]->set(name, data);
744 void DocClipBase::setProducerProperty(const char *name, const char *data)
746 QMutexLocker locker(&m_producerMutex);
747 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
748 if (m_baseTrackProducers.at(i) != NULL)
749 m_baseTrackProducers[i]->set(name, data);
753 void DocClipBase::resetProducerProperty(const char *name)
755 QMutexLocker locker(&m_producerMutex);
756 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
757 if (m_baseTrackProducers.at(i) != NULL)
758 m_baseTrackProducers[i]->set(name, (const char*) NULL);
762 const char *DocClipBase::producerProperty(const char *name) const
764 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
765 if (m_baseTrackProducers.at(i) != NULL) {
766 return m_baseTrackProducers.at(i)->get(name);
773 void DocClipBase::slotRefreshProducer()
775 if (m_baseTrackProducers.count() == 0) return;
776 if (m_clipType == SLIDESHOW) {
777 setProducerProperty("ttl", getProperty("ttl").toInt());
778 //m_clipProducer->set("id", getProperty("id"));
779 if (!getProperty("animation").isEmpty()) {
780 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
782 Mlt::Filter *filter = clipService.filter(ct);
784 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
786 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
787 clipService.detach(*filter);
789 filter = clipService.filter(ct);
792 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
793 // filter does not exist, create it.
794 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
795 if (filter && filter->is_valid()) {
796 int cycle = getProperty("ttl").toInt();
797 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
798 if (!geometry.isEmpty()) {
799 if (getProperty("animation").contains("low-pass")) {
800 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
801 if (blur && blur->is_valid())
802 clipService.attach(*blur);
804 filter->set("transition.geometry", geometry.toUtf8().data());
805 filter->set("transition.cycle", cycle);
806 clipService.attach(*filter);
811 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
813 Mlt::Filter *filter = clipService.filter(0);
815 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
816 clipService.detach(*filter);
818 filter = clipService.filter(ct);
821 if (getProperty("fade") == "1") {
822 // we want a fade filter effect
823 kDebug() << "//////////// FADE WANTED";
824 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
826 Mlt::Filter *filter = clipService.filter(ct);
828 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
832 filter = clipService.filter(ct);
835 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
836 filter->set("cycle", getProperty("ttl").toInt());
837 filter->set("duration", getProperty("luma_duration").toInt());
838 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
839 if (!getProperty("softness").isEmpty()) {
840 int soft = getProperty("softness").toInt();
841 filter->set("luma.softness", (double) soft / 100.0);
844 // filter does not exist, create it...
845 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
846 filter->set("cycle", getProperty("ttl").toInt());
847 filter->set("duration", getProperty("luma_duration").toInt());
848 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
849 if (!getProperty("softness").isEmpty()) {
850 int soft = getProperty("softness").toInt();
851 filter->set("luma.softness", (double) soft / 100.0);
853 clipService.attach(*filter);
856 kDebug() << "//////////// FADE NOT WANTED!!!";
857 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
859 Mlt::Filter *filter = clipService.filter(0);
861 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
862 clipService.detach(*filter);
864 filter = clipService.filter(ct);
867 if (getProperty("crop") == "1") {
868 // we want a center crop filter effect
869 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
871 Mlt::Filter *filter = clipService.filter(ct);
873 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
877 filter = clipService.filter(ct);
880 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
881 // filter does not exist, create it...
882 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
883 filter->set("center", 1);
884 clipService.attach(*filter);
887 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
889 Mlt::Filter *filter = clipService.filter(0);
891 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
892 clipService.detach(*filter);
894 filter = clipService.filter(ct);
900 void DocClipBase::setProperties(QMap <QString, QString> properties)
902 // changing clip type is not allowed
903 properties.remove("type");
904 QMapIterator<QString, QString> i(properties);
905 bool refreshProducer = false;
907 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
908 QString oldProxy = m_properties.value("proxy");
909 while (i.hasNext()) {
911 setProperty(i.key(), i.value());
912 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
914 if (properties.contains("proxy")) {
915 QString value = properties.value("proxy");
916 // If value is "-", that means user manually disabled proxy on this clip
917 if (value.isEmpty() || value == "-") {
919 emit abortProxy(m_id, oldProxy);
922 emit createProxy(m_id);
925 if (refreshProducer) slotRefreshProducer();
928 void DocClipBase::setMetadata(QMap <QString, QString> properties)
930 QMapIterator<QString, QString> i(properties);
931 while (i.hasNext()) {
933 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
934 m_metadata.remove(i.key());
936 m_metadata.insert(i.key(), i.value());
941 QMap <QString, QString> DocClipBase::metadata() const
946 void DocClipBase::clearProperty(const QString &key)
948 m_properties.remove(key);
951 void DocClipBase::getFileHash(const QString &url)
953 if (m_clipType == SLIDESHOW) return;
955 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
958 //kDebug() << "SETTING HASH of" << value;
959 m_properties.insert("file_size", QString::number(file.size()));
961 * 1 MB = 1 second per 450 files (or faster)
962 * 10 MB = 9 seconds per 450 files (or faster)
964 if (file.size() > 1000000*2) {
965 fileData = file.read(1000000);
966 if (file.seek(file.size() - 1000000))
967 fileData.append(file.readAll());
969 fileData = file.readAll();
971 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
972 m_properties.insert("file_hash", QString(fileHash.toHex()));
976 bool DocClipBase::checkHash() const
978 KUrl url = fileURL();
979 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
983 QString DocClipBase::getClipHash() const
986 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
987 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
988 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
990 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
991 if (hash.isEmpty()) hash = getHash(fileURL().path());
997 void DocClipBase::setPlaceHolder(bool place)
999 m_placeHolder = place;
1003 QString DocClipBase::getHash(const QString &path)
1006 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1007 QByteArray fileData;
1008 QByteArray fileHash;
1010 * 1 MB = 1 second per 450 files (or faster)
1011 * 10 MB = 9 seconds per 450 files (or faster)
1013 if (file.size() > 1000000*2) {
1014 fileData = file.read(1000000);
1015 if (file.seek(file.size() - 1000000))
1016 fileData.append(file.readAll());
1018 fileData = file.readAll();
1020 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1025 void DocClipBase::refreshThumbUrl()
1027 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1030 void DocClipBase::setProperty(const QString &key, const QString &value)
1032 m_properties.insert(key, value);
1033 if (key == "resource") {
1035 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1036 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1037 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1038 else if (key == "colour") {
1039 setProducerProperty("colour", value.toUtf8().data());
1040 } else if (key == "templatetext") {
1041 setProducerProperty("templatetext", value.toUtf8().data());
1042 setProducerProperty("force_reload", 1);
1043 } else if (key == "xmldata") {
1044 setProducerProperty("xmldata", value.toUtf8().data());
1045 setProducerProperty("force_reload", 1);
1046 } else if (key == "force_aspect_num") {
1047 if (value.isEmpty()) {
1048 m_properties.remove("force_aspect_num");
1049 resetProducerProperty("force_aspect_ratio");
1050 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1051 } else if (key == "force_aspect_den") {
1052 if (value.isEmpty()) {
1053 m_properties.remove("force_aspect_den");
1054 resetProducerProperty("force_aspect_ratio");
1055 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1056 } else if (key == "force_fps") {
1057 if (value.isEmpty()) {
1058 m_properties.remove("force_fps");
1059 resetProducerProperty("force_fps");
1060 } else setProducerProperty("force_fps", value.toDouble());
1061 } else if (key == "force_progressive") {
1062 if (value.isEmpty()) {
1063 m_properties.remove("force_progressive");
1064 resetProducerProperty("force_progressive");
1065 } else setProducerProperty("force_progressive", value.toInt());
1066 } else if (key == "force_tff") {
1067 if (value.isEmpty()) {
1068 m_properties.remove("force_tff");
1069 resetProducerProperty("force_tff");
1070 } else setProducerProperty("force_tff", value.toInt());
1071 } else if (key == "threads") {
1072 if (value.isEmpty()) {
1073 m_properties.remove("threads");
1074 setProducerProperty("threads", 1);
1075 } else setProducerProperty("threads", value.toInt());
1076 } else if (key == "video_index") {
1077 if (value.isEmpty()) {
1078 m_properties.remove("video_index");
1079 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1080 } else setProducerProperty("video_index", value.toInt());
1081 } else if (key == "audio_index") {
1082 if (value.isEmpty()) {
1083 m_properties.remove("audio_index");
1084 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1085 } else setProducerProperty("audio_index", value.toInt());
1086 } else if (key == "force_colorspace") {
1087 if (value.isEmpty()) {
1088 m_properties.remove("force_colorspace");
1089 resetProducerProperty("force_colorspace");
1090 } else setProducerProperty("force_colorspace", value.toInt());
1091 } else if (key == "full_luma") {
1092 if (value.isEmpty()) {
1093 m_properties.remove("full_luma");
1094 resetProducerProperty("set.force_full_luma");
1095 } else setProducerProperty("set.force_full_luma", value.toInt());
1099 QMap <QString, QString> DocClipBase::properties() const
1101 return m_properties;
1104 bool DocClipBase::slotGetAudioThumbs()
1106 if (m_thumbProd == NULL || isPlaceHolder()) return false;
1107 if (!KdenliveSettings::audiothumbnails() || m_audioTimer == NULL) {
1108 if (m_audioTimer != NULL) m_audioTimer->stop();
1111 if (m_audioThumbCreated) {
1112 m_audioTimer->stop();
1115 m_audioTimer->start(1500);
1116 double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
1117 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
1121 bool DocClipBase::isPlaceHolder() const
1123 return m_placeHolder;
1126 void DocClipBase::addCutZone(int in, int out, QString desc)
1129 info.zone = QPoint(in, out);
1130 info.description = desc;
1131 for (int i = 0; i < m_cutZones.count(); i++)
1132 if (m_cutZones.at(i).zone == info.zone) {
1135 m_cutZones.append(info);
1138 bool DocClipBase::hasCutZone(QPoint p) const
1140 for (int i = 0; i < m_cutZones.count(); i++)
1141 if (m_cutZones.at(i).zone == p) return true;
1146 void DocClipBase::removeCutZone(int in, int out)
1149 for (int i = 0; i < m_cutZones.count(); i++) {
1150 if (m_cutZones.at(i).zone == p) {
1151 m_cutZones.removeAt(i);
1157 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1159 QPoint old(oldin, oldout);
1160 for (int i = 0; i < m_cutZones.size(); ++i) {
1161 if (m_cutZones.at(i).zone == old) {
1163 info.zone = QPoint(in, out);
1164 info.description = desc;
1165 m_cutZones.replace(i, info);
1171 QList <CutZoneInfo> DocClipBase::cutZones() const
1176 bool DocClipBase::hasVideoCodec(const QString &codec) const
1178 Mlt::Producer *prod = NULL;
1179 if (m_baseTrackProducers.count() == 0) return false;
1180 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1181 if (m_baseTrackProducers.at(i) != NULL) {
1182 prod = m_baseTrackProducers.at(i);
1187 if (!prod) return false;
1188 int default_video = prod->get_int("video_index");
1190 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1191 return prod->get(property) == codec;
1194 bool DocClipBase::hasAudioCodec(const QString &codec) const
1196 Mlt::Producer *prod = NULL;
1197 if (m_baseTrackProducers.count() == 0) return false;
1198 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1199 if (m_baseTrackProducers.at(i) != NULL) {
1200 prod = m_baseTrackProducers.at(i);
1204 if (!prod) return false;
1205 int default_video = prod->get_int("audio_index");
1207 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1208 return prod->get(property) == codec;
1212 void DocClipBase::slotExtractImage(QList <int> frames)
1214 if (m_thumbProd == NULL) return;
1215 m_thumbProd->extractImage(frames);
1218 QPixmap DocClipBase::extractImage(int frame, int width, int height)
1220 if (m_thumbProd == NULL) return QPixmap(width, height);
1221 QMutexLocker locker(&m_producerMutex);
1222 QPixmap p = m_thumbProd->extractImage(frame, width, height);