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);
73 m_baseTrackProducers.clear();
76 void DocClipBase::slotCreateAudioTimer() {
77 connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
78 connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
79 m_audioTimer = new QTimer(this);
80 connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
83 void DocClipBase::askForAudioThumbs() {
84 if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
87 void DocClipBase::slotClearAudioCache() {
88 if (m_thumbProd) m_thumbProd->stopAudioThumbs();
89 if (m_audioTimer != NULL) m_audioTimer->stop();
90 audioFrameChache.clear();
91 m_audioThumbCreated = false;
94 /*void DocClipBase::getClipMainThumb() {
95 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
98 KThumb *DocClipBase::thumbProducer() {
102 bool DocClipBase::audioThumbCreated() const {
103 return m_audioThumbCreated;
106 void DocClipBase::setName(const QString name) {
110 const QString & DocClipBase::name() const {
115 const QString &DocClipBase::getId() const {
119 void DocClipBase::setId(const QString &newId) {
123 const CLIPTYPE & DocClipBase::clipType() const {
127 void DocClipBase::setClipType(CLIPTYPE type) {
130 m_properties.insert("type", QString::number((int) type));
131 if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
132 slotCreateAudioTimer();
135 KUrl DocClipBase::fileURL() const {
136 QString res = m_properties.value("resource");
137 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
141 void DocClipBase::setClipThumbFrame(const uint &ix) {
142 m_properties.insert("thumbnail", QString::number((int) ix));
145 uint DocClipBase::getClipThumbFrame() const {
146 return (uint) m_properties.value("thumbnail").toInt();
149 const QString DocClipBase::description() const {
150 return m_properties.value("description");
153 bool DocClipBase::isTransparent() const {
154 return (m_properties.value("transparency") == "1");
157 const QString DocClipBase::getProperty(const QString prop) const {
158 return m_properties.value(prop);
161 void DocClipBase::setDuration(GenTime dur) {
163 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
166 const GenTime &DocClipBase::duration() const {
170 const GenTime &DocClipBase::maxDuration() const {
171 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
172 const GenTime dur(15000, KdenliveSettings::project_fps());
178 bool DocClipBase::hasFileSize() const {
184 QDomElement DocClipBase::toXML() const {
187 QDomElement clip = doc.createElement("producer");
189 QMapIterator<QString, QString> i(m_properties);
190 while (i.hasNext()) {
192 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
194 //doc.appendChild(clip);
195 //kDebug()<<"/// CLIP XML: "<<doc.toString();
199 DocClipBase *DocClipBase::
200 createClip(KdenliveDoc *doc, const QDomElement & element) {
201 DocClipBase *clip = 0;
203 QDomNode node = element;
205 if (element.tagName() != "kdenliveclip") {
207 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
211 QDomNode n = element.firstChild();
213 while (!n.isNull()) {
214 QDomElement e = n.toElement();
216 QString tagName = e.tagName();
217 if (e.tagName() == "avfile") {
218 // clip = DocClipAVFile::createClip(e);
219 } else if (e.tagName() == "DocTrackBaseList") {
220 // clip = DocClipProject::createClip(doc, e);
223 QDomText text = n.toText();
224 if (!text.isNull()) {
225 description = text.nodeValue();
232 kWarning() << "DocClipBase::createClip() unable to create clip";
234 // setup DocClipBase specifics of the clip.
235 QMap <QString, QString> props;
236 props.insert("description", description);
237 clip->setProperties(props);
238 clip->setAudioThumbCreated(false);
243 void DocClipBase::setAudioThumbCreated(bool isDone) {
244 m_audioThumbCreated = isDone;
248 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
249 m_thumbnail = pixmap;
252 const QPixmap & DocClipBase::thumbnail() const {
256 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
257 //kDebug() << "CLIPBASE RECIEDVEDÂ AUDIOÂ DATA*********************************************";
258 audioFrameChache = data;
259 m_audioThumbCreated = true;
263 QList < GenTime > DocClipBase::snapMarkers() const {
264 QList < GenTime > markers;
266 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
267 markers.append(m_snapMarkers[count].time());
273 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
274 return m_snapMarkers;
277 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
278 m_snapMarkers = markers;
281 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
282 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
283 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
284 if ((*it).time() >= time)
288 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
289 (*it).setComment(comment);
290 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
292 CommentedTime t(time, comment);
293 m_snapMarkers.insert(it, t);
298 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
299 QList < CommentedTime >::Iterator it;
300 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
301 if ((*it).time() == time)
304 if (it != m_snapMarkers.end()) {
305 (*it).setComment(comment);
307 kError() << "trying to edit Snap Marker that does not already exists";
311 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
312 QString result = i18n("Marker");
313 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
315 while (itt != m_snapMarkers.end()) {
316 if ((*itt).time() == time)
321 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
322 result = (*itt).comment();
323 m_snapMarkers.erase(itt);
329 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
330 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
332 while (itt != m_snapMarkers.end()) {
333 if ((*itt).time() == time)
341 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
343 for (it = 0; it < m_snapMarkers.count(); it++) {
344 if (m_snapMarkers[it].time() >= currTime)
347 if (it == 0) return GenTime();
348 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
349 return m_snapMarkers[it].time();
350 else return m_snapMarkers[it-1].time();
353 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
355 for (it = 0; it < m_snapMarkers.count(); it++) {
356 if (m_snapMarkers[it].time() > currTime)
359 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
363 QString DocClipBase::markerComment(GenTime t) {
364 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
366 while (itt != m_snapMarkers.end()) {
367 if ((*itt).time() == t)
368 return (*itt).comment();
371 return QString::null;
374 void DocClipBase::deleteProducers() {
375 qDeleteAll(m_baseTrackProducers);
376 m_baseTrackProducers.clear();
377 if (m_thumbProd) m_thumbProd->clearProducer();
380 void DocClipBase::setProducer(Mlt::Producer *producer) {
381 if (producer == NULL) return;
382 QString id = producer->get("id");
383 if (id.contains('_')) {
384 // this is a subtrack producer, insert it at correct place
385 int pos = id.section('_', 1, 1).toInt();
386 if (pos >= m_baseTrackProducers.count()) {
387 while (m_baseTrackProducers.count() - 1 < pos) {
388 m_baseTrackProducers.append(NULL);
391 if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
393 if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
394 else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
396 //m_clipProducer = producer;
397 //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
398 if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
401 Mlt::Producer *DocClipBase::producer(int track) {
402 /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
403 if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
405 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
406 if (m_baseTrackProducers.count() == 0) return NULL;
408 for (int i = 0; i < m_baseTrackProducers.count(); i++)
409 if (m_baseTrackProducers.at(i) != NULL) return m_baseTrackProducers.at(i);
412 if (track >= m_baseTrackProducers.count()) {
413 while (m_baseTrackProducers.count() - 1 < track) {
414 m_baseTrackProducers.append(NULL);
417 if (m_baseTrackProducers.at(track) == NULL) {
419 for (i = 0; i < m_baseTrackProducers.count(); i++)
420 if (m_baseTrackProducers.at(i) != NULL) break;
421 if (i >= m_baseTrackProducers.count()) return NULL;
422 m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
423 if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
424 if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
425 if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
426 if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
427 char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
428 m_baseTrackProducers[track]->set("id", tmp);
430 if (KdenliveSettings::dropbframes() && m_baseTrackProducers.at(i)->get("skip_loop_filter") && strcmp(m_baseTrackProducers.at(i)->get("skip_loop_filter"), "all") == 0) {
431 m_baseTrackProducers[track]->set("skip_loop_filter", "all");
432 m_baseTrackProducers[track]->set("skip_frame", "bidir");
435 return m_baseTrackProducers.at(track);
438 void DocClipBase::setProducerProperty(const char *name, int data) {
439 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
440 if (m_baseTrackProducers.at(i) != NULL)
441 m_baseTrackProducers[i]->set(name, data);
445 void DocClipBase::setProducerProperty(const char *name, const char *data) {
446 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
447 if (m_baseTrackProducers.at(i) != NULL)
448 m_baseTrackProducers[i]->set(name, data);
452 const char *DocClipBase::producerProperty(const char *name) const {
453 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
454 if (m_baseTrackProducers.at(i) != NULL) {
455 return m_baseTrackProducers.at(i)->get(name);
462 void DocClipBase::slotRefreshProducer() {
463 if (m_baseTrackProducers.count() == 0) return;
464 kDebug() << "//////////// REFRESH CLIP !!!!!!!!!!!!!!!!";
465 if (m_clipType == SLIDESHOW) {
466 /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
467 Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
469 delete m_clipProducer;
470 m_clipProducer = new Mlt::Producer(producer.get_producer());
471 if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
472 setProducerProperty("ttl", getProperty("ttl").toInt());
473 //m_clipProducer->set("id", getProperty("id"));
474 if (getProperty("fade") == "1") {
475 // we want a fade filter effect
476 kDebug() << "//////////// FADE WANTED";
477 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
479 Mlt::Filter *filter = clipService.filter(ct);
481 if (filter->get("mlt_service") == "luma") {
485 filter = clipService.filter(ct);
488 if (filter && filter->get("mlt_service") == "luma") {
489 filter->set("period", getProperty("ttl").toInt() - 1);
490 filter->set("luma.out", getProperty("luma_duration").toInt());
491 QString resource = getProperty("luma_file");
492 char *tmp = (char *) qstrdup(resource.toUtf8().data());
493 filter->set("luma.resource", tmp);
495 if (getProperty("softness") != QString()) {
496 int soft = getProperty("softness").toInt();
497 filter->set("luma.softness", (double) soft / 100.0);
500 // filter does not exist, create it...
501 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
502 filter->set("period", getProperty("ttl").toInt() - 1);
503 filter->set("luma.out", getProperty("luma_duration").toInt());
504 QString resource = getProperty("luma_file");
505 char *tmp = (char *) qstrdup(resource.toUtf8().data());
506 filter->set("luma.resource", tmp);
508 if (getProperty("softness") != QString()) {
509 int soft = getProperty("softness").toInt();
510 filter->set("luma.softness", (double) soft / 100.0);
512 clipService.attach(*filter);
515 kDebug() << "//////////// FADE NOT WANTED!!!";
516 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
518 Mlt::Filter *filter = clipService.filter(0);
520 if (filter->get("mlt_service") == "luma") {
521 clipService.detach(*filter);
523 filter = clipService.filter(ct);
529 void DocClipBase::setProperties(QMap <QString, QString> properties) {
530 // changing clip type is not allowed
531 properties.remove("type");
532 QMapIterator<QString, QString> i(properties);
533 bool refreshProducer = false;
535 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
536 while (i.hasNext()) {
538 setProperty(i.key(), i.value());
539 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
541 if (refreshProducer) slotRefreshProducer();
544 void DocClipBase::setMetadata(QMap <QString, QString> properties) {
545 m_metadata = properties;
548 QMap <QString, QString> DocClipBase::metadata() const {
552 void DocClipBase::clearProperty(const QString &key) {
553 m_properties.remove(key);
556 void DocClipBase::getFileHash(const QString &url) {
557 if (m_clipType == SLIDESHOW) return;
559 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
562 //kDebug() << "SETTING HASH of" << value;
563 m_properties.insert("file_size", QString::number(file.size()));
565 * 1 MB = 1 second per 450 files (or faster)
566 * 10 MB = 9 seconds per 450 files (or faster)
568 if (file.size() > 1000000*2) {
569 fileData = file.read(1000000);
570 if (file.seek(file.size() - 1000000))
571 fileData.append(file.readAll());
573 fileData = file.readAll();
575 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
576 m_properties.insert("file_hash", QString(fileHash.toHex()));
577 //kDebug() << file.fileName() << file.size() << fileHash.toHex();
581 QString DocClipBase::getClipHash() const {
583 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
584 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
585 else hash = m_properties.value("file_hash");
589 void DocClipBase::refreshThumbUrl() {
590 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
593 void DocClipBase::setProperty(const QString &key, const QString &value) {
594 m_properties.insert(key, value);
595 if (key == "resource") {
597 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
598 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
599 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
600 else if (key == "colour") {
601 char *tmp = (char *) qstrdup(value.toUtf8().data());
602 setProducerProperty("colour", tmp);
604 } else if (key == "xmldata") {
605 setProducerProperty("force_reload", 1);
606 } else if (key == "force_aspect_ratio") {
607 if (value.isEmpty()) {
608 m_properties.remove("force_aspect_ratio");
609 setProducerProperty("force_aspect_ratio", 0);
610 } else setProducerProperty("force_aspect_ratio", value.toDouble());
611 } else if (key == "threads") {
612 if (value.isEmpty()) {
613 m_properties.remove("threads");
614 setProducerProperty("threads", 1);
615 } else setProducerProperty("threads", value.toInt());
616 } else if (key == "video_index") {
617 if (value.isEmpty()) {
618 m_properties.remove("video_index");
619 setProducerProperty("video_index", m_properties.value("default_video").toInt());
620 } else setProducerProperty("video_index", value.toInt());
621 } else if (key == "audio_index") {
622 if (value.isEmpty()) {
623 m_properties.remove("audio_index");
624 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
625 } else setProducerProperty("audio_index", value.toInt());
629 QMap <QString, QString> DocClipBase::properties() const {
633 bool DocClipBase::slotGetAudioThumbs() {
634 if (m_thumbProd == NULL) return false;
635 if (!KdenliveSettings::audiothumbnails()) {
636 if (m_audioTimer != NULL) m_audioTimer->stop();
639 if (m_audioThumbCreated) {
640 if (m_audioTimer != NULL) m_audioTimer->stop();
643 if (m_audioTimer != NULL)
644 m_audioTimer->start(1500);
645 double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
646 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);