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 *>()) {
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);
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() {
69 if (m_thumbProd) delete m_thumbProd;
70 qDeleteAll(m_baseTrackProducers);
73 void DocClipBase::slotCreateAudioTimer() {
74 connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
75 connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
76 m_audioTimer = new QTimer(this);
77 connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
80 void DocClipBase::askForAudioThumbs() {
81 if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
84 void DocClipBase::slotClearAudioCache() {
85 if (m_thumbProd) m_thumbProd->stopAudioThumbs();
86 if (m_audioTimer != NULL) m_audioTimer->stop();
87 audioFrameChache.clear();
88 m_audioThumbCreated = false;
91 /*void DocClipBase::getClipMainThumb() {
92 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
95 KThumb *DocClipBase::thumbProducer() {
99 bool DocClipBase::audioThumbCreated() const {
100 return m_audioThumbCreated;
103 void DocClipBase::setName(const QString name) {
107 const QString & DocClipBase::name() const {
112 const QString &DocClipBase::getId() const {
116 void DocClipBase::setId(const QString &newId) {
120 const CLIPTYPE & DocClipBase::clipType() const {
124 void DocClipBase::setClipType(CLIPTYPE type) {
127 m_properties.insert("type", QString::number((int) type));
128 if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
129 slotCreateAudioTimer();
132 KUrl DocClipBase::fileURL() const {
133 QString res = m_properties.value("resource");
134 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
138 void DocClipBase::setClipThumbFrame(const uint &ix) {
139 m_properties.insert("thumbnail", QString::number((int) ix));
142 uint DocClipBase::getClipThumbFrame() const {
143 return (uint) m_properties.value("thumbnail").toInt();
146 const QString DocClipBase::description() const {
147 return m_properties.value("description");
150 bool DocClipBase::isTransparent() const {
151 return (m_properties.value("transparency") == "1");
154 const QString DocClipBase::getProperty(const QString prop) const {
155 return m_properties.value(prop);
158 void DocClipBase::setDuration(GenTime dur) {
160 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
163 const GenTime &DocClipBase::duration() const {
167 const GenTime &DocClipBase::maxDuration() const {
168 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
169 const GenTime dur(10000, KdenliveSettings::project_fps());
175 bool DocClipBase::hasFileSize() const {
181 QDomElement DocClipBase::toXML() const {
184 QDomElement clip = doc.createElement("producer");
186 QMapIterator<QString, QString> i(m_properties);
187 while (i.hasNext()) {
189 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
191 //doc.appendChild(clip);
192 //kDebug()<<"/// CLIP XML: "<<doc.toString();
196 DocClipBase *DocClipBase::
197 createClip(KdenliveDoc *doc, const QDomElement & element) {
198 DocClipBase *clip = 0;
200 QDomNode node = element;
202 if (element.tagName() != "kdenliveclip") {
204 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
208 QDomNode n = element.firstChild();
210 while (!n.isNull()) {
211 QDomElement e = n.toElement();
213 QString tagName = e.tagName();
214 if (e.tagName() == "avfile") {
215 // clip = DocClipAVFile::createClip(e);
216 } else if (e.tagName() == "DocTrackBaseList") {
217 // clip = DocClipProject::createClip(doc, e);
220 QDomText text = n.toText();
221 if (!text.isNull()) {
222 description = text.nodeValue();
229 kWarning() << "DocClipBase::createClip() unable to create clip";
231 // setup DocClipBase specifics of the clip.
232 QMap <QString, QString> props;
233 props.insert("description", description);
234 clip->setProperties(props);
235 clip->setAudioThumbCreated(false);
240 void DocClipBase::setAudioThumbCreated(bool isDone) {
241 m_audioThumbCreated = isDone;
245 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
246 m_thumbnail = pixmap;
249 const QPixmap & DocClipBase::thumbnail() const {
253 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
254 //kDebug() << "CLIPBASE RECIEDVEDÂ AUDIOÂ DATA*********************************************";
255 audioFrameChache = data;
256 m_audioThumbCreated = true;
260 QList < GenTime > DocClipBase::snapMarkers() const {
261 QList < GenTime > markers;
263 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
264 markers.append(m_snapMarkers[count].time());
270 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
271 return m_snapMarkers;
274 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
275 m_snapMarkers = markers;
278 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
279 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
280 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
281 if ((*it).time() >= time)
285 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
286 (*it).setComment(comment);
287 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
289 CommentedTime t(time, comment);
290 m_snapMarkers.insert(it, t);
295 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
296 QList < CommentedTime >::Iterator it;
297 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
298 if ((*it).time() == time)
301 if (it != m_snapMarkers.end()) {
302 (*it).setComment(comment);
304 kError() << "trying to edit Snap Marker that does not already exists";
308 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
309 QString result = i18n("Marker");
310 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
312 while (itt != m_snapMarkers.end()) {
313 if ((*itt).time() == time)
318 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
319 result = (*itt).comment();
320 m_snapMarkers.erase(itt);
326 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
327 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
329 while (itt != m_snapMarkers.end()) {
330 if ((*itt).time() == time)
338 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
340 for (it = 0; it < m_snapMarkers.count(); it++) {
341 if (m_snapMarkers[it].time() >= currTime)
344 if (it == 0) return GenTime();
345 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
346 return m_snapMarkers[it].time();
347 else return m_snapMarkers[it-1].time();
350 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
352 for (it = 0; it < m_snapMarkers.count(); it++) {
353 if (m_snapMarkers[it].time() > currTime)
356 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
360 QString DocClipBase::markerComment(GenTime t) {
361 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
363 while (itt != m_snapMarkers.end()) {
364 if ((*itt).time() == t)
365 return (*itt).comment();
368 return QString::null;
371 void DocClipBase::setProducer(Mlt::Producer *producer) {
372 if (producer == NULL) return;
373 QString id = producer->get("id");
374 if (id.contains('_')) {
375 // this is a subtrack producer, insert it at correct place
376 int pos = id.section('_', 1, 1).toInt();
377 if (pos >= m_baseTrackProducers.count()) {
378 while (m_baseTrackProducers.count() - 1 < pos) {
379 m_baseTrackProducers.append(NULL);
382 if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
384 if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
385 else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
387 //m_clipProducer = producer;
388 //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
389 if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
392 Mlt::Producer *DocClipBase::producer(int track) {
393 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
394 if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
396 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
397 if (m_baseTrackProducers.count() == 0) return NULL;
399 for (i = 0; i < m_baseTrackProducers.count(); i++)
400 if (m_baseTrackProducers.at(i) != NULL) break;
401 if (i < m_baseTrackProducers.count()) return m_baseTrackProducers.at(i);
404 if (track >= m_baseTrackProducers.count()) {
405 while (m_baseTrackProducers.count() - 1 < track) {
406 m_baseTrackProducers.append(NULL);
409 if (m_baseTrackProducers.at(track) == NULL) {
411 for (i = 0; i < m_baseTrackProducers.count(); i++)
412 if (m_baseTrackProducers.at(i) != NULL) break;
413 if (i >= m_baseTrackProducers.count()) return NULL;
414 m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
415 if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
416 if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
417 if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
418 if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
419 char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
420 m_baseTrackProducers[track]->set("id", tmp);
423 return m_baseTrackProducers.at(track);
426 void DocClipBase::setProducerProperty(const char *name, int data) {
427 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
428 if (m_baseTrackProducers.at(i) != NULL)
429 m_baseTrackProducers[i]->set(name, data);
433 void DocClipBase::setProducerProperty(const char *name, const char *data) {
434 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
435 if (m_baseTrackProducers.at(i) != NULL)
436 m_baseTrackProducers[i]->set(name, data);
440 void DocClipBase::slotRefreshProducer() {
441 if (m_baseTrackProducers.count() == 0) return;
442 kDebug() << "//////////// REFRESH CLIP !!!!!!!!!!!!!!!!";
443 if (m_clipType == SLIDESHOW) {
444 /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
445 Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
447 delete m_clipProducer;
448 m_clipProducer = new Mlt::Producer(producer.get_producer());
449 if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
450 setProducerProperty("ttl", getProperty("ttl").toInt());
451 //m_clipProducer->set("id", getProperty("id"));
452 if (getProperty("fade") == "1") {
453 // we want a fade filter effect
454 kDebug() << "//////////// FADE WANTED";
455 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
457 Mlt::Filter *filter = clipService.filter(ct);
459 if (filter->get("mlt_service") == "luma") {
463 filter = clipService.filter(ct);
466 if (filter && filter->get("mlt_service") == "luma") {
467 filter->set("period", getProperty("ttl").toInt() - 1);
468 filter->set("luma.out", getProperty("luma_duration").toInt());
469 QString resource = getProperty("luma_file");
470 char *tmp = (char *) qstrdup(resource.toUtf8().data());
471 filter->set("luma.resource", tmp);
473 if (getProperty("softness") != QString()) {
474 int soft = getProperty("softness").toInt();
475 filter->set("luma.softness", (double) soft / 100.0);
478 // filter does not exist, create it...
479 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
480 filter->set("period", getProperty("ttl").toInt() - 1);
481 filter->set("luma.out", getProperty("luma_duration").toInt());
482 QString resource = getProperty("luma_file");
483 char *tmp = (char *) qstrdup(resource.toUtf8().data());
484 filter->set("luma.resource", tmp);
486 if (getProperty("softness") != QString()) {
487 int soft = getProperty("softness").toInt();
488 filter->set("luma.softness", (double) soft / 100.0);
490 clipService.attach(*filter);
493 kDebug() << "//////////// FADE NOT WANTED!!!";
494 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
496 Mlt::Filter *filter = clipService.filter(0);
498 if (filter->get("mlt_service") == "luma") {
499 clipService.detach(*filter);
501 filter = clipService.filter(ct);
507 void DocClipBase::setProperties(QMap <QString, QString> properties) {
508 // changing clip type is not allowed
509 properties.remove("type");
510 QMapIterator<QString, QString> i(properties);
511 bool refreshProducer = false;
513 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
514 while (i.hasNext()) {
516 setProperty(i.key(), i.value());
517 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
519 if (refreshProducer) slotRefreshProducer();
522 void DocClipBase::clearProperty(const QString &key) {
523 m_properties.remove(key);
526 void DocClipBase::getFileHash(const QString &url) {
527 if (m_clipType == SLIDESHOW) return;
529 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
532 //kDebug() << "SETTING HASH of" << value;
533 m_properties.insert("file_size", QString::number(file.size()));
535 * 1 MB = 1 second per 450 files (or faster)
536 * 10 MB = 9 seconds per 450 files (or faster)
538 if (file.size() > 1000000*2) {
539 fileData = file.read(1000000);
540 if (file.seek(file.size() - 1000000))
541 fileData.append(file.readAll());
543 fileData = file.readAll();
545 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
546 m_properties.insert("file_hash", QString(fileHash.toHex()));
547 //kDebug() << file.fileName() << file.size() << fileHash.toHex();
551 void DocClipBase::setProperty(const QString &key, const QString &value) {
552 m_properties.insert(key, value);
553 if (key == "resource") {
554 m_thumbProd->updateClipUrl(KUrl(value));
556 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
557 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
558 else if (key == "colour") {
559 char *tmp = (char *) qstrdup(value.toUtf8().data());
560 setProducerProperty("colour", tmp);
562 } else if (key == "xmldata") {
563 setProducerProperty("force_reload", 1);
564 } else if (key == "force_aspect_ratio") {
565 if (value.isEmpty()) {
566 m_properties.remove("force_aspect_ratio");
567 setProducerProperty("force_aspect_ratio", 0);
568 } else setProducerProperty("force_aspect_ratio", value.toDouble());
569 } else if (key == "threads") {
570 if (value.isEmpty()) {
571 m_properties.remove("threads");
572 setProducerProperty("threads", 1);
573 } else setProducerProperty("threads", value.toInt());
574 } else if (key == "video_index") {
575 if (value.isEmpty()) {
576 m_properties.remove("video_index");
577 setProducerProperty("video_index", m_properties.value("default_video").toInt());
578 } else setProducerProperty("video_index", value.toInt());
579 } else if (key == "audio_index") {
580 if (value.isEmpty()) {
581 m_properties.remove("audio_index");
582 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
583 } else setProducerProperty("audio_index", value.toInt());
587 QMap <QString, QString> DocClipBase::properties() const {
591 bool DocClipBase::slotGetAudioThumbs() {
592 if (m_thumbProd == NULL) return false;
593 if (m_audioThumbCreated) {
594 if (m_audioTimer != NULL) m_audioTimer->stop();
597 if (m_audioTimer != NULL)
598 m_audioTimer->start(1500);
599 double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
600 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);