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());
42 if (xml.hasAttribute("duration")) {
43 setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
45 int out = xml.attribute("out").toInt();
46 int in = xml.attribute("in").toInt();
47 setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
50 if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
52 //if (!url.isEmpty() && QFile::exists(url.path()))
54 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
55 if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
57 //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
60 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
61 DocClipBase::operator=(clip);
63 m_clipType = clip.clipType();
65 m_duration = clip.duration();
66 m_audioThumbCreated = clip.audioThumbCreated();
67 m_properties = clip.properties();
71 DocClipBase::~DocClipBase() {
79 qDeleteAll(m_baseTrackProducers);
80 m_baseTrackProducers.clear();
83 void DocClipBase::setZone(QPoint zone) {
84 m_properties.insert("zone_in", QString::number(zone.x()));
85 m_properties.insert("zone_out", QString::number(zone.y()));
88 QPoint DocClipBase::zone() const {
90 zone.setX(m_properties.value("zone_in").toInt());
91 zone.setY(m_properties.value("zone_out", "50").toInt());
95 void DocClipBase::slotCreateAudioTimer() {
96 connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
97 connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
98 m_audioTimer = new QTimer(this);
99 connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
102 void DocClipBase::askForAudioThumbs() {
103 if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
106 void DocClipBase::slotClearAudioCache() {
107 if (m_thumbProd) m_thumbProd->stopAudioThumbs();
108 if (m_audioTimer != NULL) m_audioTimer->stop();
109 audioFrameChache.clear();
110 m_audioThumbCreated = false;
113 /*void DocClipBase::getClipMainThumb() {
114 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
117 KThumb *DocClipBase::thumbProducer() {
121 bool DocClipBase::audioThumbCreated() const {
122 return m_audioThumbCreated;
125 const QString DocClipBase::name() const {
127 return m_properties.value("name");
130 const QString &DocClipBase::getId() const {
134 void DocClipBase::setId(const QString &newId) {
138 const CLIPTYPE & DocClipBase::clipType() const {
142 void DocClipBase::setClipType(CLIPTYPE type) {
145 m_properties.insert("type", QString::number((int) type));
146 if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
147 slotCreateAudioTimer();
150 KUrl DocClipBase::fileURL() const {
151 QString res = m_properties.value("resource");
152 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
156 void DocClipBase::setClipThumbFrame(const uint &ix) {
157 m_properties.insert("thumbnail", QString::number((int) ix));
160 uint DocClipBase::getClipThumbFrame() const {
161 return (uint) m_properties.value("thumbnail").toInt();
164 const QString DocClipBase::description() const {
165 return m_properties.value("description");
168 bool DocClipBase::isTransparent() const {
169 return (m_properties.value("transparency") == "1");
172 const QString DocClipBase::getProperty(const QString prop) const {
173 return m_properties.value(prop);
176 void DocClipBase::setDuration(GenTime dur) {
178 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
181 const GenTime &DocClipBase::duration() const {
185 const GenTime DocClipBase::maxDuration() const {
186 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
187 const GenTime dur(15000, KdenliveSettings::project_fps());
193 bool DocClipBase::hasFileSize() const {
199 QDomElement DocClipBase::toXML() const {
201 QDomElement clip = doc.createElement("producer");
203 QMapIterator<QString, QString> i(m_properties);
204 while (i.hasNext()) {
206 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
208 doc.appendChild(clip);
209 //kDebug()<<"/// CLIP XML: "<<doc.toString();
210 return doc.documentElement();
213 DocClipBase *DocClipBase::
214 createClip(KdenliveDoc *doc, const QDomElement & element) {
215 DocClipBase *clip = 0;
217 QDomNode node = element;
219 if (element.tagName() != "kdenliveclip") {
221 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
225 QDomNode n = element.firstChild();
227 while (!n.isNull()) {
228 QDomElement e = n.toElement();
230 QString tagName = e.tagName();
231 if (e.tagName() == "avfile") {
232 // clip = DocClipAVFile::createClip(e);
233 } else if (e.tagName() == "DocTrackBaseList") {
234 // clip = DocClipProject::createClip(doc, e);
237 QDomText text = n.toText();
238 if (!text.isNull()) {
239 description = text.nodeValue();
246 kWarning() << "DocClipBase::createClip() unable to create clip";
248 // setup DocClipBase specifics of the clip.
249 QMap <QString, QString> props;
250 props.insert("description", description);
251 clip->setProperties(props);
252 clip->setAudioThumbCreated(false);
257 void DocClipBase::setAudioThumbCreated(bool isDone) {
258 m_audioThumbCreated = isDone;
262 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
263 m_thumbnail = pixmap;
266 const QPixmap & DocClipBase::thumbnail() const {
270 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
271 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
272 audioFrameChache = data;
273 m_audioThumbCreated = true;
277 QList < GenTime > DocClipBase::snapMarkers() const {
278 QList < GenTime > markers;
280 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
281 markers.append(m_snapMarkers[count].time());
287 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
288 return m_snapMarkers;
291 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
292 m_snapMarkers = markers;
295 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
296 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
297 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
298 if ((*it).time() >= time)
302 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
303 (*it).setComment(comment);
304 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
306 CommentedTime t(time, comment);
307 m_snapMarkers.insert(it, t);
312 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
313 QList < CommentedTime >::Iterator it;
314 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
315 if ((*it).time() == time)
318 if (it != m_snapMarkers.end()) {
319 (*it).setComment(comment);
321 kError() << "trying to edit Snap Marker that does not already exists";
325 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
326 QString result = i18n("Marker");
327 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
329 while (itt != m_snapMarkers.end()) {
330 if ((*itt).time() == time)
335 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
336 result = (*itt).comment();
337 m_snapMarkers.erase(itt);
343 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
344 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
346 while (itt != m_snapMarkers.end()) {
347 if ((*itt).time() == time)
355 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
357 for (it = 0; it < m_snapMarkers.count(); it++) {
358 if (m_snapMarkers[it].time() >= currTime)
361 if (it == 0) return GenTime();
362 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
363 return m_snapMarkers[it].time();
364 else return m_snapMarkers[it-1].time();
367 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
369 for (it = 0; it < m_snapMarkers.count(); it++) {
370 if (m_snapMarkers[it].time() > currTime)
373 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
377 QString DocClipBase::markerComment(GenTime t) {
378 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
380 while (itt != m_snapMarkers.end()) {
381 if ((*itt).time() == t)
382 return (*itt).comment();
388 void DocClipBase::deleteProducers() {
389 qDeleteAll(m_baseTrackProducers);
390 m_baseTrackProducers.clear();
391 if (m_thumbProd) m_thumbProd->clearProducer();
394 void DocClipBase::setProducer(Mlt::Producer *producer) {
395 if (producer == NULL) return;
396 QString id = producer->get("id");
397 if (id.contains('_')) {
398 // this is a subtrack producer, insert it at correct place
399 int pos = id.section('_', 1, 1).toInt();
400 if (pos >= m_baseTrackProducers.count()) {
401 while (m_baseTrackProducers.count() - 1 < pos) {
402 m_baseTrackProducers.append(NULL);
405 if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
407 if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
408 else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
410 //m_clipProducer = producer;
411 //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
412 if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
415 Mlt::Producer *DocClipBase::producer(int track) {
416 /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
417 if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
419 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
420 if (m_baseTrackProducers.count() == 0) return NULL;
421 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
422 if (m_baseTrackProducers.at(i) != NULL)
423 return m_baseTrackProducers.at(i);
427 if (track >= m_baseTrackProducers.count()) {
428 while (m_baseTrackProducers.count() - 1 < track) {
429 m_baseTrackProducers.append(NULL);
432 if (m_baseTrackProducers.at(track) == NULL) {
434 for (i = 0; i < m_baseTrackProducers.count(); i++)
435 if (m_baseTrackProducers.at(i) != NULL) break;
436 if (i >= m_baseTrackProducers.count()) return NULL;
437 m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
438 if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
439 if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
440 if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
441 if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
442 char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
443 m_baseTrackProducers[track]->set("id", tmp);
445 if (KdenliveSettings::dropbframes() && m_baseTrackProducers.at(i)->get("skip_loop_filter") && strcmp(m_baseTrackProducers.at(i)->get("skip_loop_filter"), "all") == 0) {
446 m_baseTrackProducers[track]->set("skip_loop_filter", "all");
447 m_baseTrackProducers[track]->set("skip_frame", "bidir");
450 return m_baseTrackProducers.at(track);
453 void DocClipBase::setProducerProperty(const char *name, int data) {
454 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
455 if (m_baseTrackProducers.at(i) != NULL)
456 m_baseTrackProducers[i]->set(name, data);
460 void DocClipBase::setProducerProperty(const char *name, const char *data) {
461 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
462 if (m_baseTrackProducers.at(i) != NULL)
463 m_baseTrackProducers[i]->set(name, data);
467 const char *DocClipBase::producerProperty(const char *name) const {
468 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
469 if (m_baseTrackProducers.at(i) != NULL) {
470 return m_baseTrackProducers.at(i)->get(name);
477 void DocClipBase::slotRefreshProducer() {
478 if (m_baseTrackProducers.count() == 0) return;
479 kDebug() << "//////////// REFRESH CLIP !!!!!!!!!!!!!!!!";
480 if (m_clipType == SLIDESHOW) {
481 /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
482 Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
484 delete m_clipProducer;
485 m_clipProducer = new Mlt::Producer(producer.get_producer());
486 if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
487 setProducerProperty("ttl", getProperty("ttl").toInt());
488 //m_clipProducer->set("id", getProperty("id"));
489 if (getProperty("fade") == "1") {
490 // we want a fade filter effect
491 kDebug() << "//////////// FADE WANTED";
492 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
494 Mlt::Filter *filter = clipService.filter(ct);
496 if (filter->get("mlt_service") == "luma") {
500 filter = clipService.filter(ct);
503 if (filter && filter->get("mlt_service") == "luma") {
504 filter->set("period", getProperty("ttl").toInt() - 1);
505 filter->set("luma.out", getProperty("luma_duration").toInt());
506 QString resource = getProperty("luma_file");
507 char *tmp = (char *) qstrdup(resource.toUtf8().data());
508 filter->set("luma.resource", tmp);
510 if (!getProperty("softness").isEmpty()) {
511 int soft = getProperty("softness").toInt();
512 filter->set("luma.softness", (double) soft / 100.0);
515 // filter does not exist, create it...
516 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
517 filter->set("period", getProperty("ttl").toInt() - 1);
518 filter->set("luma.out", getProperty("luma_duration").toInt());
519 QString resource = getProperty("luma_file");
520 char *tmp = (char *) qstrdup(resource.toUtf8().data());
521 filter->set("luma.resource", tmp);
523 if (!getProperty("softness").isEmpty()) {
524 int soft = getProperty("softness").toInt();
525 filter->set("luma.softness", (double) soft / 100.0);
527 clipService.attach(*filter);
530 kDebug() << "//////////// FADE NOT WANTED!!!";
531 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
533 Mlt::Filter *filter = clipService.filter(0);
535 if (filter->get("mlt_service") == "luma") {
536 clipService.detach(*filter);
538 filter = clipService.filter(ct);
544 void DocClipBase::setProperties(QMap <QString, QString> properties) {
545 // changing clip type is not allowed
546 properties.remove("type");
547 QMapIterator<QString, QString> i(properties);
548 bool refreshProducer = false;
550 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
551 while (i.hasNext()) {
553 setProperty(i.key(), i.value());
554 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
556 if (refreshProducer) slotRefreshProducer();
559 void DocClipBase::setMetadata(QMap <QString, QString> properties) {
560 m_metadata = properties;
563 QMap <QString, QString> DocClipBase::metadata() const {
567 void DocClipBase::clearProperty(const QString &key) {
568 m_properties.remove(key);
571 void DocClipBase::getFileHash(const QString &url) {
572 if (m_clipType == SLIDESHOW) return;
574 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
577 //kDebug() << "SETTING HASH of" << value;
578 m_properties.insert("file_size", QString::number(file.size()));
580 * 1 MB = 1 second per 450 files (or faster)
581 * 10 MB = 9 seconds per 450 files (or faster)
583 if (file.size() > 1000000*2) {
584 fileData = file.read(1000000);
585 if (file.seek(file.size() - 1000000))
586 fileData.append(file.readAll());
588 fileData = file.readAll();
590 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
591 m_properties.insert("file_hash", QString(fileHash.toHex()));
592 //kDebug() << file.fileName() << file.size() << fileHash.toHex();
596 QString DocClipBase::getClipHash() const {
598 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
599 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
600 else hash = m_properties.value("file_hash");
604 void DocClipBase::refreshThumbUrl() {
605 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
608 void DocClipBase::setProperty(const QString &key, const QString &value) {
609 m_properties.insert(key, value);
610 if (key == "resource") {
612 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
613 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
614 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
615 else if (key == "colour") {
616 char *tmp = (char *) qstrdup(value.toUtf8().data());
617 setProducerProperty("colour", tmp);
619 } else if (key == "xmldata") {
620 setProducerProperty("force_reload", 1);
621 } else if (key == "force_aspect_ratio") {
622 if (value.isEmpty()) {
623 m_properties.remove("force_aspect_ratio");
624 setProducerProperty("force_aspect_ratio", 0);
625 } else setProducerProperty("force_aspect_ratio", value.toDouble());
626 } else if (key == "threads") {
627 if (value.isEmpty()) {
628 m_properties.remove("threads");
629 setProducerProperty("threads", 1);
630 } else setProducerProperty("threads", value.toInt());
631 } else if (key == "video_index") {
632 if (value.isEmpty()) {
633 m_properties.remove("video_index");
634 setProducerProperty("video_index", m_properties.value("default_video").toInt());
635 } else setProducerProperty("video_index", value.toInt());
636 } else if (key == "audio_index") {
637 if (value.isEmpty()) {
638 m_properties.remove("audio_index");
639 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
640 } else setProducerProperty("audio_index", value.toInt());
644 QMap <QString, QString> DocClipBase::properties() const {
648 bool DocClipBase::slotGetAudioThumbs() {
649 if (m_thumbProd == NULL) return false;
650 if (!KdenliveSettings::audiothumbnails()) {
651 if (m_audioTimer != NULL) m_audioTimer->stop();
654 if (m_audioThumbCreated) {
655 if (m_audioTimer != NULL) m_audioTimer->stop();
658 if (m_audioTimer != NULL)
659 m_audioTimer->start(1500);
660 double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
661 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);