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_properties(QMap <QString, QString> ()), audioFrameChache(QMap<int, QMap<int, QByteArray> > ()), m_baseTrackProducers(QList <Mlt::Producer *>()) {
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 qDeleteAll(m_baseTrackProducers);
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) {
124 m_properties.insert("type", QString::number((int) type));
125 if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
126 slotCreateAudioTimer();
129 KUrl DocClipBase::fileURL() const {
130 QString res = m_properties.value("resource");
131 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
135 void DocClipBase::setClipThumbFrame(const uint &ix) {
136 m_properties.insert("thumbnail", QString::number((int) ix));
139 uint DocClipBase::getClipThumbFrame() const {
140 return (uint) m_properties.value("thumbnail").toInt();
143 const QString DocClipBase::description() const {
144 return m_properties.value("description");
147 bool DocClipBase::isTransparent() const {
148 return (m_properties.value("transparency") == "1");
151 const QString DocClipBase::getProperty(const QString prop) const {
152 return m_properties.value(prop);
155 void DocClipBase::setDuration(GenTime dur) {
157 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
160 const GenTime &DocClipBase::duration() const {
164 const GenTime &DocClipBase::maxDuration() const {
165 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
166 const GenTime dur(10000, KdenliveSettings::project_fps());
172 bool DocClipBase::hasFileSize() const {
178 QDomElement DocClipBase::toXML() const {
181 QDomElement clip = doc.createElement("producer");
183 QMapIterator<QString, QString> i(m_properties);
184 while (i.hasNext()) {
186 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
188 //doc.appendChild(clip);
189 //kDebug()<<"/// CLIP XML: "<<doc.toString();
193 DocClipBase *DocClipBase::
194 createClip(KdenliveDoc *doc, const QDomElement & element) {
195 DocClipBase *clip = 0;
197 QDomNode node = element;
199 if (element.tagName() != "kdenliveclip") {
201 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
205 QDomNode n = element.firstChild();
207 while (!n.isNull()) {
208 QDomElement e = n.toElement();
210 QString tagName = e.tagName();
211 if (e.tagName() == "avfile") {
212 // clip = DocClipAVFile::createClip(e);
213 } else if (e.tagName() == "DocTrackBaseList") {
214 // clip = DocClipProject::createClip(doc, e);
217 QDomText text = n.toText();
218 if (!text.isNull()) {
219 description = text.nodeValue();
226 kWarning() << "DocClipBase::createClip() unable to create clip";
228 // setup DocClipBase specifics of the clip.
229 QMap <QString, QString> props;
230 props.insert("description", description);
231 clip->setProperties(props);
232 clip->setAudioThumbCreated(false);
237 void DocClipBase::setAudioThumbCreated(bool isDone) {
238 m_audioThumbCreated = isDone;
242 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
243 m_thumbnail = pixmap;
246 const QPixmap & DocClipBase::thumbnail() const {
250 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
251 kDebug() << "CLIPBASE RECIEDVEDÂ AUDIOÂ DATA*********************************************";
252 audioFrameChache = data;
253 m_audioThumbCreated = true;
257 QList < GenTime > DocClipBase::snapMarkers() const {
258 QList < GenTime > markers;
260 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
261 markers.append(m_snapMarkers[count].time());
267 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
268 return m_snapMarkers;
271 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
272 m_snapMarkers = markers;
275 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
276 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
277 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
278 if ((*it).time() >= time)
282 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
283 (*it).setComment(comment);
284 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
286 CommentedTime t(time, comment);
287 m_snapMarkers.insert(it, t);
292 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
293 QList < CommentedTime >::Iterator it;
294 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
295 if ((*it).time() == time)
298 if (it != m_snapMarkers.end()) {
299 (*it).setComment(comment);
301 kError() << "trying to edit Snap Marker that does not already exists";
305 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
306 QString result = i18n("Marker");
307 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
309 while (itt != m_snapMarkers.end()) {
310 if ((*itt).time() == time)
315 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
316 result = (*itt).comment();
317 m_snapMarkers.erase(itt);
323 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
324 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
326 while (itt != m_snapMarkers.end()) {
327 if ((*itt).time() == time)
335 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
337 for (it = 0; it < m_snapMarkers.count(); it++) {
338 if (m_snapMarkers[it].time() >= currTime)
341 if (it == 0) return GenTime();
342 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
343 return m_snapMarkers[it].time();
344 else return m_snapMarkers[it-1].time();
347 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
349 for (it = 0; it < m_snapMarkers.count(); it++) {
350 if (m_snapMarkers[it].time() > currTime)
353 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
357 QString DocClipBase::markerComment(GenTime t) {
358 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
360 while (itt != m_snapMarkers.end()) {
361 if ((*itt).time() == t)
362 return (*itt).comment();
365 return QString::null;
368 void DocClipBase::setProducer(Mlt::Producer *producer) {
369 if (producer == NULL) return;
370 QString id = producer->get("id");
371 kDebug() << "// SET PRODUCER: " << id;
372 if (id.contains('_')) {
373 // this is a subtrack producer, insert it at correct place
374 int pos = id.section('_', 1, 1).toInt();
375 kDebug() << "// POS = " << pos << ", MAX: " << m_baseTrackProducers.count();
376 if (pos >= m_baseTrackProducers.count()) {
377 while (m_baseTrackProducers.count() - 1 < pos) {
378 m_baseTrackProducers.append(NULL);
381 kDebug() << "// POS = " << pos << ", NEW MAX: " << m_baseTrackProducers.count();
382 if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
383 } else m_baseTrackProducers.append(producer);
384 //m_clipProducer = producer;
385 //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
386 if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
389 Mlt::Producer *DocClipBase::producer(int track) {
390 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
391 if (m_baseTrackProducers.count() == 0) return NULL;
393 for (i = 0; i < m_baseTrackProducers.count(); i++)
394 if (m_baseTrackProducers.at(i) != NULL) break;
395 if (i < m_baseTrackProducers.count()) return m_baseTrackProducers.at(i);
398 if (track >= m_baseTrackProducers.count()) {
399 while (m_baseTrackProducers.count() - 1 < track) {
400 m_baseTrackProducers.append(NULL);
403 if (m_baseTrackProducers.at(track) == NULL) {
405 for (i = 0; i < m_baseTrackProducers.count(); i++)
406 if (m_baseTrackProducers.at(i) != NULL) break;
407 if (i >= m_baseTrackProducers.count()) return NULL;
408 m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
409 if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
410 if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
411 if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
412 if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
413 char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
414 m_baseTrackProducers[track]->set("id", tmp);
417 return m_baseTrackProducers.at(track);
420 void DocClipBase::setProducerProperty(const char *name, int data) {
421 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
422 if (m_baseTrackProducers.at(i) != NULL)
423 m_baseTrackProducers[i]->set(name, data);
427 void DocClipBase::setProducerProperty(const char *name, const char *data) {
428 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
429 if (m_baseTrackProducers.at(i) != NULL)
430 m_baseTrackProducers[i]->set(name, data);
434 void DocClipBase::slotRefreshProducer() {
435 if (m_baseTrackProducers.count() == 0) return;
436 kDebug() << "//////////// REFRESH CLIP !!!!!!!!!!!!!!!!";
437 if (m_clipType == SLIDESHOW) {
438 /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
439 Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
441 delete m_clipProducer;
442 m_clipProducer = new Mlt::Producer(producer.get_producer());
443 if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
444 setProducerProperty("ttl", getProperty("ttl").toInt());
445 //m_clipProducer->set("id", getProperty("id"));
446 if (getProperty("fade") == "1") {
447 // we want a fade filter effect
448 kDebug() << "//////////// FADE WANTED";
449 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
451 Mlt::Filter *filter = clipService.filter(ct);
453 if (filter->get("mlt_service") == "luma") {
457 filter = clipService.filter(ct);
460 if (filter && filter->get("mlt_service") == "luma") {
461 filter->set("period", getProperty("ttl").toInt() - 1);
462 filter->set("luma.out", getProperty("luma_duration").toInt());
463 QString resource = getProperty("luma_file");
464 char *tmp = (char *) qstrdup(resource.toUtf8().data());
465 filter->set("luma.resource", tmp);
467 if (getProperty("softness") != QString()) {
468 int soft = getProperty("softness").toInt();
469 filter->set("luma.softness", (double) soft / 100.0);
472 // filter does not exist, create it...
473 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
474 filter->set("period", getProperty("ttl").toInt() - 1);
475 filter->set("luma.out", getProperty("luma_duration").toInt());
476 QString resource = getProperty("luma_file");
477 char *tmp = (char *) qstrdup(resource.toUtf8().data());
478 filter->set("luma.resource", tmp);
480 if (getProperty("softness") != QString()) {
481 int soft = getProperty("softness").toInt();
482 filter->set("luma.softness", (double) soft / 100.0);
484 clipService.attach(*filter);
487 kDebug() << "//////////// FADE NOT WANTED!!!";
488 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
490 Mlt::Filter *filter = clipService.filter(0);
492 if (filter->get("mlt_service") == "luma") {
493 clipService.detach(*filter);
495 filter = clipService.filter(ct);
501 void DocClipBase::setProperties(QMap <QString, QString> properties) {
502 // changing clip type is not allowed
503 properties.remove("type");
504 QMapIterator<QString, QString> i(properties);
505 bool refreshProducer = false;
507 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
508 while (i.hasNext()) {
510 setProperty(i.key(), i.value());
511 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
513 if (refreshProducer) slotRefreshProducer();
516 void DocClipBase::clearProperty(const QString &key) {
517 m_properties.remove(key);
520 void DocClipBase::setProperty(const QString &key, const QString &value) {
521 m_properties.insert(key, value);
522 if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
523 else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
524 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
525 else if (key == "colour") {
526 char *tmp = (char *) qstrdup(value.toUtf8().data());
527 setProducerProperty("colour", tmp);
529 } else if (key == "xmldata") {
530 setProducerProperty("force_reload", 1);
531 } else if (key == "force_aspect_ratio") {
532 if (value.isEmpty()) {
533 m_properties.remove("force_aspect_ratio");
534 setProducerProperty("force_aspect_ratio", 0);
535 } else setProducerProperty("force_aspect_ratio", value.toDouble());
536 } else if (key == "threads") {
537 if (value.isEmpty()) {
538 m_properties.remove("threads");
539 setProducerProperty("threads", 1);
540 } else setProducerProperty("threads", value.toInt());
541 } else if (key == "video_index") {
542 if (value.isEmpty()) {
543 m_properties.remove("video_index");
544 setProducerProperty("video_index", m_properties.value("default_video").toInt());
545 } else setProducerProperty("video_index", value.toInt());
546 } else if (key == "audio_index") {
547 if (value.isEmpty()) {
548 m_properties.remove("audio_index");
549 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
550 } else setProducerProperty("audio_index", value.toInt());
554 QMap <QString, QString> DocClipBase::properties() const {
558 bool DocClipBase::slotGetAudioThumbs() {
559 if (m_thumbProd == NULL) return false;
560 if (m_audioThumbCreated) {
561 if (m_audioTimer != NULL) m_audioTimer->stop();
564 if (m_audioTimer != NULL)
565 m_audioTimer->start(1500);
566 double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
567 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);