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