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 ***************************************************************************/
20 #include "kdenlivesettings.h"
21 #include "docclipbase.h"
23 #include "clipmanager.h"
25 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id):
26 m_id(id), m_description(QString()), m_refcount(0), m_audioThumbCreated(false), m_duration(GenTime()), m_thumbProd(NULL), m_audioTimer(NULL), m_clipProducer(NULL), m_properties(QMap <QString, QString> ()), audioFrameChache(QMap<int, QMap<int, QByteArray> > ()) {
27 int type = xml.attribute("type").toInt();
28 m_clipType = (CLIPTYPE) type;
29 m_name = xml.attribute("name");
31 QDomNamedNodeMap attributes = xml.attributes();
32 for (unsigned int i = 0; i < attributes.count(); i++) {
33 m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
36 KUrl url = KUrl(xml.attribute("resource"));
37 int out = xml.attribute("out").toInt();
39 setDuration(GenTime(out, KdenliveSettings::project_fps()));
41 out = xml.attribute("duration").toInt();
42 if (out != 0) setDuration(GenTime(out, KdenliveSettings::project_fps()));
44 if (m_name.isEmpty()) m_name = url.fileName();
46 //if (!url.isEmpty() && QFile::exists(url.path()))
48 m_thumbProd = new KThumb(clipManager, url, m_id);
49 if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
51 //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
54 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
55 DocClipBase::operator=(clip);
57 m_clipType = clip.clipType();
59 m_duration = clip.duration();
60 m_audioThumbCreated = clip.audioThumbCreated();
61 m_properties = clip.properties();
65 DocClipBase::~DocClipBase() {
66 if (m_thumbProd) delete m_thumbProd;
67 if (m_clipProducer) delete m_clipProducer;
70 void DocClipBase::slotCreateAudioTimer() {
71 connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
72 connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
73 m_audioTimer = new QTimer(this);
74 connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
77 void DocClipBase::askForAudioThumbs() {
78 if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
81 void DocClipBase::slotClearAudioCache() {
82 if (m_thumbProd) m_thumbProd->stopAudioThumbs();
83 if (m_audioTimer != NULL) m_audioTimer->stop();
84 audioFrameChache.clear();
85 m_audioThumbCreated = false;
88 /*void DocClipBase::getClipMainThumb() {
89 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
92 KThumb *DocClipBase::thumbProducer() {
96 bool DocClipBase::audioThumbCreated() const {
97 return m_audioThumbCreated;
100 void DocClipBase::setName(const QString name) {
104 const QString & DocClipBase::name() const {
109 const QString &DocClipBase::getId() const {
113 void DocClipBase::setId(const QString &newId) {
117 const CLIPTYPE & DocClipBase::clipType() const {
121 void DocClipBase::setClipType(CLIPTYPE type) {
123 m_properties.insert("type", QString::number((int) type));
124 if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
125 slotCreateAudioTimer();
128 KUrl DocClipBase::fileURL() const {
129 QString res = m_properties.value("resource");
130 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
134 void DocClipBase::setClipThumbFrame(const uint &ix) {
135 m_properties.insert("thumbnail", QString::number((int) ix));
138 uint DocClipBase::getClipThumbFrame() const {
139 return (uint) m_properties.value("thumbnail").toInt();
142 const QString DocClipBase::description() const {
143 return m_properties.value("description");
146 bool DocClipBase::isTransparent() const {
147 return (m_properties.value("transparency") == "1");
150 const QString DocClipBase::getProperty(const QString prop) const {
151 return m_properties.value(prop);
154 void DocClipBase::setDuration(GenTime dur) {
156 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
159 const GenTime &DocClipBase::duration() const {
163 const GenTime &DocClipBase::maxDuration() const {
164 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
165 const GenTime dur(10000, KdenliveSettings::project_fps());
171 bool DocClipBase::hasFileSize() const {
177 QDomElement DocClipBase::toXML() const {
180 QDomElement clip = doc.createElement("producer");
182 QMapIterator<QString, QString> i(m_properties);
183 while (i.hasNext()) {
185 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
187 //doc.appendChild(clip);
188 //kDebug()<<"/// CLIP XML: "<<doc.toString();
192 DocClipBase *DocClipBase::
193 createClip(KdenliveDoc *doc, const QDomElement & element) {
194 DocClipBase *clip = 0;
196 QDomNode node = element;
198 if (element.tagName() != "kdenliveclip") {
200 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
204 QDomNode n = element.firstChild();
206 while (!n.isNull()) {
207 QDomElement e = n.toElement();
209 QString tagName = e.tagName();
210 if (e.tagName() == "avfile") {
211 // clip = DocClipAVFile::createClip(e);
212 } else if (e.tagName() == "DocTrackBaseList") {
213 // clip = DocClipProject::createClip(doc, e);
216 QDomText text = n.toText();
217 if (!text.isNull()) {
218 description = text.nodeValue();
225 kWarning() << "DocClipBase::createClip() unable to create clip";
227 // setup DocClipBase specifics of the clip.
228 QMap <QString, QString> props;
229 props.insert("description", description);
230 clip->setProperties(props);
231 clip->setAudioThumbCreated(false);
236 void DocClipBase::setAudioThumbCreated(bool isDone) {
237 m_audioThumbCreated = isDone;
241 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
242 m_thumbnail = pixmap;
245 const QPixmap & DocClipBase::thumbnail() const {
249 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
250 kDebug() << "CLIPBASE RECIEDVEDÂ AUDIOÂ DATA*********************************************";
251 audioFrameChache = data;
252 m_audioThumbCreated = true;
256 QList < GenTime > DocClipBase::snapMarkers() const {
257 QList < GenTime > markers;
259 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
260 markers.append(m_snapMarkers[count].time());
266 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
267 return m_snapMarkers;
270 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
271 m_snapMarkers = markers;
274 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
275 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
276 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
277 if ((*it).time() >= time)
281 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
282 (*it).setComment(comment);
283 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
285 CommentedTime t(time, comment);
286 m_snapMarkers.insert(it, t);
291 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
292 QList < CommentedTime >::Iterator it;
293 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
294 if ((*it).time() == time)
297 if (it != m_snapMarkers.end()) {
298 (*it).setComment(comment);
300 kError() << "trying to edit Snap Marker that does not already exists";
304 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
305 QString result = i18n("Marker");
306 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
308 while (itt != m_snapMarkers.end()) {
309 if ((*itt).time() == time)
314 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
315 result = (*itt).comment();
316 m_snapMarkers.erase(itt);
322 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
323 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
325 while (itt != m_snapMarkers.end()) {
326 if ((*itt).time() == time)
334 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
336 for (it = 0; it < m_snapMarkers.count(); it++) {
337 if (m_snapMarkers[it].time() >= currTime)
340 if (it == 0) return GenTime();
341 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
342 return m_snapMarkers[it].time();
343 else return m_snapMarkers[it-1].time();
346 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
348 for (it = 0; it < m_snapMarkers.count(); it++) {
349 if (m_snapMarkers[it].time() > currTime)
352 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
356 QString DocClipBase::markerComment(GenTime t) {
357 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
359 while (itt != m_snapMarkers.end()) {
360 if ((*itt).time() == t)
361 return (*itt).comment();
364 return QString::null;
367 void DocClipBase::setProducer(Mlt::Producer *producer) {
368 if (producer == NULL) return;
369 m_clipProducer = producer;
370 m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
371 if (m_thumbProd) m_thumbProd->setProducer(producer);
374 Mlt::Producer *DocClipBase::producer() {
375 return m_clipProducer;
378 void DocClipBase::slotRefreshProducer() {
379 if (m_clipProducer == NULL) return;
380 kDebug() << "//////////// REFRESH CLIP !!!!!!!!!!!!!!!!";
381 if (m_clipType == SLIDESHOW) {
382 /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
383 Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
385 delete m_clipProducer;
386 m_clipProducer = new Mlt::Producer(producer.get_producer());
387 if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
388 m_clipProducer->set("ttl", getProperty("ttl").toInt());
389 //m_clipProducer->set("id", getProperty("id"));
390 if (getProperty("fade") == "1") {
391 // we want a fade filter effect
392 kDebug() << "//////////// FADE WANTED";
393 Mlt::Service clipService(m_clipProducer->get_service());
395 Mlt::Filter *filter = clipService.filter(ct);
397 if (filter->get("mlt_service") == "luma") {
401 filter = clipService.filter(ct);
404 if (filter && filter->get("mlt_service") == "luma") {
405 filter->set("period", getProperty("ttl").toInt() - 1);
406 filter->set("luma.out", getProperty("luma_duration").toInt());
407 QString resource = getProperty("luma_file");
408 char *tmp = (char *) qstrdup(resource.toUtf8().data());
409 filter->set("luma.resource", tmp);
411 if (getProperty("softness") != QString()) {
412 int soft = getProperty("softness").toInt();
413 filter->set("luma.softness", (double) soft / 100.0);
416 // filter does not exist, create it...
417 Mlt::Filter *filter = new Mlt::Filter(*(m_clipProducer->profile()), "luma");
418 filter->set("period", getProperty("ttl").toInt() - 1);
419 filter->set("luma.out", getProperty("luma_duration").toInt());
420 QString resource = getProperty("luma_file");
421 char *tmp = (char *) qstrdup(resource.toUtf8().data());
422 filter->set("luma.resource", tmp);
424 if (getProperty("softness") != QString()) {
425 int soft = getProperty("softness").toInt();
426 filter->set("luma.softness", (double) soft / 100.0);
428 clipService.attach(*filter);
431 kDebug() << "//////////// FADE NOT WANTED!!!";
432 Mlt::Service clipService(m_clipProducer->get_service());
434 Mlt::Filter *filter = clipService.filter(0);
436 if (filter->get("mlt_service") == "luma") {
437 clipService.detach(*filter);
439 filter = clipService.filter(ct);
445 void DocClipBase::setProperties(QMap <QString, QString> properties) {
446 // changing clip type is not allowed
447 properties.remove("type");
448 QMapIterator<QString, QString> i(properties);
449 bool refreshProducer = false;
451 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
452 while (i.hasNext()) {
454 setProperty(i.key(), i.value());
455 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
457 if (refreshProducer) slotRefreshProducer();
460 void DocClipBase::clearProperty(const QString &key) {
461 m_properties.remove(key);
464 void DocClipBase::setProperty(const QString &key, const QString &value) {
465 m_properties.insert(key, value);
466 if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
467 else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
468 else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
469 else if (key == "colour") {
470 char *tmp = (char *) qstrdup(value.toUtf8().data());
471 m_clipProducer->set("colour", tmp);
473 } else if (key == "xmldata") {
474 m_clipProducer->set("force_reload", 1);
475 } else if (key == "force_aspect_ratio") {
476 if (value.isEmpty()) {
477 m_properties.remove("force_aspect_ratio");
478 m_clipProducer->set("force_aspect_ratio", 0);
479 } else m_clipProducer->set("force_aspect_ratio", value.toDouble());
480 } else if (key == "threads") {
481 if (value.isEmpty()) {
482 m_properties.remove("threads");
483 m_clipProducer->set("threads", 1);
484 } else m_clipProducer->set("threads", value.toInt());
485 } else if (key == "video_index") {
486 if (value.isEmpty()) {
487 m_properties.remove("video_index");
488 m_clipProducer->set("video_index", m_properties.value("default_video").toInt());
489 } else m_clipProducer->set("video_index", value.toInt());
490 } else if (key == "audio_index") {
491 if (value.isEmpty()) {
492 m_properties.remove("audio_index");
493 m_clipProducer->set("audio_index", m_properties.value("default_audio").toInt());
494 } else m_clipProducer->set("audio_index", value.toInt());
498 QMap <QString, QString> DocClipBase::properties() const {
502 bool DocClipBase::slotGetAudioThumbs() {
503 if (m_thumbProd == NULL) return false;
504 if (m_audioThumbCreated) {
505 if (m_audioTimer != NULL) m_audioTimer->stop();
508 if (m_audioTimer != NULL)
509 m_audioTimer->start(1500);
510 double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
511 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);