1 /**************************1*************************************************
2 DocClipBase.cpp - description
4 begin : Fri Apr 12 2002
5 copyright : (C) 2002 by Jason Wood
6 email : jasonwood@blueyonder.co.uk
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
18 #include <QCryptographicHash>
22 #include "kdenlivesettings.h"
23 #include "docclipbase.h"
25 #include "clipmanager.h"
27 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id):
28 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 > ()) {
29 int type = xml.attribute("type").toInt();
30 m_clipType = (CLIPTYPE) type;
31 m_name = xml.attribute("name");
33 QDomNamedNodeMap attributes = xml.attributes();
34 for (unsigned int i = 0; i < attributes.count(); i++) {
35 m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
38 KUrl url = KUrl(xml.attribute("resource"));
39 if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
40 int out = xml.attribute("out").toInt();
42 setDuration(GenTime(out, KdenliveSettings::project_fps()));
44 out = xml.attribute("duration").toInt();
45 if (out != 0) setDuration(GenTime(out, KdenliveSettings::project_fps()));
47 if (m_name.isEmpty()) m_name = url.fileName();
49 //if (!url.isEmpty() && QFile::exists(url.path()))
51 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
52 if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
54 //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
57 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
58 DocClipBase::operator=(clip);
60 m_clipType = clip.clipType();
62 m_duration = clip.duration();
63 m_audioThumbCreated = clip.audioThumbCreated();
64 m_properties = clip.properties();
68 DocClipBase::~DocClipBase() {
72 qDeleteAll(m_baseTrackProducers);
75 void DocClipBase::slotCreateAudioTimer() {
76 connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
77 connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
78 m_audioTimer = new QTimer(this);
79 connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
82 void DocClipBase::askForAudioThumbs() {
83 if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
86 void DocClipBase::slotClearAudioCache() {
87 if (m_thumbProd) m_thumbProd->stopAudioThumbs();
88 if (m_audioTimer != NULL) m_audioTimer->stop();
89 audioFrameChache.clear();
90 m_audioThumbCreated = false;
93 /*void DocClipBase::getClipMainThumb() {
94 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
97 KThumb *DocClipBase::thumbProducer() {
101 bool DocClipBase::audioThumbCreated() const {
102 return m_audioThumbCreated;
105 void DocClipBase::setName(const QString name) {
109 const QString & DocClipBase::name() const {
114 const QString &DocClipBase::getId() const {
118 void DocClipBase::setId(const QString &newId) {
122 const CLIPTYPE & DocClipBase::clipType() const {
126 void DocClipBase::setClipType(CLIPTYPE type) {
129 m_properties.insert("type", QString::number((int) type));
130 if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
131 slotCreateAudioTimer();
134 KUrl DocClipBase::fileURL() const {
135 QString res = m_properties.value("resource");
136 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
140 void DocClipBase::setClipThumbFrame(const uint &ix) {
141 m_properties.insert("thumbnail", QString::number((int) ix));
144 uint DocClipBase::getClipThumbFrame() const {
145 return (uint) m_properties.value("thumbnail").toInt();
148 const QString DocClipBase::description() const {
149 return m_properties.value("description");
152 bool DocClipBase::isTransparent() const {
153 return (m_properties.value("transparency") == "1");
156 const QString DocClipBase::getProperty(const QString prop) const {
157 return m_properties.value(prop);
160 void DocClipBase::setDuration(GenTime dur) {
162 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
165 const GenTime &DocClipBase::duration() const {
169 const GenTime &DocClipBase::maxDuration() const {
170 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
171 const GenTime dur(15000, KdenliveSettings::project_fps());
177 bool DocClipBase::hasFileSize() const {
183 QDomElement DocClipBase::toXML() const {
186 QDomElement clip = doc.createElement("producer");
188 QMapIterator<QString, QString> i(m_properties);
189 while (i.hasNext()) {
191 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
193 //doc.appendChild(clip);
194 //kDebug()<<"/// CLIP XML: "<<doc.toString();
198 DocClipBase *DocClipBase::
199 createClip(KdenliveDoc *doc, const QDomElement & element) {
200 DocClipBase *clip = 0;
202 QDomNode node = element;
204 if (element.tagName() != "kdenliveclip") {
206 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
210 QDomNode n = element.firstChild();
212 while (!n.isNull()) {
213 QDomElement e = n.toElement();
215 QString tagName = e.tagName();
216 if (e.tagName() == "avfile") {
217 // clip = DocClipAVFile::createClip(e);
218 } else if (e.tagName() == "DocTrackBaseList") {
219 // clip = DocClipProject::createClip(doc, e);
222 QDomText text = n.toText();
223 if (!text.isNull()) {
224 description = text.nodeValue();
231 kWarning() << "DocClipBase::createClip() unable to create clip";
233 // setup DocClipBase specifics of the clip.
234 QMap <QString, QString> props;
235 props.insert("description", description);
236 clip->setProperties(props);
237 clip->setAudioThumbCreated(false);
242 void DocClipBase::setAudioThumbCreated(bool isDone) {
243 m_audioThumbCreated = isDone;
247 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
248 m_thumbnail = pixmap;
251 const QPixmap & DocClipBase::thumbnail() const {
255 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
256 //kDebug() << "CLIPBASE RECIEDVEDÂ AUDIOÂ DATA*********************************************";
257 audioFrameChache = data;
258 m_audioThumbCreated = true;
262 QList < GenTime > DocClipBase::snapMarkers() const {
263 QList < GenTime > markers;
265 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
266 markers.append(m_snapMarkers[count].time());
272 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
273 return m_snapMarkers;
276 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
277 m_snapMarkers = markers;
280 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
281 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
282 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
283 if ((*it).time() >= time)
287 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
288 (*it).setComment(comment);
289 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
291 CommentedTime t(time, comment);
292 m_snapMarkers.insert(it, t);
297 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
298 QList < CommentedTime >::Iterator it;
299 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
300 if ((*it).time() == time)
303 if (it != m_snapMarkers.end()) {
304 (*it).setComment(comment);
306 kError() << "trying to edit Snap Marker that does not already exists";
310 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
311 QString result = i18n("Marker");
312 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
314 while (itt != m_snapMarkers.end()) {
315 if ((*itt).time() == time)
320 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
321 result = (*itt).comment();
322 m_snapMarkers.erase(itt);
328 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
329 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
331 while (itt != m_snapMarkers.end()) {
332 if ((*itt).time() == time)
340 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
342 for (it = 0; it < m_snapMarkers.count(); it++) {
343 if (m_snapMarkers[it].time() >= currTime)
346 if (it == 0) return GenTime();
347 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
348 return m_snapMarkers[it].time();
349 else return m_snapMarkers[it-1].time();
352 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
354 for (it = 0; it < m_snapMarkers.count(); it++) {
355 if (m_snapMarkers[it].time() > currTime)
358 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
362 QString DocClipBase::markerComment(GenTime t) {
363 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
365 while (itt != m_snapMarkers.end()) {
366 if ((*itt).time() == t)
367 return (*itt).comment();
370 return QString::null;
373 void DocClipBase::setProducer(Mlt::Producer *producer) {
374 if (producer == NULL) return;
375 QString id = producer->get("id");
376 if (id.contains('_')) {
377 // this is a subtrack producer, insert it at correct place
378 int pos = id.section('_', 1, 1).toInt();
379 if (pos >= m_baseTrackProducers.count()) {
380 while (m_baseTrackProducers.count() - 1 < pos) {
381 m_baseTrackProducers.append(NULL);
384 if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
386 if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
387 else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
389 //m_clipProducer = producer;
390 //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
391 if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
394 Mlt::Producer *DocClipBase::producer(int track) {
395 /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
396 if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
398 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
399 if (m_baseTrackProducers.count() == 0) return NULL;
401 for (i = 0; i < m_baseTrackProducers.count(); i++)
402 if (m_baseTrackProducers.at(i) != NULL) break;
403 if (i < m_baseTrackProducers.count()) return m_baseTrackProducers.at(i);
406 if (track >= m_baseTrackProducers.count()) {
407 while (m_baseTrackProducers.count() - 1 < track) {
408 m_baseTrackProducers.append(NULL);
411 if (m_baseTrackProducers.at(track) == NULL) {
413 for (i = 0; i < m_baseTrackProducers.count(); i++)
414 if (m_baseTrackProducers.at(i) != NULL) break;
415 if (i >= m_baseTrackProducers.count()) return NULL;
416 m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
417 if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
418 if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
419 if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
420 if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
421 char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
422 m_baseTrackProducers[track]->set("id", tmp);
425 return m_baseTrackProducers.at(track);
428 void DocClipBase::setProducerProperty(const char *name, int data) {
429 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
430 if (m_baseTrackProducers.at(i) != NULL)
431 m_baseTrackProducers[i]->set(name, data);
435 void DocClipBase::setProducerProperty(const char *name, const char *data) {
436 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
437 if (m_baseTrackProducers.at(i) != NULL)
438 m_baseTrackProducers[i]->set(name, data);
442 void DocClipBase::slotRefreshProducer() {
443 if (m_baseTrackProducers.count() == 0) return;
444 kDebug() << "//////////// REFRESH CLIP !!!!!!!!!!!!!!!!";
445 if (m_clipType == SLIDESHOW) {
446 /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
447 Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
449 delete m_clipProducer;
450 m_clipProducer = new Mlt::Producer(producer.get_producer());
451 if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
452 setProducerProperty("ttl", getProperty("ttl").toInt());
453 //m_clipProducer->set("id", getProperty("id"));
454 if (getProperty("fade") == "1") {
455 // we want a fade filter effect
456 kDebug() << "//////////// FADE WANTED";
457 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
459 Mlt::Filter *filter = clipService.filter(ct);
461 if (filter->get("mlt_service") == "luma") {
465 filter = clipService.filter(ct);
468 if (filter && filter->get("mlt_service") == "luma") {
469 filter->set("period", getProperty("ttl").toInt() - 1);
470 filter->set("luma.out", getProperty("luma_duration").toInt());
471 QString resource = getProperty("luma_file");
472 char *tmp = (char *) qstrdup(resource.toUtf8().data());
473 filter->set("luma.resource", tmp);
475 if (getProperty("softness") != QString()) {
476 int soft = getProperty("softness").toInt();
477 filter->set("luma.softness", (double) soft / 100.0);
480 // filter does not exist, create it...
481 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
482 filter->set("period", getProperty("ttl").toInt() - 1);
483 filter->set("luma.out", getProperty("luma_duration").toInt());
484 QString resource = getProperty("luma_file");
485 char *tmp = (char *) qstrdup(resource.toUtf8().data());
486 filter->set("luma.resource", tmp);
488 if (getProperty("softness") != QString()) {
489 int soft = getProperty("softness").toInt();
490 filter->set("luma.softness", (double) soft / 100.0);
492 clipService.attach(*filter);
495 kDebug() << "//////////// FADE NOT WANTED!!!";
496 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
498 Mlt::Filter *filter = clipService.filter(0);
500 if (filter->get("mlt_service") == "luma") {
501 clipService.detach(*filter);
503 filter = clipService.filter(ct);
509 void DocClipBase::setProperties(QMap <QString, QString> properties) {
510 // changing clip type is not allowed
511 properties.remove("type");
512 QMapIterator<QString, QString> i(properties);
513 bool refreshProducer = false;
515 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
516 while (i.hasNext()) {
518 setProperty(i.key(), i.value());
519 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
521 if (refreshProducer) slotRefreshProducer();
524 void DocClipBase::clearProperty(const QString &key) {
525 m_properties.remove(key);
528 void DocClipBase::getFileHash(const QString &url) {
529 if (m_clipType == SLIDESHOW) return;
531 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
534 //kDebug() << "SETTING HASH of" << value;
535 m_properties.insert("file_size", QString::number(file.size()));
537 * 1 MB = 1 second per 450 files (or faster)
538 * 10 MB = 9 seconds per 450 files (or faster)
540 if (file.size() > 1000000*2) {
541 fileData = file.read(1000000);
542 if (file.seek(file.size() - 1000000))
543 fileData.append(file.readAll());
545 fileData = file.readAll();
547 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
548 m_properties.insert("file_hash", QString(fileHash.toHex()));
549 //kDebug() << file.fileName() << file.size() << fileHash.toHex();
553 QString DocClipBase::getClipHash() const {
555 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
556 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
557 else hash = m_properties.value("file_hash");
561 void DocClipBase::refreshThumbUrl() {
562 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
565 void DocClipBase::setProperty(const QString &key, const QString &value) {
566 m_properties.insert(key, value);
567 if (key == "resource") {
569 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
570 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
571 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
572 else if (key == "colour") {
573 char *tmp = (char *) qstrdup(value.toUtf8().data());
574 setProducerProperty("colour", tmp);
576 } else if (key == "xmldata") {
577 setProducerProperty("force_reload", 1);
578 } else if (key == "force_aspect_ratio") {
579 if (value.isEmpty()) {
580 m_properties.remove("force_aspect_ratio");
581 setProducerProperty("force_aspect_ratio", 0);
582 } else setProducerProperty("force_aspect_ratio", value.toDouble());
583 } else if (key == "threads") {
584 if (value.isEmpty()) {
585 m_properties.remove("threads");
586 setProducerProperty("threads", 1);
587 } else setProducerProperty("threads", value.toInt());
588 } else if (key == "video_index") {
589 if (value.isEmpty()) {
590 m_properties.remove("video_index");
591 setProducerProperty("video_index", m_properties.value("default_video").toInt());
592 } else setProducerProperty("video_index", value.toInt());
593 } else if (key == "audio_index") {
594 if (value.isEmpty()) {
595 m_properties.remove("audio_index");
596 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
597 } else setProducerProperty("audio_index", value.toInt());
601 QMap <QString, QString> DocClipBase::properties() const {
605 bool DocClipBase::slotGetAudioThumbs() {
606 if (m_thumbProd == NULL) return false;
607 if (!KdenliveSettings::audiothumbnails()) {
608 if (m_audioTimer != NULL) m_audioTimer->stop();
611 if (m_audioThumbCreated) {
612 if (m_audioTimer != NULL) m_audioTimer->stop();
615 if (m_audioTimer != NULL)
616 m_audioTimer->start(1500);
617 double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
618 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);