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>
36 #include <QCryptographicHash>
37 #include <QtConcurrentRun>
41 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id) :
45 m_baseTrackProducers(),
46 m_audioTrackProducers(),
47 m_videoOnlyProducer(NULL),
48 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 //if (!url.isEmpty() && QFile::exists(url.path()))
91 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
92 if (m_clipType == AV || m_clipType == AUDIO || m_clipType == PLAYLIST) slotCreateAudioTimer();
94 //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
97 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
98 DocClipBase::operator=(clip);
100 m_clipType = clip.clipType();
101 m_name = clip.name();
102 m_duration = clip.duration();
103 m_audioThumbCreated = clip.audioThumbCreated();
104 m_properties = clip.properties();
108 DocClipBase::~DocClipBase()
110 kDebug() << "CLIP " << m_id << " DELETED******************************";
113 m_audioTimer->stop();
116 /*kDebug() <<" * * *CNT "<<m_baseTrackProducers.count();
117 if (m_baseTrackProducers.count() > 0) kDebug()<<"YOYO: "<<m_baseTrackProducers.at(0)->get_out()<<", CUT: "<<m_baseTrackProducers.at(0)->is_cut();*/
118 qDeleteAll(m_baseTrackProducers);
119 m_baseTrackProducers.clear();
120 qDeleteAll(m_audioTrackProducers);
121 m_audioTrackProducers.clear();
122 delete m_videoOnlyProducer;
123 m_videoOnlyProducer = NULL;
126 void DocClipBase::setZone(QPoint zone)
128 m_properties.insert("zone_in", QString::number(zone.x()));
129 m_properties.insert("zone_out", QString::number(zone.y()));
132 QPoint DocClipBase::zone() const
134 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
138 void DocClipBase::slotCreateAudioTimer()
140 connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
141 m_audioTimer = new QTimer(this);
142 connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
145 void DocClipBase::askForAudioThumbs()
147 if (m_thumbProd && m_audioTimer) m_thumbProd->askForAudioThumbs(getId());
150 void DocClipBase::slotClearAudioCache()
152 if (m_thumbProd) m_thumbProd->stopAudioThumbs();
153 if (m_audioTimer != NULL) m_audioTimer->stop();
154 m_audioFrameCache.clear();
155 m_audioThumbCreated = false;
158 /*void DocClipBase::getClipMainThumb() {
159 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
162 KThumb *DocClipBase::thumbProducer()
167 bool DocClipBase::audioThumbCreated() const
169 return m_audioThumbCreated;
172 const QString DocClipBase::name() const
175 return m_properties.value("name");
178 const QString &DocClipBase::getId() const
183 void DocClipBase::setId(const QString &newId)
188 const CLIPTYPE & DocClipBase::clipType() const
193 void DocClipBase::setClipType(CLIPTYPE type)
197 m_properties.insert("type", QString::number((int) type));
198 if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO || m_clipType == PLAYLIST))
199 slotCreateAudioTimer();
202 KUrl DocClipBase::fileURL() const
204 QString res = m_properties.value("resource");
205 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
209 void DocClipBase::setClipThumbFrame(const uint &ix)
211 m_properties.insert("thumbnail", QString::number((int) ix));
214 uint DocClipBase::getClipThumbFrame() const
216 return (uint) m_properties.value("thumbnail").toInt();
219 const QString DocClipBase::description() const
221 return m_properties.value("description");
224 bool DocClipBase::isTransparent() const
226 return (m_properties.value("transparency") == "1");
229 const QString DocClipBase::getProperty(const QString prop) const
231 return m_properties.value(prop);
234 void DocClipBase::setDuration(GenTime dur)
237 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
240 const GenTime &DocClipBase::duration() const
245 const GenTime DocClipBase::maxDuration() const
247 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
248 /*const GenTime dur(15000, KdenliveSettings::project_fps());
255 bool DocClipBase::hasFileSize() const
260 qulonglong DocClipBase::fileSize() const
262 return m_properties.value("file_size").toULongLong();
266 QDomElement DocClipBase::toXML() const
269 QDomElement clip = doc.createElement("producer");
271 QMapIterator<QString, QString> i(m_properties);
272 while (i.hasNext()) {
274 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
276 doc.appendChild(clip);
277 if (!m_cutZones.isEmpty()) {
279 for (int i = 0; i < m_cutZones.size(); i++) {
280 CutZoneInfo info = m_cutZones.at(i);
281 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
283 clip.setAttribute("cutzones", cuts.join(";"));
285 //kDebug() << "/// CLIP XML: " << doc.toString();
286 return doc.documentElement();
290 void DocClipBase::setAudioThumbCreated(bool isDone)
292 m_audioThumbCreated = isDone;
296 void DocClipBase::setThumbnail(const QPixmap & pixmap)
298 m_thumbnail = pixmap;
301 const QPixmap & DocClipBase::thumbnail() const
306 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data)
308 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
309 m_audioFrameCache = data;
310 m_audioThumbCreated = true;
314 QList < GenTime > DocClipBase::snapMarkers() const
316 QList < GenTime > markers;
318 for (int count = 0; count < m_snapMarkers.count(); ++count) {
319 markers.append(m_snapMarkers.at(count).time());
325 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
327 return m_snapMarkers;
331 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
333 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
334 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
335 if ((*it).time() >= time)
339 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
340 (*it).setComment(comment);
341 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
343 CommentedTime t(time, comment);
344 m_snapMarkers.insert(it, t);
349 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
351 QList < CommentedTime >::Iterator it;
352 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
353 if ((*it).time() == time)
356 if (it != m_snapMarkers.end()) {
357 (*it).setComment(comment);
359 kError() << "trying to edit Snap Marker that does not already exists";
363 QString DocClipBase::deleteSnapMarker(const GenTime & time)
365 QString result = i18n("Marker");
366 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
368 while (itt != m_snapMarkers.end()) {
369 if ((*itt).time() == time)
374 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
375 result = (*itt).comment();
376 m_snapMarkers.erase(itt);
382 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
384 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
386 while (itt != m_snapMarkers.end()) {
387 if ((*itt).time() == time)
395 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
398 for (it = 0; it < m_snapMarkers.count(); it++) {
399 if (m_snapMarkers.at(it).time() >= currTime)
402 if (it == 0) return GenTime();
403 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
404 return m_snapMarkers.at(it).time();
405 else return m_snapMarkers.at(it - 1).time();
408 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
411 for (it = 0; it < m_snapMarkers.count(); it++) {
412 if (m_snapMarkers.at(it).time() > currTime)
415 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
419 QString DocClipBase::markerComment(GenTime t)
421 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
423 while (itt != m_snapMarkers.end()) {
424 if ((*itt).time() == t)
425 return (*itt).comment();
431 void DocClipBase::clearProducers()
433 m_baseTrackProducers.clear();
436 void DocClipBase::deleteProducers(bool clearThumbCreator)
438 kDebug() << "// CLIP KILL PRODS ct: " << m_baseTrackProducers.count();
439 if (clearThumbCreator && m_thumbProd) m_thumbProd->clearProducer();
440 /*kDebug()<<"// CLIP KILL PRODS ct: "<<m_baseTrackProducers.count();
441 int max = m_baseTrackProducers.count();
442 for (int i = 0; i < max; i++) {
443 kDebug()<<"// CLIP KILL PROD "<<i;
444 Mlt::Producer *p = m_baseTrackProducers.takeAt(i);
449 m_baseTrackProducers.insert(i, NULL);
452 delete m_videoOnlyProducer;
453 m_videoOnlyProducer = NULL;
455 qDeleteAll(m_baseTrackProducers);
456 m_baseTrackProducers.clear();
457 qDeleteAll(m_audioTrackProducers);
458 m_audioTrackProducers.clear();
461 void DocClipBase::setValid()
463 m_placeHolder = false;
466 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset)
468 if (producer == NULL || (m_placeHolder && !reset)) return;
469 if (m_thumbProd && (reset || !m_thumbProd->hasProducer())) m_thumbProd->setProducer(producer);
471 // Clear all previous producers
472 kDebug() << "/+++++++++++++++ DELETE ALL PRODS " << producer->get("id");
473 deleteProducers(false);
475 QString id = producer->get("id");
476 if (id.contains('_')) {
477 // this is a subtrack producer, insert it at correct place
478 id = id.section('_', 1);
479 if (id.endsWith("audio")) {
480 int pos = id.section('_', 0, 0).toInt();
481 if (pos >= m_audioTrackProducers.count()) {
482 while (m_audioTrackProducers.count() - 1 < pos) {
483 m_audioTrackProducers.append(NULL);
486 if (m_audioTrackProducers.at(pos) == NULL) m_audioTrackProducers[pos] = producer;
488 } else if (id.endsWith("video")) {
489 m_videoOnlyProducer = producer;
492 int pos = id.toInt();
493 if (pos >= m_baseTrackProducers.count()) {
494 while (m_baseTrackProducers.count() - 1 < pos) {
495 m_baseTrackProducers.append(NULL);
498 if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
500 if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
501 else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
503 //m_clipProducer = producer;
504 //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
507 static double getPixelAspect(QMap<QString, QString>& props) {
508 int width = props.value("frame_size").section('x', 0, 0).toInt();
509 int height = props.value("frame_size").section('x', 1, 1).toInt();
510 int aspectNumerator = props.value("force_aspect_num").toInt();
511 int aspectDenominator = props.value("force_aspect_den").toInt();
512 if (aspectDenominator != 0 && width != 0)
513 return double(height) * aspectNumerator / aspectDenominator / width;
518 Mlt::Producer *DocClipBase::audioProducer(int track)
520 if (m_audioTrackProducers.count() <= track) {
521 while (m_audioTrackProducers.count() - 1 < track) {
522 m_audioTrackProducers.append(NULL);
525 if (m_audioTrackProducers.at(track) == NULL) {
526 Mlt::Producer *base = producer();
527 m_audioTrackProducers[track] = new Mlt::Producer(*(base->profile()), base->get("resource"));
528 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
529 m_audioTrackProducers.at(track)->set("force_aspect_ratio", getPixelAspect(m_properties));
530 if (m_properties.contains("force_fps")) m_audioTrackProducers.at(track)->set("force_fps", m_properties.value("force_fps").toDouble());
531 if (m_properties.contains("force_progressive")) m_audioTrackProducers.at(track)->set("force_progressive", m_properties.value("force_progressive").toInt());
532 if (m_properties.contains("force_tff")) m_audioTrackProducers.at(track)->set("force_tff", m_properties.value("force_tff").toInt());
533 if (m_properties.contains("threads")) m_audioTrackProducers.at(track)->set("threads", m_properties.value("threads").toInt());
534 m_audioTrackProducers.at(track)->set("video_index", -1);
535 if (m_properties.contains("audio_index")) m_audioTrackProducers.at(track)->set("audio_index", m_properties.value("audio_index").toInt());
536 m_audioTrackProducers.at(track)->set("id", QString(getId() + '_' + QString::number(track) + "_audio").toUtf8().data());
537 if (m_properties.contains("force_colorspace")) m_audioTrackProducers.at(track)->set("force_colorspace", m_properties.value("force_colorspace").toInt());
538 if (m_properties.contains("full_luma")) m_audioTrackProducers.at(track)->set("set.force_full_luma", m_properties.value("full_luma").toInt());
540 return m_audioTrackProducers.at(track);
543 Mlt::Producer *DocClipBase::videoProducer()
545 if (m_videoOnlyProducer == NULL) {
547 for (i = 0; i < m_baseTrackProducers.count(); i++)
548 if (m_baseTrackProducers.at(i) != NULL) break;
549 if (i >= m_baseTrackProducers.count()) return NULL;
550 m_videoOnlyProducer = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
551 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
552 m_videoOnlyProducer->set("force_aspect_ratio", getPixelAspect(m_properties));
553 if (m_properties.contains("force_fps")) m_videoOnlyProducer->set("force_fps", m_properties.value("force_fps").toDouble());
554 if (m_properties.contains("force_progressive")) m_videoOnlyProducer->set("force_progressive", m_properties.value("force_progressive").toInt());
555 if (m_properties.contains("force_tff")) m_videoOnlyProducer->set("force_tff", m_properties.value("force_tff").toInt());
556 if (m_properties.contains("threads")) m_videoOnlyProducer->set("threads", m_properties.value("threads").toInt());
557 m_videoOnlyProducer->set("audio_index", -1);
558 if (m_properties.contains("video_index")) m_videoOnlyProducer->set("video_index", m_properties.value("video_index").toInt());
559 m_videoOnlyProducer->set("id", QString(getId() + "_video").toUtf8().data());
560 if (m_properties.contains("force_colorspace")) m_videoOnlyProducer->set("force_colorspace", m_properties.value("force_colorspace").toInt());
561 if (m_properties.contains("full_luma")) m_videoOnlyProducer->set("set.force_full_luma", m_properties.value("full_luma").toInt());
563 return m_videoOnlyProducer;
566 Mlt::Producer *DocClipBase::producer(int track)
568 /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
569 if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
571 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
572 if (m_baseTrackProducers.count() == 0) return NULL;
573 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
574 if (m_baseTrackProducers.at(i) != NULL)
575 return m_baseTrackProducers.at(i);
579 if (track >= m_baseTrackProducers.count()) {
580 while (m_baseTrackProducers.count() - 1 < track) {
581 m_baseTrackProducers.append(NULL);
584 if (m_baseTrackProducers.at(track) == NULL) {
586 for (i = 0; i < m_baseTrackProducers.count(); i++)
587 if (m_baseTrackProducers.at(i) != NULL) break;
589 if (i >= m_baseTrackProducers.count()) return NULL;
591 if (KIO::NetAccess::exists(KUrl(m_baseTrackProducers.at(i)->get("resource")), KIO::NetAccess::SourceSide, 0))
592 m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
593 else { // special case for placeholder clips
594 m_baseTrackProducers[track] = NULL;
597 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
598 m_baseTrackProducers[track]->set("force_aspect_ratio", getPixelAspect(m_properties));
599 if (m_properties.contains("force_fps")) m_baseTrackProducers[track]->set("force_fps", m_properties.value("force_fps").toDouble());
600 if (m_properties.contains("force_progressive")) m_baseTrackProducers[track]->set("force_progressive", m_properties.value("force_progressive").toInt());
601 if (m_properties.contains("force_tff")) m_baseTrackProducers[track]->set("force_tff", m_properties.value("force_tff").toInt());
602 if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
603 if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
604 if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
605 m_baseTrackProducers[track]->set("id", QString(getId() + '_' + QString::number(track)).toUtf8().data());
607 if (m_properties.contains("force_colorspace")) m_baseTrackProducers[track]->set("force_colorspace", m_properties.value("force_colorspace").toInt());
608 if (m_properties.contains("full_luma")) m_baseTrackProducers[track]->set("set.force_full_luma", m_properties.value("full_luma").toInt());
610 return m_baseTrackProducers.at(track);
613 void DocClipBase::setProducerProperty(const char *name, int data)
615 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
616 if (m_baseTrackProducers.at(i) != NULL)
617 m_baseTrackProducers[i]->set(name, data);
621 void DocClipBase::setProducerProperty(const char *name, double data)
623 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
624 if (m_baseTrackProducers.at(i) != NULL)
625 m_baseTrackProducers[i]->set(name, data);
629 void DocClipBase::setProducerProperty(const char *name, const char *data)
631 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
632 if (m_baseTrackProducers.at(i) != NULL)
633 m_baseTrackProducers[i]->set(name, data);
637 void DocClipBase::resetProducerProperty(const char *name)
639 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
640 if (m_baseTrackProducers.at(i) != NULL)
641 m_baseTrackProducers[i]->set(name, (const char*) NULL);
645 const char *DocClipBase::producerProperty(const char *name) const
647 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
648 if (m_baseTrackProducers.at(i) != NULL) {
649 return m_baseTrackProducers.at(i)->get(name);
656 void DocClipBase::slotRefreshProducer()
658 if (m_baseTrackProducers.count() == 0) return;
659 kDebug() << "//////////// REFRESH CLIP !!!!!!!!!!!!!!!!";
660 if (m_clipType == SLIDESHOW) {
661 /*Mlt::Producer producer(*(m_clipProducer->profile()), getProperty("resource").toUtf8().data());
662 delete m_clipProducer;
663 m_clipProducer = new Mlt::Producer(producer.get_producer());
664 if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
665 setProducerProperty("ttl", getProperty("ttl").toInt());
666 //m_clipProducer->set("id", getProperty("id"));
667 if (!getProperty("animation").isEmpty()) {
668 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
670 Mlt::Filter *filter = clipService.filter(ct);
672 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
674 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
675 clipService.detach(*filter);
677 filter = clipService.filter(ct);
680 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
681 // filter does not exist, create it.
682 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
683 if (filter && filter->is_valid()) {
684 int cycle = getProperty("ttl").toInt();
685 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
686 if (!geometry.isEmpty()) {
687 if (getProperty("animation").contains("low-pass")) {
688 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
689 if (blur && blur->is_valid())
690 clipService.attach(*blur);
692 filter->set("transition.geometry", geometry.toUtf8().data());
693 filter->set("transition.cycle", cycle);
694 clipService.attach(*filter);
699 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
701 Mlt::Filter *filter = clipService.filter(0);
703 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
704 clipService.detach(*filter);
706 filter = clipService.filter(ct);
709 if (getProperty("fade") == "1") {
710 // we want a fade filter effect
711 kDebug() << "//////////// FADE WANTED";
712 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
714 Mlt::Filter *filter = clipService.filter(ct);
716 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
720 filter = clipService.filter(ct);
723 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
724 filter->set("cycle", getProperty("ttl").toInt());
725 filter->set("duration", getProperty("luma_duration").toInt());
726 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
727 if (!getProperty("softness").isEmpty()) {
728 int soft = getProperty("softness").toInt();
729 filter->set("luma.softness", (double) soft / 100.0);
732 // filter does not exist, create it...
733 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
734 filter->set("cycle", getProperty("ttl").toInt());
735 filter->set("duration", getProperty("luma_duration").toInt());
736 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
737 if (!getProperty("softness").isEmpty()) {
738 int soft = getProperty("softness").toInt();
739 filter->set("luma.softness", (double) soft / 100.0);
741 clipService.attach(*filter);
744 kDebug() << "//////////// FADE NOT WANTED!!!";
745 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
747 Mlt::Filter *filter = clipService.filter(0);
749 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
750 clipService.detach(*filter);
752 filter = clipService.filter(ct);
755 if (getProperty("crop") == "1") {
756 // we want a center crop filter effect
757 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
759 Mlt::Filter *filter = clipService.filter(ct);
761 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
765 filter = clipService.filter(ct);
768 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
769 // filter does not exist, create it...
770 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
771 filter->set("center", 1);
772 clipService.attach(*filter);
775 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
777 Mlt::Filter *filter = clipService.filter(0);
779 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
780 clipService.detach(*filter);
782 filter = clipService.filter(ct);
788 void DocClipBase::setProperties(QMap <QString, QString> properties)
790 // changing clip type is not allowed
791 properties.remove("type");
792 QMapIterator<QString, QString> i(properties);
793 bool refreshProducer = false;
795 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
796 while (i.hasNext()) {
798 setProperty(i.key(), i.value());
799 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
801 if (refreshProducer) slotRefreshProducer();
804 void DocClipBase::setMetadata(QMap <QString, QString> properties)
806 QMapIterator<QString, QString> i(properties);
807 while (i.hasNext()) {
809 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
810 m_metadata.remove(i.key());
812 m_metadata.insert(i.key(), i.value());
817 QMap <QString, QString> DocClipBase::metadata() const
822 void DocClipBase::clearProperty(const QString &key)
824 m_properties.remove(key);
827 void DocClipBase::getFileHash(const QString url)
829 if (m_clipType == SLIDESHOW) return;
831 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
834 //kDebug() << "SETTING HASH of" << value;
835 m_properties.insert("file_size", QString::number(file.size()));
837 * 1 MB = 1 second per 450 files (or faster)
838 * 10 MB = 9 seconds per 450 files (or faster)
840 if (file.size() > 1000000*2) {
841 fileData = file.read(1000000);
842 if (file.seek(file.size() - 1000000))
843 fileData.append(file.readAll());
845 fileData = file.readAll();
847 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
848 m_properties.insert("file_hash", QString(fileHash.toHex()));
852 bool DocClipBase::checkHash() const
854 KUrl url = fileURL();
855 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
859 QString DocClipBase::getClipHash() const
862 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
863 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
864 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
865 else hash = m_properties.value("file_hash");
869 void DocClipBase::setPlaceHolder(bool place)
871 m_placeHolder = place;
875 QString DocClipBase::getHash(const QString &path)
878 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
882 * 1 MB = 1 second per 450 files (or faster)
883 * 10 MB = 9 seconds per 450 files (or faster)
885 if (file.size() > 1000000*2) {
886 fileData = file.read(1000000);
887 if (file.seek(file.size() - 1000000))
888 fileData.append(file.readAll());
890 fileData = file.readAll();
892 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
897 void DocClipBase::refreshThumbUrl()
899 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
902 void DocClipBase::setProperty(const QString &key, const QString &value)
904 m_properties.insert(key, value);
905 if (key == "resource") {
907 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
908 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
909 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
910 else if (key == "colour") {
911 setProducerProperty("colour", value.toUtf8().data());
912 } else if (key == "templatetext") {
913 setProducerProperty("templatetext", value.toUtf8().data());
914 setProducerProperty("force_reload", 1);
915 } else if (key == "xmldata") {
916 setProducerProperty("xmldata", value.toUtf8().data());
917 setProducerProperty("force_reload", 1);
918 } else if (key == "force_aspect_num") {
919 if (value.isEmpty()) {
920 m_properties.remove("force_aspect_num");
921 resetProducerProperty("force_aspect_ratio");
922 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
923 } else if (key == "force_aspect_den") {
924 if (value.isEmpty()) {
925 m_properties.remove("force_aspect_den");
926 resetProducerProperty("force_aspect_ratio");
927 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
928 } else if (key == "force_fps") {
929 if (value.isEmpty()) {
930 m_properties.remove("force_fps");
931 resetProducerProperty("force_fps");
932 } else setProducerProperty("force_fps", value.toDouble());
933 } else if (key == "force_progressive") {
934 if (value.isEmpty()) {
935 m_properties.remove("force_progressive");
936 resetProducerProperty("force_progressive");
937 } else setProducerProperty("force_progressive", value.toInt());
938 } else if (key == "force_tff") {
939 if (value.isEmpty()) {
940 m_properties.remove("force_tff");
941 resetProducerProperty("force_tff");
942 } else setProducerProperty("force_tff", value.toInt());
943 } else if (key == "threads") {
944 if (value.isEmpty()) {
945 m_properties.remove("threads");
946 setProducerProperty("threads", 1);
947 } else setProducerProperty("threads", value.toInt());
948 } else if (key == "video_index") {
949 if (value.isEmpty()) {
950 m_properties.remove("video_index");
951 setProducerProperty("video_index", m_properties.value("default_video").toInt());
952 } else setProducerProperty("video_index", value.toInt());
953 } else if (key == "audio_index") {
954 if (value.isEmpty()) {
955 m_properties.remove("audio_index");
956 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
957 } else setProducerProperty("audio_index", value.toInt());
958 } else if (key == "force_colorspace") {
959 if (value.isEmpty()) {
960 m_properties.remove("force_colorspace");
961 resetProducerProperty("force_colorspace");
962 } else setProducerProperty("force_colorspace", value.toInt());
963 } else if (key == "full_luma") {
964 if (value.isEmpty()) {
965 m_properties.remove("full_luma");
966 resetProducerProperty("set.force_full_luma");
967 } else setProducerProperty("set.force_full_luma", value.toInt());
971 QMap <QString, QString> DocClipBase::properties() const
976 bool DocClipBase::slotGetAudioThumbs()
978 if (m_thumbProd == NULL || isPlaceHolder()) return false;
979 if (!KdenliveSettings::audiothumbnails() || m_audioTimer == NULL) {
980 if (m_audioTimer != NULL) m_audioTimer->stop();
983 if (m_audioThumbCreated) {
984 m_audioTimer->stop();
987 m_audioTimer->start(1500);
988 double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
989 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
993 bool DocClipBase::isPlaceHolder() const
995 return m_placeHolder;
998 void DocClipBase::addCutZone(int in, int out, QString desc)
1001 info.zone = QPoint(in, out);
1002 info.description = desc;
1003 for (int i = 0; i < m_cutZones.count(); i++)
1004 if (m_cutZones.at(i).zone == info.zone) {
1007 m_cutZones.append(info);
1010 bool DocClipBase::hasCutZone(QPoint p) const
1012 for (int i = 0; i < m_cutZones.count(); i++)
1013 if (m_cutZones.at(i).zone == p) return true;
1018 void DocClipBase::removeCutZone(int in, int out)
1021 for (int i = 0; i < m_cutZones.count(); i++) {
1022 if (m_cutZones.at(i).zone == p) {
1023 m_cutZones.removeAt(i);
1029 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1031 QPoint old(oldin, oldout);
1032 for (int i = 0; i < m_cutZones.size(); ++i) {
1033 if (m_cutZones.at(i).zone == old) {
1035 info.zone = QPoint(in, out);
1036 info.description = desc;
1037 m_cutZones.replace(i, info);
1043 QList <CutZoneInfo> DocClipBase::cutZones() const
1048 bool DocClipBase::hasVideoCodec(const QString &codec) const
1050 Mlt::Producer *prod = NULL;
1051 if (m_baseTrackProducers.count() == 0) return false;
1052 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1053 if (m_baseTrackProducers.at(i) != NULL) {
1054 prod = m_baseTrackProducers.at(i);
1059 if (!prod) return false;
1060 int default_video = prod->get_int("video_index");
1062 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1063 return prod->get(property) == codec;
1066 bool DocClipBase::hasAudioCodec(const QString &codec) const
1068 Mlt::Producer *prod = NULL;
1069 if (m_baseTrackProducers.count() == 0) return false;
1070 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1071 if (m_baseTrackProducers.at(i) != NULL) {
1072 prod = m_baseTrackProducers.at(i);
1076 if (!prod) return false;
1077 int default_video = prod->get_int("audio_index");
1079 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1080 return prod->get(property) == codec;
1083 void DocClipBase::generateProxy(KUrl proxyFolder)
1085 if (m_proxyThread.isRunning()) return;
1086 QStringList parameters;
1087 parameters << "-i" << m_properties.value("resource");
1088 QString params = KdenliveSettings::proxyparams().simplified();
1089 foreach(QString s, params.split(' '))
1091 // Make sure we don't block when proxy file already exists
1093 if (m_properties.value("file_hash").isEmpty()) getFileHash(m_properties.value("resource"));
1094 QString path = proxyFolder.path(KUrl::AddTrailingSlash) + "proxy/" + m_properties.value("file_hash") + ".avi";
1095 setProperty("proxy", path.toUtf8().data());
1096 if (QFile::exists(path)) {
1097 emit proxyReady(m_id, true);
1101 m_proxyThread = QtConcurrent::run(this, &DocClipBase::slotGenerateProxy, parameters);
1104 void DocClipBase::slotGenerateProxy(QStringList parameters)
1106 int result = QProcess::execute("ffmpeg", parameters);
1107 if (result == 0) emit proxyReady(m_id, true);
1109 resetProducerProperty("proxy");
1110 emit proxyReady(m_id, false);