1 /**************************1*************************************************
2 DocClipBase.cpp - description
4 begin : Fri Apr 12 2002
5 copyright : (C) 2002 by Jason Wood
6 copyright : (C) 2009 by Jean-Baptiste Mardelle
8 ***************************************************************************/
10 /***************************************************************************
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
17 ***************************************************************************/
20 #include "docclipbase.h"
21 #include "kdenlivesettings.h"
23 #include "clipmanager.h"
27 #include <QCryptographicHash>
29 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id):
30 m_id(id), m_description(QString()), m_refcount(0), m_audioThumbCreated(false), m_duration(GenTime()), m_thumbProd(NULL), m_audioTimer(NULL), m_properties(QMap <QString, QString> ()), audioFrameChache(QMap<int, QMap<int, QByteArray> > ()), m_baseTrackProducers(QList <Mlt::Producer *>()), m_snapMarkers(QList < CommentedTime > ()) {
31 int type = xml.attribute("type").toInt();
32 m_clipType = (CLIPTYPE) type;
34 QDomNamedNodeMap attributes = xml.attributes();
35 for (unsigned int i = 0; i < attributes.count(); i++) {
36 m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
39 KUrl url = KUrl(xml.attribute("resource"));
40 if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
41 int out = xml.attribute("out").toInt();
43 setDuration(GenTime(out, KdenliveSettings::project_fps()));
45 out = xml.attribute("duration").toInt();
46 if (out != 0) setDuration(GenTime(out, KdenliveSettings::project_fps()));
48 if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
50 //if (!url.isEmpty() && QFile::exists(url.path()))
52 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
53 if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
55 //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
58 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
59 DocClipBase::operator=(clip);
61 m_clipType = clip.clipType();
63 m_duration = clip.duration();
64 m_audioThumbCreated = clip.audioThumbCreated();
65 m_properties = clip.properties();
69 DocClipBase::~DocClipBase() {
77 qDeleteAll(m_baseTrackProducers);
78 m_baseTrackProducers.clear();
81 void DocClipBase::setZone(QPoint zone) {
82 m_properties.insert("zone_in", QString::number(zone.x()));
83 m_properties.insert("zone_out", QString::number(zone.y()));
86 QPoint DocClipBase::zone() const {
88 zone.setX(m_properties.value("zone_in").toInt());
89 zone.setY(m_properties.value("zone_out", "50").toInt());
93 void DocClipBase::slotCreateAudioTimer() {
94 connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
95 connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
96 m_audioTimer = new QTimer(this);
97 connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
100 void DocClipBase::askForAudioThumbs() {
101 if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
104 void DocClipBase::slotClearAudioCache() {
105 if (m_thumbProd) m_thumbProd->stopAudioThumbs();
106 if (m_audioTimer != NULL) m_audioTimer->stop();
107 audioFrameChache.clear();
108 m_audioThumbCreated = false;
111 /*void DocClipBase::getClipMainThumb() {
112 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
115 KThumb *DocClipBase::thumbProducer() {
119 bool DocClipBase::audioThumbCreated() const {
120 return m_audioThumbCreated;
123 const QString DocClipBase::name() const {
125 return m_properties.value("name");
128 const QString &DocClipBase::getId() const {
132 void DocClipBase::setId(const QString &newId) {
136 const CLIPTYPE & DocClipBase::clipType() const {
140 void DocClipBase::setClipType(CLIPTYPE type) {
143 m_properties.insert("type", QString::number((int) type));
144 if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
145 slotCreateAudioTimer();
148 KUrl DocClipBase::fileURL() const {
149 QString res = m_properties.value("resource");
150 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
154 void DocClipBase::setClipThumbFrame(const uint &ix) {
155 m_properties.insert("thumbnail", QString::number((int) ix));
158 uint DocClipBase::getClipThumbFrame() const {
159 return (uint) m_properties.value("thumbnail").toInt();
162 const QString DocClipBase::description() const {
163 return m_properties.value("description");
166 bool DocClipBase::isTransparent() const {
167 return (m_properties.value("transparency") == "1");
170 const QString DocClipBase::getProperty(const QString prop) const {
171 return m_properties.value(prop);
174 void DocClipBase::setDuration(GenTime dur) {
176 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
179 const GenTime &DocClipBase::duration() const {
183 const GenTime DocClipBase::maxDuration() const {
184 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
185 const GenTime dur(15000, KdenliveSettings::project_fps());
191 bool DocClipBase::hasFileSize() const {
197 QDomElement DocClipBase::toXML() const {
199 QDomElement clip = doc.createElement("producer");
201 QMapIterator<QString, QString> i(m_properties);
202 while (i.hasNext()) {
204 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
206 doc.appendChild(clip);
207 //kDebug()<<"/// CLIP XML: "<<doc.toString();
208 return doc.documentElement();
211 DocClipBase *DocClipBase::
212 createClip(KdenliveDoc *doc, const QDomElement & element) {
213 DocClipBase *clip = 0;
215 QDomNode node = element;
217 if (element.tagName() != "kdenliveclip") {
219 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
223 QDomNode n = element.firstChild();
225 while (!n.isNull()) {
226 QDomElement e = n.toElement();
228 QString tagName = e.tagName();
229 if (e.tagName() == "avfile") {
230 // clip = DocClipAVFile::createClip(e);
231 } else if (e.tagName() == "DocTrackBaseList") {
232 // clip = DocClipProject::createClip(doc, e);
235 QDomText text = n.toText();
236 if (!text.isNull()) {
237 description = text.nodeValue();
244 kWarning() << "DocClipBase::createClip() unable to create clip";
246 // setup DocClipBase specifics of the clip.
247 QMap <QString, QString> props;
248 props.insert("description", description);
249 clip->setProperties(props);
250 clip->setAudioThumbCreated(false);
255 void DocClipBase::setAudioThumbCreated(bool isDone) {
256 m_audioThumbCreated = isDone;
260 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
261 m_thumbnail = pixmap;
264 const QPixmap & DocClipBase::thumbnail() const {
268 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
269 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
270 audioFrameChache = data;
271 m_audioThumbCreated = true;
275 QList < GenTime > DocClipBase::snapMarkers() const {
276 QList < GenTime > markers;
278 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
279 markers.append(m_snapMarkers[count].time());
285 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
286 return m_snapMarkers;
289 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
290 m_snapMarkers = markers;
293 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
294 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
295 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
296 if ((*it).time() >= time)
300 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
301 (*it).setComment(comment);
302 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
304 CommentedTime t(time, comment);
305 m_snapMarkers.insert(it, t);
310 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
311 QList < CommentedTime >::Iterator it;
312 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
313 if ((*it).time() == time)
316 if (it != m_snapMarkers.end()) {
317 (*it).setComment(comment);
319 kError() << "trying to edit Snap Marker that does not already exists";
323 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
324 QString result = i18n("Marker");
325 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
327 while (itt != m_snapMarkers.end()) {
328 if ((*itt).time() == time)
333 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
334 result = (*itt).comment();
335 m_snapMarkers.erase(itt);
341 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
342 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
344 while (itt != m_snapMarkers.end()) {
345 if ((*itt).time() == time)
353 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
355 for (it = 0; it < m_snapMarkers.count(); it++) {
356 if (m_snapMarkers[it].time() >= currTime)
359 if (it == 0) return GenTime();
360 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
361 return m_snapMarkers[it].time();
362 else return m_snapMarkers[it-1].time();
365 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
367 for (it = 0; it < m_snapMarkers.count(); it++) {
368 if (m_snapMarkers[it].time() > currTime)
371 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
375 QString DocClipBase::markerComment(GenTime t) {
376 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
378 while (itt != m_snapMarkers.end()) {
379 if ((*itt).time() == t)
380 return (*itt).comment();
386 void DocClipBase::deleteProducers() {
387 qDeleteAll(m_baseTrackProducers);
388 m_baseTrackProducers.clear();
389 if (m_thumbProd) m_thumbProd->clearProducer();
392 void DocClipBase::setProducer(Mlt::Producer *producer) {
393 if (producer == NULL) return;
394 QString id = producer->get("id");
395 if (id.contains('_')) {
396 // this is a subtrack producer, insert it at correct place
397 int pos = id.section('_', 1, 1).toInt();
398 if (pos >= m_baseTrackProducers.count()) {
399 while (m_baseTrackProducers.count() - 1 < pos) {
400 m_baseTrackProducers.append(NULL);
403 if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
405 if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
406 else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
408 //m_clipProducer = producer;
409 //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
410 if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
413 Mlt::Producer *DocClipBase::producer(int track) {
414 /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
415 if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
417 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
418 if (m_baseTrackProducers.count() == 0) return NULL;
420 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
421 if (m_baseTrackProducers.at(i) != NULL)
422 return m_baseTrackProducers.at(i);
426 if (track >= m_baseTrackProducers.count()) {
427 while (m_baseTrackProducers.count() - 1 < track) {
428 m_baseTrackProducers.append(NULL);
431 if (m_baseTrackProducers.at(track) == NULL) {
433 for (i = 0; i < m_baseTrackProducers.count(); i++)
434 if (m_baseTrackProducers.at(i) != NULL) break;
435 if (i >= m_baseTrackProducers.count()) return NULL;
436 m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
437 if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
438 if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
439 if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
440 if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
441 char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
442 m_baseTrackProducers[track]->set("id", tmp);
444 if (KdenliveSettings::dropbframes() && m_baseTrackProducers.at(i)->get("skip_loop_filter") && strcmp(m_baseTrackProducers.at(i)->get("skip_loop_filter"), "all") == 0) {
445 m_baseTrackProducers[track]->set("skip_loop_filter", "all");
446 m_baseTrackProducers[track]->set("skip_frame", "bidir");
449 return m_baseTrackProducers.at(track);
452 void DocClipBase::setProducerProperty(const char *name, int data) {
453 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
454 if (m_baseTrackProducers.at(i) != NULL)
455 m_baseTrackProducers[i]->set(name, data);
459 void DocClipBase::setProducerProperty(const char *name, const char *data) {
460 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
461 if (m_baseTrackProducers.at(i) != NULL)
462 m_baseTrackProducers[i]->set(name, data);
466 const char *DocClipBase::producerProperty(const char *name) const {
467 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
468 if (m_baseTrackProducers.at(i) != NULL) {
469 return m_baseTrackProducers.at(i)->get(name);
476 void DocClipBase::slotRefreshProducer() {
477 if (m_baseTrackProducers.count() == 0) return;
478 kDebug() << "//////////// REFRESH CLIP !!!!!!!!!!!!!!!!";
479 if (m_clipType == SLIDESHOW) {
480 /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
481 Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
483 delete m_clipProducer;
484 m_clipProducer = new Mlt::Producer(producer.get_producer());
485 if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
486 setProducerProperty("ttl", getProperty("ttl").toInt());
487 //m_clipProducer->set("id", getProperty("id"));
488 if (getProperty("fade") == "1") {
489 // we want a fade filter effect
490 kDebug() << "//////////// FADE WANTED";
491 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
493 Mlt::Filter *filter = clipService.filter(ct);
495 if (filter->get("mlt_service") == "luma") {
499 filter = clipService.filter(ct);
502 if (filter && filter->get("mlt_service") == "luma") {
503 filter->set("period", getProperty("ttl").toInt() - 1);
504 filter->set("luma.out", getProperty("luma_duration").toInt());
505 QString resource = getProperty("luma_file");
506 char *tmp = (char *) qstrdup(resource.toUtf8().data());
507 filter->set("luma.resource", tmp);
509 if (!getProperty("softness").isEmpty()) {
510 int soft = getProperty("softness").toInt();
511 filter->set("luma.softness", (double) soft / 100.0);
514 // filter does not exist, create it...
515 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
516 filter->set("period", getProperty("ttl").toInt() - 1);
517 filter->set("luma.out", getProperty("luma_duration").toInt());
518 QString resource = getProperty("luma_file");
519 char *tmp = (char *) qstrdup(resource.toUtf8().data());
520 filter->set("luma.resource", tmp);
522 if (!getProperty("softness").isEmpty()) {
523 int soft = getProperty("softness").toInt();
524 filter->set("luma.softness", (double) soft / 100.0);
526 clipService.attach(*filter);
529 kDebug() << "//////////// FADE NOT WANTED!!!";
530 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
532 Mlt::Filter *filter = clipService.filter(0);
534 if (filter->get("mlt_service") == "luma") {
535 clipService.detach(*filter);
537 filter = clipService.filter(ct);
543 void DocClipBase::setProperties(QMap <QString, QString> properties) {
544 // changing clip type is not allowed
545 properties.remove("type");
546 QMapIterator<QString, QString> i(properties);
547 bool refreshProducer = false;
549 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
550 while (i.hasNext()) {
552 setProperty(i.key(), i.value());
553 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
555 if (refreshProducer) slotRefreshProducer();
558 void DocClipBase::setMetadata(QMap <QString, QString> properties) {
559 m_metadata = properties;
562 QMap <QString, QString> DocClipBase::metadata() const {
566 void DocClipBase::clearProperty(const QString &key) {
567 m_properties.remove(key);
570 void DocClipBase::getFileHash(const QString &url) {
571 if (m_clipType == SLIDESHOW) return;
573 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
576 //kDebug() << "SETTING HASH of" << value;
577 m_properties.insert("file_size", QString::number(file.size()));
579 * 1 MB = 1 second per 450 files (or faster)
580 * 10 MB = 9 seconds per 450 files (or faster)
582 if (file.size() > 1000000*2) {
583 fileData = file.read(1000000);
584 if (file.seek(file.size() - 1000000))
585 fileData.append(file.readAll());
587 fileData = file.readAll();
589 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
590 m_properties.insert("file_hash", QString(fileHash.toHex()));
591 //kDebug() << file.fileName() << file.size() << fileHash.toHex();
595 QString DocClipBase::getClipHash() const {
597 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
598 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
599 else hash = m_properties.value("file_hash");
603 void DocClipBase::refreshThumbUrl() {
604 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
607 void DocClipBase::setProperty(const QString &key, const QString &value) {
608 m_properties.insert(key, value);
609 if (key == "resource") {
611 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
612 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
613 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
614 else if (key == "colour") {
615 char *tmp = (char *) qstrdup(value.toUtf8().data());
616 setProducerProperty("colour", tmp);
618 } else if (key == "xmldata") {
619 setProducerProperty("force_reload", 1);
620 } else if (key == "force_aspect_ratio") {
621 if (value.isEmpty()) {
622 m_properties.remove("force_aspect_ratio");
623 setProducerProperty("force_aspect_ratio", 0);
624 } else setProducerProperty("force_aspect_ratio", value.toDouble());
625 } else if (key == "threads") {
626 if (value.isEmpty()) {
627 m_properties.remove("threads");
628 setProducerProperty("threads", 1);
629 } else setProducerProperty("threads", value.toInt());
630 } else if (key == "video_index") {
631 if (value.isEmpty()) {
632 m_properties.remove("video_index");
633 setProducerProperty("video_index", m_properties.value("default_video").toInt());
634 } else setProducerProperty("video_index", value.toInt());
635 } else if (key == "audio_index") {
636 if (value.isEmpty()) {
637 m_properties.remove("audio_index");
638 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
639 } else setProducerProperty("audio_index", value.toInt());
643 QMap <QString, QString> DocClipBase::properties() const {
647 bool DocClipBase::slotGetAudioThumbs() {
648 if (m_thumbProd == NULL) return false;
649 if (!KdenliveSettings::audiothumbnails()) {
650 if (m_audioTimer != NULL) m_audioTimer->stop();
653 if (m_audioThumbCreated) {
654 if (m_audioTimer != NULL) m_audioTimer->stop();
657 if (m_audioTimer != NULL)
658 m_audioTimer->start(1500);
659 double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
660 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);