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;
32 QDomNamedNodeMap attributes = xml.attributes();
33 for (unsigned int i = 0; i < attributes.count(); i++) {
34 m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
37 KUrl url = KUrl(xml.attribute("resource"));
38 if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
39 int out = xml.attribute("out").toInt();
41 setDuration(GenTime(out, KdenliveSettings::project_fps()));
43 out = xml.attribute("duration").toInt();
44 if (out != 0) setDuration(GenTime(out, KdenliveSettings::project_fps()));
46 if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
48 //if (!url.isEmpty() && QFile::exists(url.path()))
50 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
51 if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
53 //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
56 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
57 DocClipBase::operator=(clip);
59 m_clipType = clip.clipType();
61 m_duration = clip.duration();
62 m_audioThumbCreated = clip.audioThumbCreated();
63 m_properties = clip.properties();
67 DocClipBase::~DocClipBase() {
75 qDeleteAll(m_baseTrackProducers);
76 m_baseTrackProducers.clear();
79 void DocClipBase::setZone(QPoint zone) {
80 m_properties.insert("zone_in", QString::number(zone.x()));
81 m_properties.insert("zone_out", QString::number(zone.y()));
84 QPoint DocClipBase::zone() const {
86 zone.setX(m_properties.value("zone_in").toInt());
87 zone.setY(m_properties.value("zone_out", "50").toInt());
91 void DocClipBase::slotCreateAudioTimer() {
92 connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
93 connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
94 m_audioTimer = new QTimer(this);
95 connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
98 void DocClipBase::askForAudioThumbs() {
99 if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
102 void DocClipBase::slotClearAudioCache() {
103 if (m_thumbProd) m_thumbProd->stopAudioThumbs();
104 if (m_audioTimer != NULL) m_audioTimer->stop();
105 audioFrameChache.clear();
106 m_audioThumbCreated = false;
109 /*void DocClipBase::getClipMainThumb() {
110 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
113 KThumb *DocClipBase::thumbProducer() {
117 bool DocClipBase::audioThumbCreated() const {
118 return m_audioThumbCreated;
121 const QString & DocClipBase::name() const {
123 return m_properties.value("name");
126 const QString &DocClipBase::getId() const {
130 void DocClipBase::setId(const QString &newId) {
134 const CLIPTYPE & DocClipBase::clipType() const {
138 void DocClipBase::setClipType(CLIPTYPE type) {
141 m_properties.insert("type", QString::number((int) type));
142 if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
143 slotCreateAudioTimer();
146 KUrl DocClipBase::fileURL() const {
147 QString res = m_properties.value("resource");
148 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
152 void DocClipBase::setClipThumbFrame(const uint &ix) {
153 m_properties.insert("thumbnail", QString::number((int) ix));
156 uint DocClipBase::getClipThumbFrame() const {
157 return (uint) m_properties.value("thumbnail").toInt();
160 const QString DocClipBase::description() const {
161 return m_properties.value("description");
164 bool DocClipBase::isTransparent() const {
165 return (m_properties.value("transparency") == "1");
168 const QString DocClipBase::getProperty(const QString prop) const {
169 return m_properties.value(prop);
172 void DocClipBase::setDuration(GenTime dur) {
174 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
177 const GenTime &DocClipBase::duration() const {
181 const GenTime &DocClipBase::maxDuration() const {
182 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
183 const GenTime dur(15000, KdenliveSettings::project_fps());
189 bool DocClipBase::hasFileSize() const {
195 QDomElement DocClipBase::toXML() const {
198 QDomElement clip = doc.createElement("producer");
200 QMapIterator<QString, QString> i(m_properties);
201 while (i.hasNext()) {
203 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
205 //doc.appendChild(clip);
206 //kDebug()<<"/// CLIP XML: "<<doc.toString();
210 DocClipBase *DocClipBase::
211 createClip(KdenliveDoc *doc, const QDomElement & element) {
212 DocClipBase *clip = 0;
214 QDomNode node = element;
216 if (element.tagName() != "kdenliveclip") {
218 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
222 QDomNode n = element.firstChild();
224 while (!n.isNull()) {
225 QDomElement e = n.toElement();
227 QString tagName = e.tagName();
228 if (e.tagName() == "avfile") {
229 // clip = DocClipAVFile::createClip(e);
230 } else if (e.tagName() == "DocTrackBaseList") {
231 // clip = DocClipProject::createClip(doc, e);
234 QDomText text = n.toText();
235 if (!text.isNull()) {
236 description = text.nodeValue();
243 kWarning() << "DocClipBase::createClip() unable to create clip";
245 // setup DocClipBase specifics of the clip.
246 QMap <QString, QString> props;
247 props.insert("description", description);
248 clip->setProperties(props);
249 clip->setAudioThumbCreated(false);
254 void DocClipBase::setAudioThumbCreated(bool isDone) {
255 m_audioThumbCreated = isDone;
259 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
260 m_thumbnail = pixmap;
263 const QPixmap & DocClipBase::thumbnail() const {
267 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
268 //kDebug() << "CLIPBASE RECIEDVEDÂ AUDIOÂ DATA*********************************************";
269 audioFrameChache = data;
270 m_audioThumbCreated = true;
274 QList < GenTime > DocClipBase::snapMarkers() const {
275 QList < GenTime > markers;
277 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
278 markers.append(m_snapMarkers[count].time());
284 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
285 return m_snapMarkers;
288 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
289 m_snapMarkers = markers;
292 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
293 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
294 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
295 if ((*it).time() >= time)
299 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
300 (*it).setComment(comment);
301 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
303 CommentedTime t(time, comment);
304 m_snapMarkers.insert(it, t);
309 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
310 QList < CommentedTime >::Iterator it;
311 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
312 if ((*it).time() == time)
315 if (it != m_snapMarkers.end()) {
316 (*it).setComment(comment);
318 kError() << "trying to edit Snap Marker that does not already exists";
322 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
323 QString result = i18n("Marker");
324 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
326 while (itt != m_snapMarkers.end()) {
327 if ((*itt).time() == time)
332 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
333 result = (*itt).comment();
334 m_snapMarkers.erase(itt);
340 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
341 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
343 while (itt != m_snapMarkers.end()) {
344 if ((*itt).time() == time)
352 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
354 for (it = 0; it < m_snapMarkers.count(); it++) {
355 if (m_snapMarkers[it].time() >= currTime)
358 if (it == 0) return GenTime();
359 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
360 return m_snapMarkers[it].time();
361 else return m_snapMarkers[it-1].time();
364 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
366 for (it = 0; it < m_snapMarkers.count(); it++) {
367 if (m_snapMarkers[it].time() > currTime)
370 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
374 QString DocClipBase::markerComment(GenTime t) {
375 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
377 while (itt != m_snapMarkers.end()) {
378 if ((*itt).time() == t)
379 return (*itt).comment();
382 return QString::null;
385 void DocClipBase::deleteProducers() {
386 qDeleteAll(m_baseTrackProducers);
387 m_baseTrackProducers.clear();
388 if (m_thumbProd) m_thumbProd->clearProducer();
391 void DocClipBase::setProducer(Mlt::Producer *producer) {
392 if (producer == NULL) return;
393 QString id = producer->get("id");
394 if (id.contains('_')) {
395 // this is a subtrack producer, insert it at correct place
396 int pos = id.section('_', 1, 1).toInt();
397 if (pos >= m_baseTrackProducers.count()) {
398 while (m_baseTrackProducers.count() - 1 < pos) {
399 m_baseTrackProducers.append(NULL);
402 if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
404 if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
405 else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
407 //m_clipProducer = producer;
408 //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
409 if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
412 Mlt::Producer *DocClipBase::producer(int track) {
413 /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
414 if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
416 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
417 if (m_baseTrackProducers.count() == 0) return NULL;
419 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
420 if (m_baseTrackProducers.at(i) != NULL)
421 return m_baseTrackProducers.at(i);
425 if (track >= m_baseTrackProducers.count()) {
426 while (m_baseTrackProducers.count() - 1 < track) {
427 m_baseTrackProducers.append(NULL);
430 if (m_baseTrackProducers.at(track) == NULL) {
432 for (i = 0; i < m_baseTrackProducers.count(); i++)
433 if (m_baseTrackProducers.at(i) != NULL) break;
434 if (i >= m_baseTrackProducers.count()) return NULL;
435 m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
436 if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
437 if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
438 if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
439 if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
440 char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
441 m_baseTrackProducers[track]->set("id", tmp);
443 if (KdenliveSettings::dropbframes() && m_baseTrackProducers.at(i)->get("skip_loop_filter") && strcmp(m_baseTrackProducers.at(i)->get("skip_loop_filter"), "all") == 0) {
444 m_baseTrackProducers[track]->set("skip_loop_filter", "all");
445 m_baseTrackProducers[track]->set("skip_frame", "bidir");
448 return m_baseTrackProducers.at(track);
451 void DocClipBase::setProducerProperty(const char *name, int data) {
452 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
453 if (m_baseTrackProducers.at(i) != NULL)
454 m_baseTrackProducers[i]->set(name, data);
458 void DocClipBase::setProducerProperty(const char *name, const char *data) {
459 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
460 if (m_baseTrackProducers.at(i) != NULL)
461 m_baseTrackProducers[i]->set(name, data);
465 const char *DocClipBase::producerProperty(const char *name) const {
466 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
467 if (m_baseTrackProducers.at(i) != NULL) {
468 return m_baseTrackProducers.at(i)->get(name);
475 void DocClipBase::slotRefreshProducer() {
476 if (m_baseTrackProducers.count() == 0) return;
477 kDebug() << "//////////// REFRESH CLIP !!!!!!!!!!!!!!!!";
478 if (m_clipType == SLIDESHOW) {
479 /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
480 Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
482 delete m_clipProducer;
483 m_clipProducer = new Mlt::Producer(producer.get_producer());
484 if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
485 setProducerProperty("ttl", getProperty("ttl").toInt());
486 //m_clipProducer->set("id", getProperty("id"));
487 if (getProperty("fade") == "1") {
488 // we want a fade filter effect
489 kDebug() << "//////////// FADE WANTED";
490 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
492 Mlt::Filter *filter = clipService.filter(ct);
494 if (filter->get("mlt_service") == "luma") {
498 filter = clipService.filter(ct);
501 if (filter && filter->get("mlt_service") == "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);
513 // filter does not exist, create it...
514 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
515 filter->set("period", getProperty("ttl").toInt() - 1);
516 filter->set("luma.out", getProperty("luma_duration").toInt());
517 QString resource = getProperty("luma_file");
518 char *tmp = (char *) qstrdup(resource.toUtf8().data());
519 filter->set("luma.resource", tmp);
521 if (getProperty("softness") != QString()) {
522 int soft = getProperty("softness").toInt();
523 filter->set("luma.softness", (double) soft / 100.0);
525 clipService.attach(*filter);
528 kDebug() << "//////////// FADE NOT WANTED!!!";
529 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
531 Mlt::Filter *filter = clipService.filter(0);
533 if (filter->get("mlt_service") == "luma") {
534 clipService.detach(*filter);
536 filter = clipService.filter(ct);
542 void DocClipBase::setProperties(QMap <QString, QString> properties) {
543 // changing clip type is not allowed
544 properties.remove("type");
545 QMapIterator<QString, QString> i(properties);
546 bool refreshProducer = false;
548 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
549 while (i.hasNext()) {
551 setProperty(i.key(), i.value());
552 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
554 if (refreshProducer) slotRefreshProducer();
557 void DocClipBase::setMetadata(QMap <QString, QString> properties) {
558 m_metadata = properties;
561 QMap <QString, QString> DocClipBase::metadata() const {
565 void DocClipBase::clearProperty(const QString &key) {
566 m_properties.remove(key);
569 void DocClipBase::getFileHash(const QString &url) {
570 if (m_clipType == SLIDESHOW) return;
572 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
575 //kDebug() << "SETTING HASH of" << value;
576 m_properties.insert("file_size", QString::number(file.size()));
578 * 1 MB = 1 second per 450 files (or faster)
579 * 10 MB = 9 seconds per 450 files (or faster)
581 if (file.size() > 1000000*2) {
582 fileData = file.read(1000000);
583 if (file.seek(file.size() - 1000000))
584 fileData.append(file.readAll());
586 fileData = file.readAll();
588 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
589 m_properties.insert("file_hash", QString(fileHash.toHex()));
590 //kDebug() << file.fileName() << file.size() << fileHash.toHex();
594 QString DocClipBase::getClipHash() const {
596 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
597 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
598 else hash = m_properties.value("file_hash");
602 void DocClipBase::refreshThumbUrl() {
603 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
606 void DocClipBase::setProperty(const QString &key, const QString &value) {
607 m_properties.insert(key, value);
608 if (key == "resource") {
610 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
611 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
612 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
613 else if (key == "colour") {
614 char *tmp = (char *) qstrdup(value.toUtf8().data());
615 setProducerProperty("colour", tmp);
617 } else if (key == "xmldata") {
618 setProducerProperty("force_reload", 1);
619 } else if (key == "force_aspect_ratio") {
620 if (value.isEmpty()) {
621 m_properties.remove("force_aspect_ratio");
622 setProducerProperty("force_aspect_ratio", 0);
623 } else setProducerProperty("force_aspect_ratio", value.toDouble());
624 } else if (key == "threads") {
625 if (value.isEmpty()) {
626 m_properties.remove("threads");
627 setProducerProperty("threads", 1);
628 } else setProducerProperty("threads", value.toInt());
629 } else if (key == "video_index") {
630 if (value.isEmpty()) {
631 m_properties.remove("video_index");
632 setProducerProperty("video_index", m_properties.value("default_video").toInt());
633 } else setProducerProperty("video_index", value.toInt());
634 } else if (key == "audio_index") {
635 if (value.isEmpty()) {
636 m_properties.remove("audio_index");
637 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
638 } else setProducerProperty("audio_index", value.toInt());
642 QMap <QString, QString> DocClipBase::properties() const {
646 bool DocClipBase::slotGetAudioThumbs() {
647 if (m_thumbProd == NULL) return false;
648 if (!KdenliveSettings::audiothumbnails()) {
649 if (m_audioTimer != NULL) m_audioTimer->stop();
652 if (m_audioThumbCreated) {
653 if (m_audioTimer != NULL) m_audioTimer->stop();
656 if (m_audioTimer != NULL)
657 m_audioTimer->start(1500);
658 double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
659 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);