]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
b64517df6496cdd18477906d33089344e4c3c6eb
[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     delete m_thumbProd;
96     if (m_audioTimer) {
97         m_audioTimer->stop();
98         delete m_audioTimer;
99     }
100     qDeleteAll(m_baseTrackProducers);
101     m_baseTrackProducers.clear();
102     qDeleteAll(m_audioTrackProducers);
103     m_audioTrackProducers.clear();
104     delete m_videoOnlyProducer;
105     m_videoOnlyProducer = NULL;
106 }
107
108 void DocClipBase::setZone(QPoint zone)
109 {
110     m_properties.insert("zone_in", QString::number(zone.x()));
111     m_properties.insert("zone_out", QString::number(zone.y()));
112 }
113
114 QPoint DocClipBase::zone() const
115 {
116     QPoint zone;
117     zone.setX(m_properties.value("zone_in").toInt());
118     zone.setY(m_properties.value("zone_out", "50").toInt());
119     return zone;
120 }
121
122 void DocClipBase::slotCreateAudioTimer()
123 {
124     connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
125     connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
126     m_audioTimer = new QTimer(this);
127     connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
128 }
129
130 void DocClipBase::askForAudioThumbs()
131 {
132     if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
133 }
134
135 void DocClipBase::slotClearAudioCache()
136 {
137     if (m_thumbProd) m_thumbProd->stopAudioThumbs();
138     if (m_audioTimer != NULL) m_audioTimer->stop();
139     m_audioFrameCache.clear();
140     m_audioThumbCreated = false;
141 }
142
143 /*void DocClipBase::getClipMainThumb() {
144     if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
145 }*/
146
147 KThumb *DocClipBase::thumbProducer()
148 {
149     return m_thumbProd;
150 }
151
152 bool DocClipBase::audioThumbCreated() const
153 {
154     return m_audioThumbCreated;
155 }
156
157 const QString DocClipBase::name() const
158 {
159
160     return m_properties.value("name");
161 }
162
163 const QString &DocClipBase::getId() const
164 {
165     return m_id;
166 }
167
168 void DocClipBase::setId(const QString &newId)
169 {
170     m_id = newId;
171 }
172
173 const CLIPTYPE & DocClipBase::clipType() const
174 {
175     return m_clipType;
176 }
177
178 void DocClipBase::setClipType(CLIPTYPE type)
179 {
180     m_clipType = type;
181
182     m_properties.insert("type", QString::number((int) type));
183     if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
184         slotCreateAudioTimer();
185 }
186
187 KUrl DocClipBase::fileURL() const
188 {
189     QString res = m_properties.value("resource");
190     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
191     return KUrl();
192 }
193
194 void DocClipBase::setClipThumbFrame(const uint &ix)
195 {
196     m_properties.insert("thumbnail", QString::number((int) ix));
197 }
198
199 uint DocClipBase::getClipThumbFrame() const
200 {
201     return (uint) m_properties.value("thumbnail").toInt();
202 }
203
204 const QString DocClipBase::description() const
205 {
206     return m_properties.value("description");
207 }
208
209 bool DocClipBase::isTransparent() const
210 {
211     return (m_properties.value("transparency") == "1");
212 }
213
214 const QString DocClipBase::getProperty(const QString prop) const
215 {
216     return m_properties.value(prop);
217 }
218
219 void DocClipBase::setDuration(GenTime dur)
220 {
221     m_duration = dur;
222     m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
223 }
224
225 const GenTime &DocClipBase::duration() const
226 {
227     return m_duration;
228 }
229
230 const GenTime DocClipBase::maxDuration() const
231 {
232     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
233         const GenTime dur(15000, KdenliveSettings::project_fps());
234         return dur;
235     }
236     return m_duration;
237 }
238
239 bool DocClipBase::hasFileSize() const
240 {
241     return true;
242 }
243
244
245 // virtual
246 QDomElement DocClipBase::toXML() const
247 {
248     QDomDocument doc;
249     QDomElement clip = doc.createElement("producer");
250
251     QMapIterator<QString, QString> i(m_properties);
252     while (i.hasNext()) {
253         i.next();
254         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
255     }
256     doc.appendChild(clip);
257     //kDebug()<<"/// CLIP XML: "<<doc.toString();
258     return doc.documentElement();
259 }
260
261
262 void DocClipBase::setAudioThumbCreated(bool isDone)
263 {
264     m_audioThumbCreated = isDone;
265 }
266
267
268 void DocClipBase::setThumbnail(const QPixmap & pixmap)
269 {
270     m_thumbnail = pixmap;
271 }
272
273 const QPixmap & DocClipBase::thumbnail() const
274 {
275     return m_thumbnail;
276 }
277
278 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data)
279 {
280     //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
281     m_audioFrameCache = data;
282     m_audioThumbCreated = true;
283     emit gotAudioData();
284 }
285
286 QList < GenTime > DocClipBase::snapMarkers() const
287 {
288     QList < GenTime > markers;
289
290     for (int count = 0; count < m_snapMarkers.count(); ++count) {
291         markers.append(m_snapMarkers[count].time());
292     }
293
294     return markers;
295 }
296
297 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
298 {
299     return m_snapMarkers;
300 }
301
302 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers)
303 {
304     m_snapMarkers = markers;
305 }
306
307 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
308 {
309     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
310     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
311         if ((*it).time() >= time)
312             break;
313     }
314
315     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
316         (*it).setComment(comment);
317         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
318     } else {
319         CommentedTime t(time, comment);
320         m_snapMarkers.insert(it, t);
321     }
322
323 }
324
325 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
326 {
327     QList < CommentedTime >::Iterator it;
328     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
329         if ((*it).time() == time)
330             break;
331     }
332     if (it != m_snapMarkers.end()) {
333         (*it).setComment(comment);
334     } else {
335         kError() << "trying to edit Snap Marker that does not already exists";
336     }
337 }
338
339 QString DocClipBase::deleteSnapMarker(const GenTime & time)
340 {
341     QString result = i18n("Marker");
342     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
343
344     while (itt != m_snapMarkers.end()) {
345         if ((*itt).time() == time)
346             break;
347         ++itt;
348     }
349
350     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
351         result = (*itt).comment();
352         m_snapMarkers.erase(itt);
353     }
354     return result;
355 }
356
357
358 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
359 {
360     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
361
362     while (itt != m_snapMarkers.end()) {
363         if ((*itt).time() == time)
364             return time;
365         ++itt;
366     }
367
368     return GenTime(0.0);
369 }
370
371 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
372 {
373     int it;
374     for (it = 0; it < m_snapMarkers.count(); it++) {
375         if (m_snapMarkers[it].time() >= currTime)
376             break;
377     }
378     if (it == 0) return GenTime();
379     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
380         return m_snapMarkers[it].time();
381     else return m_snapMarkers[it-1].time();
382 }
383
384 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
385 {
386     int it;
387     for (it = 0; it < m_snapMarkers.count(); it++) {
388         if (m_snapMarkers[it].time() > currTime)
389             break;
390     }
391     if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
392     return duration();
393 }
394
395 QString DocClipBase::markerComment(GenTime t)
396 {
397     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
398
399     while (itt != m_snapMarkers.end()) {
400         if ((*itt).time() == t)
401             return (*itt).comment();
402         ++itt;
403     }
404     return QString();
405 }
406
407 void DocClipBase::deleteProducers()
408 {
409     qDeleteAll(m_baseTrackProducers);
410     m_baseTrackProducers.clear();
411     if (m_thumbProd) m_thumbProd->clearProducer();
412     qDeleteAll(m_audioTrackProducers);
413     m_audioTrackProducers.clear();
414     delete m_videoOnlyProducer;
415     m_videoOnlyProducer = NULL;
416 }
417
418 void DocClipBase::setProducer(Mlt::Producer *producer)
419 {
420     if (producer == NULL) return;
421     QString id = producer->get("id");
422     if (id.contains('_')) {
423         // this is a subtrack producer, insert it at correct place
424         id = id.section('_', 1);
425         if (id.endsWith("audio")) {
426             int pos = id.section('_', 0, 0).toInt();
427             if (pos >= m_audioTrackProducers.count()) {
428                 while (m_audioTrackProducers.count() - 1 < pos) {
429                     m_audioTrackProducers.append(NULL);
430                 }
431             }
432             if (m_audioTrackProducers.at(pos) == NULL) m_audioTrackProducers[pos] = producer;
433             return;
434         }
435         if (id.endsWith("video")) {
436             m_videoOnlyProducer = producer;
437             return;
438         }
439         int pos = id.toInt();
440         if (pos >= m_baseTrackProducers.count()) {
441             while (m_baseTrackProducers.count() - 1 < pos) {
442                 m_baseTrackProducers.append(NULL);
443             }
444         }
445         if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
446     } else {
447         if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
448         else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
449     }
450     //m_clipProducer = producer;
451     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
452     if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
453 }
454
455 Mlt::Producer *DocClipBase::audioProducer(int track)
456 {
457     if (m_audioTrackProducers.count() <= track) {
458         while (m_audioTrackProducers.count() - 1 < track) {
459             m_audioTrackProducers.append(NULL);
460         }
461     }
462     if (m_audioTrackProducers.at(track) == NULL) {
463         Mlt::Producer *base = producer();
464         m_audioTrackProducers[track] = new Mlt::Producer(*(base->profile()), base->get("resource"));
465         if (m_properties.contains("force_aspect_ratio")) m_audioTrackProducers.at(track)->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
466         if (m_properties.contains("threads")) m_audioTrackProducers.at(track)->set("threads", m_properties.value("threads").toInt());
467         m_audioTrackProducers.at(track)->set("video_index", -1);
468         if (m_properties.contains("audio_index")) m_audioTrackProducers.at(track)->set("audio_index", m_properties.value("audio_index").toInt());
469         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track) + "_audio").toUtf8().data());
470         m_audioTrackProducers.at(track)->set("id", tmp);
471         delete[] tmp;
472     }
473     return m_audioTrackProducers.at(track);
474 }
475
476 Mlt::Producer *DocClipBase::videoProducer()
477 {
478     if (m_videoOnlyProducer == NULL) {
479         int i;
480         for (i = 0; i < m_baseTrackProducers.count(); i++)
481             if (m_baseTrackProducers.at(i) != NULL) break;
482         if (i >= m_baseTrackProducers.count()) return NULL;
483         m_videoOnlyProducer = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
484         if (m_properties.contains("force_aspect_ratio")) m_videoOnlyProducer->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
485         if (m_properties.contains("threads")) m_videoOnlyProducer->set("threads", m_properties.value("threads").toInt());
486         m_videoOnlyProducer->set("audio_index", -1);
487         if (m_properties.contains("video_index")) m_videoOnlyProducer->set("video_index", m_properties.value("video_index").toInt());
488         char *tmp = (char *) qstrdup(QString(getId() + "_video").toUtf8().data());
489         m_videoOnlyProducer->set("id", tmp);
490         delete[] tmp;
491     }
492     return m_videoOnlyProducer;
493 }
494
495 Mlt::Producer *DocClipBase::producer(int track)
496 {
497     /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
498         if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
499     }*/
500     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
501         if (m_baseTrackProducers.count() == 0) return NULL;
502         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
503             if (m_baseTrackProducers.at(i) != NULL)
504                 return m_baseTrackProducers.at(i);
505         }
506         return NULL;
507     }
508     if (track >= m_baseTrackProducers.count()) {
509         while (m_baseTrackProducers.count() - 1 < track) {
510             m_baseTrackProducers.append(NULL);
511         }
512     }
513     if (m_baseTrackProducers.at(track) == NULL) {
514         int i;
515         for (i = 0; i < m_baseTrackProducers.count(); i++)
516             if (m_baseTrackProducers.at(i) != NULL) break;
517
518         if (i >= m_baseTrackProducers.count()) return NULL;
519
520         if (KIO::NetAccess::exists(KUrl(m_baseTrackProducers.at(i)->get("resource")), KIO::NetAccess::SourceSide, 0))
521             m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
522         else { // special case for placeholder clips
523             m_baseTrackProducers[track] = NULL;
524             return NULL;
525         }
526
527         if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
528         if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
529         if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
530         if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
531         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
532         m_baseTrackProducers[track]->set("id", tmp);
533         delete[] tmp;
534         if (KdenliveSettings::dropbframes() && m_baseTrackProducers.at(i)->get("skip_loop_filter") && strcmp(m_baseTrackProducers.at(i)->get("skip_loop_filter"), "all") == 0) {
535             m_baseTrackProducers[track]->set("skip_loop_filter", "all");
536             m_baseTrackProducers[track]->set("skip_frame", "bidir");
537         }
538     }
539     return m_baseTrackProducers.at(track);
540 }
541
542 void DocClipBase::setProducerProperty(const char *name, int data)
543 {
544     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
545         if (m_baseTrackProducers.at(i) != NULL)
546             m_baseTrackProducers[i]->set(name, data);
547     }
548 }
549
550 void DocClipBase::setProducerProperty(const char *name, const char *data)
551 {
552     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
553         if (m_baseTrackProducers.at(i) != NULL)
554             m_baseTrackProducers[i]->set(name, data);
555     }
556 }
557
558 const char *DocClipBase::producerProperty(const char *name) const
559 {
560     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
561         if (m_baseTrackProducers.at(i) != NULL) {
562             return m_baseTrackProducers.at(i)->get(name);
563         }
564     }
565     return NULL;
566 }
567
568
569 void DocClipBase::slotRefreshProducer()
570 {
571     if (m_baseTrackProducers.count() == 0) return;
572     kDebug() << "////////////   REFRESH CLIP !!!!!!!!!!!!!!!!";
573     if (m_clipType == SLIDESHOW) {
574         /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
575                Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
576                delete[] tmp;
577         delete m_clipProducer;
578         m_clipProducer = new Mlt::Producer(producer.get_producer());
579         if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
580         setProducerProperty("ttl", getProperty("ttl").toInt());
581         //m_clipProducer->set("id", getProperty("id"));
582         if (getProperty("fade") == "1") {
583             // we want a fade filter effect
584             kDebug() << "////////////   FADE WANTED";
585             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
586             int ct = 0;
587             Mlt::Filter *filter = clipService.filter(ct);
588             while (filter) {
589                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
590                     break;
591                 }
592                 ct++;
593                 filter = clipService.filter(ct);
594             }
595
596             if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
597                 filter->set("period", getProperty("ttl").toInt() - 1);
598                 filter->set("luma.out", getProperty("luma_duration").toInt());
599                 QString resource = getProperty("luma_file");
600                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
601                 filter->set("luma.resource", tmp);
602                 delete[] tmp;
603                 if (!getProperty("softness").isEmpty()) {
604                     int soft = getProperty("softness").toInt();
605                     filter->set("luma.softness", (double) soft / 100.0);
606                 }
607             } else {
608                 // filter does not exist, create it...
609                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
610                 filter->set("period", getProperty("ttl").toInt() - 1);
611                 filter->set("luma.out", getProperty("luma_duration").toInt());
612                 QString resource = getProperty("luma_file");
613                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
614                 filter->set("luma.resource", tmp);
615                 delete[] tmp;
616                 if (!getProperty("softness").isEmpty()) {
617                     int soft = getProperty("softness").toInt();
618                     filter->set("luma.softness", (double) soft / 100.0);
619                 }
620                 clipService.attach(*filter);
621             }
622         } else {
623             kDebug() << "////////////   FADE NOT WANTED!!!";
624             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
625             int ct = 0;
626             Mlt::Filter *filter = clipService.filter(0);
627             while (filter) {
628                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
629                     clipService.detach(*filter);
630                 } else ct++;
631                 filter = clipService.filter(ct);
632             }
633         }
634     }
635 }
636
637 void DocClipBase::setProperties(QMap <QString, QString> properties)
638 {
639     // changing clip type is not allowed
640     properties.remove("type");
641     QMapIterator<QString, QString> i(properties);
642     bool refreshProducer = false;
643     QStringList keys;
644     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
645     while (i.hasNext()) {
646         i.next();
647         setProperty(i.key(), i.value());
648         if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
649     }
650     if (refreshProducer) slotRefreshProducer();
651 }
652
653 void DocClipBase::setMetadata(QMap <QString, QString> properties)
654 {
655     m_metadata = properties;
656 }
657
658 QMap <QString, QString> DocClipBase::metadata() const
659 {
660     return m_metadata;
661 }
662
663 void DocClipBase::clearProperty(const QString &key)
664 {
665     m_properties.remove(key);
666 }
667
668 void DocClipBase::getFileHash(const QString url)
669 {
670     if (m_clipType == SLIDESHOW) return;
671     QFile file(url);
672     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
673         QByteArray fileData;
674         QByteArray fileHash;
675         //kDebug() << "SETTING HASH of" << value;
676         m_properties.insert("file_size", QString::number(file.size()));
677         /*
678                * 1 MB = 1 second per 450 files (or faster)
679                * 10 MB = 9 seconds per 450 files (or faster)
680                */
681         if (file.size() > 1000000*2) {
682             fileData = file.read(1000000);
683             if (file.seek(file.size() - 1000000))
684                 fileData.append(file.readAll());
685         } else
686             fileData = file.readAll();
687         file.close();
688         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
689         m_properties.insert("file_hash", QString(fileHash.toHex()));
690         //kDebug() << file.fileName() << file.size() << fileHash.toHex();
691     }
692 }
693
694 QString DocClipBase::getClipHash() const
695 {
696     QString hash;
697     if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
698     else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
699     else hash = m_properties.value("file_hash");
700     return hash;
701 }
702
703 void DocClipBase::refreshThumbUrl()
704 {
705     if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
706 }
707
708 void DocClipBase::setProperty(const QString &key, const QString &value)
709 {
710     m_properties.insert(key, value);
711     if (key == "resource") {
712         getFileHash(value);
713         if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
714     } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
715     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
716     else if (key == "colour") {
717         char *tmp = (char *) qstrdup(value.toUtf8().data());
718         setProducerProperty("colour", tmp);
719         delete[] tmp;
720     } else if (key == "xmldata") {
721         setProducerProperty("force_reload", 1);
722     } else if (key == "force_aspect_ratio") {
723         if (value.isEmpty()) {
724             m_properties.remove("force_aspect_ratio");
725             setProducerProperty("force_aspect_ratio", 0);
726         } else setProducerProperty("force_aspect_ratio", value.toDouble());
727     } else if (key == "threads") {
728         if (value.isEmpty()) {
729             m_properties.remove("threads");
730             setProducerProperty("threads", 1);
731         } else setProducerProperty("threads", value.toInt());
732     } else if (key == "video_index") {
733         if (value.isEmpty()) {
734             m_properties.remove("video_index");
735             setProducerProperty("video_index", m_properties.value("default_video").toInt());
736         } else setProducerProperty("video_index", value.toInt());
737     } else if (key == "audio_index") {
738         if (value.isEmpty()) {
739             m_properties.remove("audio_index");
740             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
741         } else setProducerProperty("audio_index", value.toInt());
742     }
743 }
744
745 QMap <QString, QString> DocClipBase::properties() const
746 {
747     return m_properties;
748 }
749
750 bool DocClipBase::slotGetAudioThumbs()
751 {
752     if (m_thumbProd == NULL) return false;
753     if (!KdenliveSettings::audiothumbnails()) {
754         if (m_audioTimer != NULL) m_audioTimer->stop();
755         return false;
756     }
757     if (m_audioThumbCreated) {
758         if (m_audioTimer != NULL) m_audioTimer->stop();
759         return false;
760     }
761     if (m_audioTimer != NULL)
762         m_audioTimer->start(1500);
763     double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
764     m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
765     return true;
766 }
767
768 bool DocClipBase::isPlaceHolder() const
769 {
770     return m_placeHolder;
771 }
772