]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
3221ef8ee9ed68795318f8348f70325980016c35
[kdenlive] / src / docclipbase.cpp
1 /***************************************************************************
2  *                         DocClipBase.cpp  -  description                 *
3  *                           -------------------                           *
4  *   begin                : Fri Apr 12 2002                                *
5  *   Copyright (C) 2002 by Jason Wood (jasonwood@blueyonder.co.uk)         *
6  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
22  ***************************************************************************/
23
24
25
26
27 #include "docclipbase.h"
28 #include "kdenlivesettings.h"
29 #include "kthumb.h"
30 #include "clipmanager.h"
31
32 #include <KIO/NetAccess>
33 #include <KDebug>
34
35 #include <QCryptographicHash>
36
37 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id) :
38         QObject(),
39         m_audioFrameCache(),
40         m_refcount(0),
41         m_baseTrackProducers(),
42         m_audioTrackProducers(),
43         m_videoOnlyProducer(NULL),
44         m_snapMarkers(),
45         m_duration(),
46         m_audioTimer(NULL),
47         m_thumbProd(NULL),
48         m_audioThumbCreated(false),
49         m_id(id),
50         m_placeHolder(xml.hasAttribute("placeholder")),
51         m_properties()
52 {
53     int type = xml.attribute("type").toInt();
54     m_clipType = (CLIPTYPE) type;
55     if (m_placeHolder) xml.removeAttribute("placeholder");
56     QDomNamedNodeMap attributes = xml.attributes();
57     for (int i = 0; i < attributes.count(); i++) {
58         m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
59     }
60
61     KUrl url = KUrl(xml.attribute("resource"));
62     if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
63
64     if (xml.hasAttribute("duration")) {
65         setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
66     } else {
67         int out = xml.attribute("out").toInt();
68         int in = xml.attribute("in").toInt();
69         setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
70     }
71
72     if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
73
74     //if (!url.isEmpty() && QFile::exists(url.path()))
75     {
76         m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
77         if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
78     }
79     //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
80 }
81
82 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
83     DocClipBase::operator=(clip);
84     m_id = clip.getId();
85     m_clipType = clip.clipType();
86     m_name = clip.name();
87     m_duration = clip.duration();
88     m_audioThumbCreated = clip.audioThumbCreated();
89     m_properties = clip.properties();
90     return *this;
91 }*/
92
93 DocClipBase::~DocClipBase()
94 {
95     kDebug() << "CLIP " << m_id << " DELETED******************************";
96     delete m_thumbProd;
97     if (m_audioTimer) {
98         m_audioTimer->stop();
99         delete m_audioTimer;
100     }
101     /*kDebug() <<" * * *CNT "<<m_baseTrackProducers.count();
102     if (m_baseTrackProducers.count() > 0) kDebug()<<"YOYO: "<<m_baseTrackProducers.at(0)->get_out()<<", CUT: "<<m_baseTrackProducers.at(0)->is_cut();*/
103     qDeleteAll(m_baseTrackProducers);
104     m_baseTrackProducers.clear();
105     qDeleteAll(m_audioTrackProducers);
106     m_audioTrackProducers.clear();
107     delete m_videoOnlyProducer;
108     m_videoOnlyProducer = NULL;
109 }
110
111 void DocClipBase::setZone(QPoint zone)
112 {
113     m_properties.insert("zone_in", QString::number(zone.x()));
114     m_properties.insert("zone_out", QString::number(zone.y()));
115 }
116
117 QPoint DocClipBase::zone() const
118 {
119     QPoint zone;
120     zone.setX(m_properties.value("zone_in").toInt());
121     zone.setY(m_properties.value("zone_out", "50").toInt());
122     return zone;
123 }
124
125 void DocClipBase::slotCreateAudioTimer()
126 {
127     connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
128     connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
129     m_audioTimer = new QTimer(this);
130     connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
131 }
132
133 void DocClipBase::askForAudioThumbs()
134 {
135     if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
136 }
137
138 void DocClipBase::slotClearAudioCache()
139 {
140     if (m_thumbProd) m_thumbProd->stopAudioThumbs();
141     if (m_audioTimer != NULL) m_audioTimer->stop();
142     m_audioFrameCache.clear();
143     m_audioThumbCreated = false;
144 }
145
146 /*void DocClipBase::getClipMainThumb() {
147     if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
148 }*/
149
150 KThumb *DocClipBase::thumbProducer()
151 {
152     return m_thumbProd;
153 }
154
155 bool DocClipBase::audioThumbCreated() const
156 {
157     return m_audioThumbCreated;
158 }
159
160 const QString DocClipBase::name() const
161 {
162
163     return m_properties.value("name");
164 }
165
166 const QString &DocClipBase::getId() const
167 {
168     return m_id;
169 }
170
171 void DocClipBase::setId(const QString &newId)
172 {
173     m_id = newId;
174 }
175
176 const CLIPTYPE & DocClipBase::clipType() const
177 {
178     return m_clipType;
179 }
180
181 void DocClipBase::setClipType(CLIPTYPE type)
182 {
183     m_clipType = type;
184
185     m_properties.insert("type", QString::number((int) type));
186     if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
187         slotCreateAudioTimer();
188 }
189
190 KUrl DocClipBase::fileURL() const
191 {
192     QString res = m_properties.value("resource");
193     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
194     return KUrl();
195 }
196
197 void DocClipBase::setClipThumbFrame(const uint &ix)
198 {
199     m_properties.insert("thumbnail", QString::number((int) ix));
200 }
201
202 uint DocClipBase::getClipThumbFrame() const
203 {
204     return (uint) m_properties.value("thumbnail").toInt();
205 }
206
207 const QString DocClipBase::description() const
208 {
209     return m_properties.value("description");
210 }
211
212 bool DocClipBase::isTransparent() const
213 {
214     return (m_properties.value("transparency") == "1");
215 }
216
217 const QString DocClipBase::getProperty(const QString prop) const
218 {
219     return m_properties.value(prop);
220 }
221
222 void DocClipBase::setDuration(GenTime dur)
223 {
224     m_duration = dur;
225     m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
226 }
227
228 const GenTime &DocClipBase::duration() const
229 {
230     return m_duration;
231 }
232
233 const GenTime DocClipBase::maxDuration() const
234 {
235     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
236         /*const GenTime dur(15000, KdenliveSettings::project_fps());
237         return dur;*/
238         return GenTime();
239     }
240     return m_duration;
241 }
242
243 bool DocClipBase::hasFileSize() const
244 {
245     return true;
246 }
247
248 qulonglong DocClipBase::fileSize() const
249 {
250     return m_properties.value("file_size").toULongLong();
251 }
252
253 // virtual
254 QDomElement DocClipBase::toXML() const
255 {
256     QDomDocument doc;
257     QDomElement clip = doc.createElement("producer");
258
259     QMapIterator<QString, QString> i(m_properties);
260     while (i.hasNext()) {
261         i.next();
262         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
263     }
264     doc.appendChild(clip);
265     //kDebug() << "/// CLIP XML: " << doc.toString();
266     return doc.documentElement();
267 }
268
269
270 void DocClipBase::setAudioThumbCreated(bool isDone)
271 {
272     m_audioThumbCreated = isDone;
273 }
274
275
276 void DocClipBase::setThumbnail(const QPixmap & pixmap)
277 {
278     m_thumbnail = pixmap;
279 }
280
281 const QPixmap & DocClipBase::thumbnail() const
282 {
283     return m_thumbnail;
284 }
285
286 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data)
287 {
288     //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
289     m_audioFrameCache = data;
290     m_audioThumbCreated = true;
291     emit gotAudioData();
292 }
293
294 QList < GenTime > DocClipBase::snapMarkers() const
295 {
296     QList < GenTime > markers;
297
298     for (int count = 0; count < m_snapMarkers.count(); ++count) {
299         markers.append(m_snapMarkers.at(count).time());
300     }
301
302     return markers;
303 }
304
305 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
306 {
307     return m_snapMarkers;
308 }
309
310 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers)
311 {
312     m_snapMarkers = markers;
313 }
314
315 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
316 {
317     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
318     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
319         if ((*it).time() >= time)
320             break;
321     }
322
323     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
324         (*it).setComment(comment);
325         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
326     } else {
327         CommentedTime t(time, comment);
328         m_snapMarkers.insert(it, t);
329     }
330
331 }
332
333 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
334 {
335     QList < CommentedTime >::Iterator it;
336     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
337         if ((*it).time() == time)
338             break;
339     }
340     if (it != m_snapMarkers.end()) {
341         (*it).setComment(comment);
342     } else {
343         kError() << "trying to edit Snap Marker that does not already exists";
344     }
345 }
346
347 QString DocClipBase::deleteSnapMarker(const GenTime & time)
348 {
349     QString result = i18n("Marker");
350     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
351
352     while (itt != m_snapMarkers.end()) {
353         if ((*itt).time() == time)
354             break;
355         ++itt;
356     }
357
358     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
359         result = (*itt).comment();
360         m_snapMarkers.erase(itt);
361     }
362     return result;
363 }
364
365
366 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
367 {
368     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
369
370     while (itt != m_snapMarkers.end()) {
371         if ((*itt).time() == time)
372             return time;
373         ++itt;
374     }
375
376     return GenTime(0.0);
377 }
378
379 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
380 {
381     int it;
382     for (it = 0; it < m_snapMarkers.count(); it++) {
383         if (m_snapMarkers.at(it).time() >= currTime)
384             break;
385     }
386     if (it == 0) return GenTime();
387     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
388         return m_snapMarkers.at(it).time();
389     else return m_snapMarkers.at(it -1).time();
390 }
391
392 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
393 {
394     int it;
395     for (it = 0; it < m_snapMarkers.count(); it++) {
396         if (m_snapMarkers.at(it).time() > currTime)
397             break;
398     }
399     if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
400     return duration();
401 }
402
403 QString DocClipBase::markerComment(GenTime t)
404 {
405     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
406
407     while (itt != m_snapMarkers.end()) {
408         if ((*itt).time() == t)
409             return (*itt).comment();
410         ++itt;
411     }
412     return QString();
413 }
414
415 void DocClipBase::clearProducers()
416 {
417     m_baseTrackProducers.clear();
418 }
419
420 void DocClipBase::deleteProducers()
421 {
422     kDebug() << "// CLIP KILL PRODS ct: " << m_baseTrackProducers.count();
423     if (m_thumbProd) m_thumbProd->clearProducer();
424     /*kDebug()<<"// CLIP KILL PRODS ct: "<<m_baseTrackProducers.count();
425     int max = m_baseTrackProducers.count();
426     for (int i = 0; i < max; i++) {
427         kDebug()<<"// CLIP KILL PROD "<<i;
428     Mlt::Producer *p = m_baseTrackProducers.takeAt(i);
429     if (p != NULL) {
430      delete p;
431      p = NULL;
432     }
433     m_baseTrackProducers.insert(i, NULL);
434     }*/
435
436     delete m_videoOnlyProducer;
437     m_videoOnlyProducer = NULL;
438
439     qDeleteAll(m_baseTrackProducers);
440     m_baseTrackProducers.clear();
441     qDeleteAll(m_audioTrackProducers);
442     m_audioTrackProducers.clear();
443 }
444
445 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset)
446 {
447     if (producer == NULL) return;
448     if (reset) {
449         // Clear all previous producers
450         kDebug() << "/+++++++++++++++   DELETE ALL PRODS " << producer->get("id");
451         deleteProducers();
452     }
453     QString id = producer->get("id");
454     kDebug() << "// set prod: " << id;
455     if (id.contains('_')) {
456         // this is a subtrack producer, insert it at correct place
457         id = id.section('_', 1);
458         if (id.endsWith("audio")) {
459             int pos = id.section('_', 0, 0).toInt();
460             if (pos >= m_audioTrackProducers.count()) {
461                 while (m_audioTrackProducers.count() - 1 < pos) {
462                     m_audioTrackProducers.append(NULL);
463                 }
464             }
465             if (m_audioTrackProducers.at(pos) == NULL) m_audioTrackProducers[pos] = producer;
466             return;
467         } else if (id.endsWith("video")) {
468             m_videoOnlyProducer = producer;
469             return;
470         }
471         int pos = id.toInt();
472         if (pos >= m_baseTrackProducers.count()) {
473             while (m_baseTrackProducers.count() - 1 < pos) {
474                 m_baseTrackProducers.append(NULL);
475             }
476         }
477         if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
478     } else {
479         if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
480         else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
481     }
482     //m_clipProducer = producer;
483     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
484     if (m_thumbProd && (reset || !m_thumbProd->hasProducer())) m_thumbProd->setProducer(producer);
485 }
486
487 Mlt::Producer *DocClipBase::audioProducer(int track)
488 {
489     if (m_audioTrackProducers.count() <= track) {
490         while (m_audioTrackProducers.count() - 1 < track) {
491             m_audioTrackProducers.append(NULL);
492         }
493     }
494     if (m_audioTrackProducers.at(track) == NULL) {
495         Mlt::Producer *base = producer();
496         m_audioTrackProducers[track] = new Mlt::Producer(*(base->profile()), base->get("resource"));
497         if (m_properties.contains("force_aspect_ratio")) m_audioTrackProducers.at(track)->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
498         if (m_properties.contains("threads")) m_audioTrackProducers.at(track)->set("threads", m_properties.value("threads").toInt());
499         m_audioTrackProducers.at(track)->set("video_index", -1);
500         if (m_properties.contains("audio_index")) m_audioTrackProducers.at(track)->set("audio_index", m_properties.value("audio_index").toInt());
501         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track) + "_audio").toUtf8().data());
502         m_audioTrackProducers.at(track)->set("id", tmp);
503         delete[] tmp;
504     }
505     return m_audioTrackProducers.at(track);
506 }
507
508 Mlt::Producer *DocClipBase::videoProducer()
509 {
510     if (m_videoOnlyProducer == NULL) {
511         int i;
512         for (i = 0; i < m_baseTrackProducers.count(); i++)
513             if (m_baseTrackProducers.at(i) != NULL) break;
514         if (i >= m_baseTrackProducers.count()) return NULL;
515         m_videoOnlyProducer = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
516         if (m_properties.contains("force_aspect_ratio")) m_videoOnlyProducer->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
517         if (m_properties.contains("threads")) m_videoOnlyProducer->set("threads", m_properties.value("threads").toInt());
518         m_videoOnlyProducer->set("audio_index", -1);
519         if (m_properties.contains("video_index")) m_videoOnlyProducer->set("video_index", m_properties.value("video_index").toInt());
520         char *tmp = (char *) qstrdup(QString(getId() + "_video").toUtf8().data());
521         m_videoOnlyProducer->set("id", tmp);
522         delete[] tmp;
523     }
524     return m_videoOnlyProducer;
525 }
526
527 Mlt::Producer *DocClipBase::producer(int track)
528 {
529     /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
530         if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
531     }*/
532     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
533         if (m_baseTrackProducers.count() == 0) return NULL;
534         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
535             if (m_baseTrackProducers.at(i) != NULL)
536                 return m_baseTrackProducers.at(i);
537         }
538         return NULL;
539     }
540     if (track >= m_baseTrackProducers.count()) {
541         while (m_baseTrackProducers.count() - 1 < track) {
542             m_baseTrackProducers.append(NULL);
543         }
544     }
545     if (m_baseTrackProducers.at(track) == NULL) {
546         int i;
547         for (i = 0; i < m_baseTrackProducers.count(); i++)
548             if (m_baseTrackProducers.at(i) != NULL) break;
549
550         if (i >= m_baseTrackProducers.count()) return NULL;
551
552         if (KIO::NetAccess::exists(KUrl(m_baseTrackProducers.at(i)->get("resource")), KIO::NetAccess::SourceSide, 0))
553             m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
554         else { // special case for placeholder clips
555             m_baseTrackProducers[track] = NULL;
556             return NULL;
557         }
558
559         if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
560         if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
561         if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
562         if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
563         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
564         m_baseTrackProducers[track]->set("id", tmp);
565         delete[] tmp;
566         if (KdenliveSettings::dropbframes() && m_baseTrackProducers.at(i)->get("skip_loop_filter") && strcmp(m_baseTrackProducers.at(i)->get("skip_loop_filter"), "all") == 0) {
567             m_baseTrackProducers[track]->set("skip_loop_filter", "all");
568             m_baseTrackProducers[track]->set("skip_frame", "bidir");
569         }
570     }
571     return m_baseTrackProducers.at(track);
572 }
573
574 void DocClipBase::setProducerProperty(const char *name, int data)
575 {
576     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
577         if (m_baseTrackProducers.at(i) != NULL)
578             m_baseTrackProducers[i]->set(name, data);
579     }
580 }
581
582 void DocClipBase::setProducerProperty(const char *name, double data)
583 {
584     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
585         if (m_baseTrackProducers.at(i) != NULL)
586             m_baseTrackProducers[i]->set(name, data);
587     }
588 }
589
590 void DocClipBase::setProducerProperty(const char *name, const char *data)
591 {
592     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
593         if (m_baseTrackProducers.at(i) != NULL)
594             m_baseTrackProducers[i]->set(name, data);
595     }
596 }
597
598 const char *DocClipBase::producerProperty(const char *name) const
599 {
600     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
601         if (m_baseTrackProducers.at(i) != NULL) {
602             return m_baseTrackProducers.at(i)->get(name);
603         }
604     }
605     return NULL;
606 }
607
608
609 void DocClipBase::slotRefreshProducer()
610 {
611     if (m_baseTrackProducers.count() == 0) return;
612     kDebug() << "////////////   REFRESH CLIP !!!!!!!!!!!!!!!!";
613     if (m_clipType == SLIDESHOW) {
614         /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
615                Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
616                delete[] tmp;
617         delete m_clipProducer;
618         m_clipProducer = new Mlt::Producer(producer.get_producer());
619         if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
620         setProducerProperty("ttl", getProperty("ttl").toInt());
621         //m_clipProducer->set("id", getProperty("id"));
622         if (getProperty("fade") == "1") {
623             // we want a fade filter effect
624             kDebug() << "////////////   FADE WANTED";
625             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
626             int ct = 0;
627             Mlt::Filter *filter = clipService.filter(ct);
628             while (filter) {
629                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
630                     break;
631                 }
632                 ct++;
633                 filter = clipService.filter(ct);
634             }
635
636             if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
637                 filter->set("period", getProperty("ttl").toInt() - 1);
638                 filter->set("luma.out", getProperty("luma_duration").toInt());
639                 QString resource = getProperty("luma_file");
640                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
641                 filter->set("luma.resource", tmp);
642                 delete[] tmp;
643                 if (!getProperty("softness").isEmpty()) {
644                     int soft = getProperty("softness").toInt();
645                     filter->set("luma.softness", (double) soft / 100.0);
646                 }
647             } else {
648                 // filter does not exist, create it...
649                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
650                 filter->set("period", getProperty("ttl").toInt() - 1);
651                 filter->set("luma.out", getProperty("luma_duration").toInt());
652                 QString resource = getProperty("luma_file");
653                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
654                 filter->set("luma.resource", tmp);
655                 delete[] tmp;
656                 if (!getProperty("softness").isEmpty()) {
657                     int soft = getProperty("softness").toInt();
658                     filter->set("luma.softness", (double) soft / 100.0);
659                 }
660                 clipService.attach(*filter);
661             }
662         } else {
663             kDebug() << "////////////   FADE NOT WANTED!!!";
664             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
665             int ct = 0;
666             Mlt::Filter *filter = clipService.filter(0);
667             while (filter) {
668                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
669                     clipService.detach(*filter);
670                 } else ct++;
671                 filter = clipService.filter(ct);
672             }
673         }
674     }
675 }
676
677 void DocClipBase::setProperties(QMap <QString, QString> properties)
678 {
679     // changing clip type is not allowed
680     properties.remove("type");
681     QMapIterator<QString, QString> i(properties);
682     bool refreshProducer = false;
683     QStringList keys;
684     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
685     while (i.hasNext()) {
686         i.next();
687         setProperty(i.key(), i.value());
688         if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
689     }
690     if (refreshProducer) slotRefreshProducer();
691 }
692
693 void DocClipBase::setMetadata(QMap <QString, QString> properties)
694 {
695     m_metadata = properties;
696 }
697
698 QMap <QString, QString> DocClipBase::metadata() const
699 {
700     return m_metadata;
701 }
702
703 void DocClipBase::clearProperty(const QString &key)
704 {
705     m_properties.remove(key);
706 }
707
708 void DocClipBase::getFileHash(const QString url)
709 {
710     if (m_clipType == SLIDESHOW) return;
711     QFile file(url);
712     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
713         QByteArray fileData;
714         QByteArray fileHash;
715         //kDebug() << "SETTING HASH of" << value;
716         m_properties.insert("file_size", QString::number(file.size()));
717         /*
718                * 1 MB = 1 second per 450 files (or faster)
719                * 10 MB = 9 seconds per 450 files (or faster)
720                */
721         if (file.size() > 1000000*2) {
722             fileData = file.read(1000000);
723             if (file.seek(file.size() - 1000000))
724                 fileData.append(file.readAll());
725         } else
726             fileData = file.readAll();
727         file.close();
728         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
729         m_properties.insert("file_hash", QString(fileHash.toHex()));
730     }
731 }
732
733 QString DocClipBase::getClipHash() const
734 {
735     QString hash;
736     if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
737     else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
738     else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
739     else hash = m_properties.value("file_hash");
740     return hash;
741 }
742
743 // static
744 QString DocClipBase::getHash(const QString &path)
745 {
746     QFile file(path);
747     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
748         QByteArray fileData;
749         QByteArray fileHash;
750         /*
751                * 1 MB = 1 second per 450 files (or faster)
752                * 10 MB = 9 seconds per 450 files (or faster)
753                */
754         if (file.size() > 1000000*2) {
755             fileData = file.read(1000000);
756             if (file.seek(file.size() - 1000000))
757                 fileData.append(file.readAll());
758         } else
759             fileData = file.readAll();
760         file.close();
761         return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
762     }
763     return QString();
764 }
765
766 void DocClipBase::refreshThumbUrl()
767 {
768     if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
769 }
770
771 void DocClipBase::setProperty(const QString &key, const QString &value)
772 {
773     m_properties.insert(key, value);
774     if (key == "resource") {
775         getFileHash(value);
776         if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
777     } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
778     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
779     else if (key == "colour") {
780         char *tmp = (char *) qstrdup(value.toUtf8().data());
781         setProducerProperty("colour", tmp);
782         delete[] tmp;
783     } else if (key == "templatetext") {
784         char *tmp = (char *) qstrdup(value.toUtf8().data());
785         setProducerProperty("templatetext", tmp);
786         delete[] tmp;
787         setProducerProperty("force_reload", 1);
788     } else if (key == "xmldata") {
789         char *tmp = (char *) qstrdup(value.toUtf8().data());
790         setProducerProperty("xmldata", tmp);
791         delete[] tmp;
792         setProducerProperty("force_reload", 1);
793     } else if (key == "force_aspect_ratio") {
794         if (value.isEmpty()) {
795             m_properties.remove("force_aspect_ratio");
796             //TODO: find a was to remove the "force_aspect_ratio" property from producer, currently does not work
797             setProducerProperty("force_aspect_ratio", 0);
798         } else setProducerProperty("force_aspect_ratio", value.toDouble());
799     } else if (key == "threads") {
800         if (value.isEmpty()) {
801             m_properties.remove("threads");
802             setProducerProperty("threads", 1);
803         } else setProducerProperty("threads", value.toInt());
804     } else if (key == "video_index") {
805         if (value.isEmpty()) {
806             m_properties.remove("video_index");
807             setProducerProperty("video_index", m_properties.value("default_video").toInt());
808         } else setProducerProperty("video_index", value.toInt());
809     } else if (key == "audio_index") {
810         if (value.isEmpty()) {
811             m_properties.remove("audio_index");
812             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
813         } else setProducerProperty("audio_index", value.toInt());
814     }
815 }
816
817 QMap <QString, QString> DocClipBase::properties() const
818 {
819     return m_properties;
820 }
821
822 bool DocClipBase::slotGetAudioThumbs()
823 {
824     if (m_thumbProd == NULL) return false;
825     if (!KdenliveSettings::audiothumbnails()) {
826         if (m_audioTimer != NULL) m_audioTimer->stop();
827         return false;
828     }
829     if (m_audioThumbCreated) {
830         if (m_audioTimer != NULL) m_audioTimer->stop();
831         return false;
832     }
833     if (m_audioTimer != NULL)
834         m_audioTimer->start(1500);
835     double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
836     m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
837     return true;
838 }
839
840 bool DocClipBase::isPlaceHolder() const
841 {
842     return m_placeHolder;
843 }
844