]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
Fix crash in thumbnails when deleting a clip
[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 #include "slideshowclip.h"
32
33 #include <KIO/NetAccess>
34 #include <KStandardDirs>
35 #include <KDebug>
36
37 #include <QCryptographicHash>
38 #include <QtConcurrentRun>
39
40 #include <cstdio>
41
42 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id) :
43         QObject(),
44         m_audioFrameCache(),
45         m_refcount(0),
46         m_baseTrackProducers(),
47         m_audioTrackProducers(),
48         m_videoOnlyProducer(NULL),
49         m_snapMarkers(QList < CommentedTime >()),
50         m_duration(),
51         m_thumbProd(NULL),
52         m_audioThumbCreated(false),
53         m_id(id),
54         m_placeHolder(xml.hasAttribute("placeholder")),
55         m_properties()
56 {
57     int type = xml.attribute("type").toInt();
58     m_clipType = (CLIPTYPE) type;
59     if (m_placeHolder) xml.removeAttribute("placeholder");
60     QDomNamedNodeMap attributes = xml.attributes();
61     for (int i = 0; i < attributes.count(); i++) {
62         QString name = attributes.item(i).nodeName();
63         if (name.startsWith("meta.attr.")) {
64             m_metadata.insert(name.section('.', 2, 3), attributes.item(i).nodeValue());
65         } else m_properties.insert(name, attributes.item(i).nodeValue());
66     }
67
68     if (xml.hasAttribute("cutzones")) {
69         QStringList cuts = xml.attribute("cutzones").split(";", QString::SkipEmptyParts);
70         for (int i = 0; i < cuts.count(); i++) {
71             QString z = cuts.at(i);
72             addCutZone(z.section('-', 0, 0).toInt(), z.section('-', 1, 1).toInt(), z.section('-', 2, 2));
73         }
74     }
75
76     KUrl url = KUrl(xml.attribute("resource"));
77     if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
78
79     if (xml.hasAttribute("duration")) {
80         setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
81     } else {
82         int out = xml.attribute("out").toInt();
83         int in = xml.attribute("in").toInt();
84         setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
85     }
86
87     if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
88
89     m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
90     
91     // Setup timer to trigger audio thumbs creation
92     m_audioTimer.setSingleShot(true);
93     m_audioTimer.setInterval(800);
94     connect(&m_audioTimer, SIGNAL(timeout()), m_thumbProd, SLOT(slotCreateAudioThumbs()));
95     
96 }
97
98 DocClipBase::~DocClipBase()
99 {
100     m_audioTimer.stop();
101     delete m_thumbProd;
102     m_thumbProd = NULL;
103     qDeleteAll(m_toDeleteProducers);
104     m_toDeleteProducers.clear();
105     qDeleteAll(m_baseTrackProducers);
106     m_baseTrackProducers.clear();
107     qDeleteAll(m_audioTrackProducers);
108     m_audioTrackProducers.clear();
109     delete m_videoOnlyProducer;
110     m_videoOnlyProducer = NULL;
111 }
112
113 void DocClipBase::setZone(QPoint zone)
114 {
115     m_properties.insert("zone_in", QString::number(zone.x()));
116     m_properties.insert("zone_out", QString::number(zone.y()));
117 }
118
119 QPoint DocClipBase::zone() const
120 {
121     QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
122     return zone;
123 }
124
125
126 bool DocClipBase::hasAudioThumb() const
127 {
128     if (m_clipType == AUDIO || m_clipType == AV || m_clipType == PLAYLIST) return true;
129     return false;
130 }
131
132 void DocClipBase::slotClearAudioCache()
133 {
134     m_audioFrameCache.clear();
135     m_audioThumbCreated = false;
136 }
137
138 /*void DocClipBase::getClipMainThumb() {
139     if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
140 }*/
141
142 KThumb *DocClipBase::thumbProducer()
143 {
144     return m_thumbProd;
145 }
146
147 bool DocClipBase::audioThumbCreated() const
148 {
149     return m_audioThumbCreated;
150 }
151
152 const QString DocClipBase::name() const
153 {
154
155     return m_properties.value("name");
156 }
157
158 const QString &DocClipBase::getId() const
159 {
160     return m_id;
161 }
162
163 const CLIPTYPE & DocClipBase::clipType() const
164 {
165     return m_clipType;
166 }
167
168 void DocClipBase::setClipType(CLIPTYPE type)
169 {
170     m_clipType = type;
171     m_properties.insert("type", QString::number((int) type));
172 }
173
174 KUrl DocClipBase::fileURL() const
175 {
176     QString res = m_properties.value("resource");
177     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
178     return KUrl();
179 }
180
181 void DocClipBase::setClipThumbFrame(const uint &ix)
182 {
183     m_properties.insert("thumbnail", QString::number((int) ix));
184 }
185
186 uint DocClipBase::getClipThumbFrame() const
187 {
188     return (uint) m_properties.value("thumbnail").toInt();
189 }
190
191 const QString DocClipBase::description() const
192 {
193     return m_properties.value("description");
194 }
195
196 bool DocClipBase::isTransparent() const
197 {
198     return (m_properties.value("transparency") == "1");
199 }
200
201 const QString DocClipBase::getProperty(const QString &prop) const
202 {
203     return m_properties.value(prop);
204 }
205
206 void DocClipBase::setDuration(GenTime dur)
207 {
208     m_duration = dur;
209     m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
210 }
211
212 const GenTime &DocClipBase::duration() const
213 {
214     return m_duration;
215 }
216
217 const GenTime DocClipBase::maxDuration() const
218 {
219     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
220         /*const GenTime dur(15000, KdenliveSettings::project_fps());
221         return dur;*/
222         return GenTime();
223     }
224     return m_duration;
225 }
226
227 bool DocClipBase::hasFileSize() const
228 {
229     return true;
230 }
231
232 qulonglong DocClipBase::fileSize() const
233 {
234     return m_properties.value("file_size").toULongLong();
235 }
236
237 // virtual
238 QDomElement DocClipBase::toXML() const
239 {
240     QDomDocument doc;
241     QDomElement clip = doc.createElement("producer");
242
243     QMapIterator<QString, QString> i(m_properties);
244     while (i.hasNext()) {
245         i.next();
246         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
247     }
248     doc.appendChild(clip);
249     if (!m_cutZones.isEmpty()) {
250         QStringList cuts;
251         for (int i = 0; i < m_cutZones.size(); i++) {
252             CutZoneInfo info = m_cutZones.at(i);
253             cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
254         }
255         clip.setAttribute("cutzones", cuts.join(";"));
256     }
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 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
268 {
269     //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
270     m_audioFrameCache = data;
271     m_audioThumbCreated = true;
272     emit gotAudioData();
273 }
274
275 QList < GenTime > DocClipBase::snapMarkers() const
276 {
277     QList < GenTime > markers;
278
279     for (int count = 0; count < m_snapMarkers.count(); ++count) {
280         markers.append(m_snapMarkers.at(count).time());
281     }
282
283     return markers;
284 }
285
286 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
287 {
288     return m_snapMarkers;
289 }
290
291
292 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
293 {
294     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
295     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
296         if ((*it).time() >= time)
297             break;
298     }
299
300     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
301         (*it).setComment(comment);
302         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
303     } else {
304         CommentedTime t(time, comment);
305         m_snapMarkers.insert(it, t);
306     }
307
308 }
309
310 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
311 {
312     QList < CommentedTime >::Iterator it;
313     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
314         if ((*it).time() == time)
315             break;
316     }
317     if (it != m_snapMarkers.end()) {
318         (*it).setComment(comment);
319     } else {
320         kError() << "trying to edit Snap Marker that does not already exists";
321     }
322 }
323
324 QString DocClipBase::deleteSnapMarker(const GenTime & time)
325 {
326     QString result = i18n("Marker");
327     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
328
329     while (itt != m_snapMarkers.end()) {
330         if ((*itt).time() == time)
331             break;
332         ++itt;
333     }
334
335     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
336         result = (*itt).comment();
337         m_snapMarkers.erase(itt);
338     }
339     return result;
340 }
341
342
343 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
344 {
345     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
346
347     while (itt != m_snapMarkers.end()) {
348         if ((*itt).time() == time)
349             return time;
350         ++itt;
351     }
352
353     return GenTime(0.0);
354 }
355
356 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
357 {
358     int it;
359     for (it = 0; it < m_snapMarkers.count(); it++) {
360         if (m_snapMarkers.at(it).time() >= currTime)
361             break;
362     }
363     if (it == 0) return GenTime();
364     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
365         return m_snapMarkers.at(it).time();
366     else return m_snapMarkers.at(it - 1).time();
367 }
368
369 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
370 {
371     int it;
372     for (it = 0; it < m_snapMarkers.count(); it++) {
373         if (m_snapMarkers.at(it).time() > currTime)
374             break;
375     }
376     if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
377     return duration();
378 }
379
380 QString DocClipBase::markerComment(GenTime t)
381 {
382     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
383
384     while (itt != m_snapMarkers.end()) {
385         if ((*itt).time() == t)
386             return (*itt).comment();
387         ++itt;
388     }
389     return QString();
390 }
391
392 void DocClipBase::clearThumbProducer()
393 {
394     if (m_thumbProd) m_thumbProd->clearProducer();
395 }
396
397 void DocClipBase::reloadThumbProducer()
398 {
399     if (m_thumbProd && !m_thumbProd->hasProducer())
400         m_thumbProd->setProducer(getProducer());
401 }
402
403 void DocClipBase::deleteProducers()
404 {
405     if (m_thumbProd) m_thumbProd->clearProducer();
406     
407     if (numReferences() > 0) {
408         // Clip is used in timeline, delay producers deletion
409         if (m_videoOnlyProducer) m_toDeleteProducers.append(m_videoOnlyProducer);
410         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
411             m_toDeleteProducers.append(m_baseTrackProducers.at(i));
412         }
413         for (int i = 0; i < m_audioTrackProducers.count(); i++) {
414             m_toDeleteProducers.append(m_audioTrackProducers.at(i));
415         }
416     }
417     else {
418         delete m_videoOnlyProducer;
419         qDeleteAll(m_baseTrackProducers);
420         qDeleteAll(m_audioTrackProducers);
421         m_replaceMutex.unlock();
422     }
423     m_videoOnlyProducer = NULL;
424     m_baseTrackProducers.clear();
425     m_audioTrackProducers.clear();
426 }
427
428 void DocClipBase::cleanupProducers()
429 {
430     /*
431     int ct = 0;
432     kDebug()<<"----------------------------------------------------------------------------------";
433     for (int i = 0; i < m_toDeleteProducers.count(); i++) {
434         if (m_toDeleteProducers.at(i) != NULL) {
435             Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
436             if (props.ref_count() > 2) {
437                 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
438                 //exit(1);
439             }
440             ct++;
441         }
442     }*/
443
444     qDeleteAll(m_toDeleteProducers);
445     m_toDeleteProducers.clear();
446     m_replaceMutex.unlock();
447 }
448
449 bool DocClipBase::isClean() const
450 {
451     return m_toDeleteProducers.isEmpty();
452 }
453
454 void DocClipBase::setValid()
455 {
456     m_placeHolder = false;
457 }
458
459 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
460 {
461     if (producer == NULL) return;
462     if (reset) {
463         QMutexLocker locker(&m_producerMutex);
464         m_replaceMutex.lock();
465         deleteProducers();
466     }
467     QString id = producer->get("id");
468     if (m_placeHolder || !producer->is_valid()) {
469         char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
470         producer->set("markup", tmp);
471         producer->set("bgcolour", "0xff0000ff");
472         producer->set("pad", "10");
473         delete[] tmp;
474     }
475     else if (m_thumbProd && !m_thumbProd->hasProducer()) {
476         if (m_clipType != AUDIO) {
477             if (!id.endsWith("_audio"))
478                 m_thumbProd->setProducer(producer);
479         }
480         else m_thumbProd->setProducer(producer);
481         getAudioThumbs();
482     }
483     bool updated = false;
484     if (id.contains('_')) {
485         // this is a subtrack producer, insert it at correct place
486         id = id.section('_', 1);
487         if (id.endsWith("audio")) {
488             int pos = id.section('_', 0, 0).toInt();
489             if (pos >= m_audioTrackProducers.count()) {
490                 while (m_audioTrackProducers.count() - 1 < pos) {
491                     m_audioTrackProducers.append(NULL);
492                 }
493             }
494             if (m_audioTrackProducers.at(pos) == NULL) {
495                 m_audioTrackProducers[pos] = producer;
496                 updated = true;
497             }
498             else delete producer;
499             return;
500         } else if (id.endsWith("video")) {
501             if (m_videoOnlyProducer == NULL) {
502                 m_videoOnlyProducer = producer;
503                 updated = true;
504             }
505             else delete producer;
506             return;
507         }
508         int pos = id.toInt();
509         if (pos >= m_baseTrackProducers.count()) {
510             while (m_baseTrackProducers.count() - 1 < pos) {
511                 m_baseTrackProducers.append(NULL);
512             }
513         }
514         if (m_baseTrackProducers.at(pos) == NULL) {
515             m_baseTrackProducers[pos] = producer;
516             updated = true;
517         }
518         else delete producer;
519     } else {
520         if (m_baseTrackProducers.isEmpty()) {
521             m_baseTrackProducers.append(producer);
522             updated = true;
523         }
524         else if (m_baseTrackProducers.at(0) == NULL) {
525             m_baseTrackProducers[0] = producer;
526             updated = true;
527         }
528         else delete producer;
529     }
530     if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
531         setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
532 }
533
534 static double getPixelAspect(QMap<QString, QString>& props) {
535     int width = props.value("frame_size").section('x', 0, 0).toInt();
536     int height = props.value("frame_size").section('x', 1, 1).toInt();
537     int aspectNumerator = props.value("force_aspect_num").toInt();
538     int aspectDenominator = props.value("force_aspect_den").toInt();
539     if (aspectDenominator != 0 && width != 0)
540         return double(height) * aspectNumerator / aspectDenominator / width;    
541     else
542         return 1.0;
543 }
544
545 Mlt::Producer *DocClipBase::audioProducer(int track)
546 {
547     QMutexLocker locker(&m_producerMutex);
548     if (m_audioTrackProducers.count() <= track) {
549         while (m_audioTrackProducers.count() - 1 < track) {
550             m_audioTrackProducers.append(NULL);
551         }
552     }
553     if (m_audioTrackProducers.at(track) == NULL) {
554         int i;
555         for (i = 0; i < m_audioTrackProducers.count(); i++)
556             if (m_audioTrackProducers.at(i) != NULL) break;
557         Mlt::Producer *base;
558         if (i >= m_audioTrackProducers.count()) {
559             // Could not find a valid producer for that clip
560             locker.unlock();
561             base = getProducer();
562             if (base == NULL) {
563                 return NULL;
564             }
565             locker.relock();
566         }
567         else base = m_audioTrackProducers.at(i);
568         m_audioTrackProducers[track] = cloneProducer(base);
569         adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
570     }
571     return m_audioTrackProducers.at(track);
572 }
573
574
575 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
576 {
577         if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
578             prod->set("force_aspect_ratio", getPixelAspect(m_properties));
579         if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
580         if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
581         if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
582         if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
583         if (mute) prod->set("audio_index", -1);
584         else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
585         if (blind) prod->set("video_index", -1);
586         else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
587         prod->set("id", id.toUtf8().constData());
588         if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
589         if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
590         if (m_properties.contains("proxy_out")) {
591             // We have a proxy clip, make sure the proxy has same duration as original
592             prod->set("length", m_properties.value("duration").toInt());
593             prod->set("out", m_properties.value("proxy_out").toInt());
594         }
595
596 }
597
598 Mlt::Producer *DocClipBase::videoProducer()
599 {
600     QMutexLocker locker(&m_producerMutex);
601     if (m_videoOnlyProducer == NULL) {
602         int i;
603         for (i = 0; i < m_baseTrackProducers.count(); i++)
604             if (m_baseTrackProducers.at(i) != NULL) break;
605         if (i >= m_baseTrackProducers.count()) return NULL;
606         m_videoOnlyProducer = cloneProducer(m_baseTrackProducers.at(i));
607         adjustProducerProperties(m_videoOnlyProducer, QString(getId() + "_video"), true, false);
608     }
609     return m_videoOnlyProducer;
610 }
611
612 Mlt::Producer *DocClipBase::getCloneProducer()
613 {
614     Mlt::Producer *source = NULL;
615     Mlt::Producer *prod = NULL;
616     if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
617         source = getProducer();
618         if (!source) return NULL;
619     }
620     if (m_clipType == COLOR) {
621         prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
622     } else if (m_clipType == TEXT) {
623         prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
624         if (prod && prod->is_valid() && m_properties.contains("xmldata"))
625             prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
626     }
627     if (!prod) {
628         if (!source) {
629             QMutexLocker locker(&m_producerMutex);
630             for (int i = 0; i < m_baseTrackProducers.count(); i++) {
631                 if (m_baseTrackProducers.at(i) != NULL) {
632                     source = m_baseTrackProducers.at(i);
633                     break;
634                 }
635             }
636             if (!source) return NULL;
637         }
638         prod = cloneProducer(source);
639     }
640     if (prod) {
641         adjustProducerProperties(prod, getId() + "_", false, false);
642         if (!m_properties.contains("proxy_out")) {
643             // Adjust length in case...
644             if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
645             if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
646         }
647     }
648     return prod;
649 }
650
651 Mlt::Producer *DocClipBase::getProducer(int track)
652 {
653     QMutexLocker locker(&m_producerMutex);
654     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
655         if (m_baseTrackProducers.count() == 0) {
656             return NULL;
657         }
658         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
659             if (m_baseTrackProducers.at(i) != NULL) {
660                 return m_baseTrackProducers.at(i);
661             }
662         }
663         return NULL;
664     }
665     if (track >= m_baseTrackProducers.count()) {
666         while (m_baseTrackProducers.count() - 1 < track) {
667             m_baseTrackProducers.append(NULL);
668         }
669     }
670     if (m_baseTrackProducers.at(track) == NULL) {
671         int i;
672         for (i = 0; i < m_baseTrackProducers.count(); i++)
673             if (m_baseTrackProducers.at(i) != NULL) break;
674
675         if (i >= m_baseTrackProducers.count()) {
676             // Could not find a valid producer for that clip, check in 
677             return NULL;
678         }
679         Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
680         adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
681         m_baseTrackProducers[track] = prod;
682     }
683     return m_baseTrackProducers.at(track);
684 }
685
686
687 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
688 {
689     Mlt::Producer *result = NULL;
690     QString url = QString::fromUtf8(source->get("resource"));
691     if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
692         result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
693     }
694     if (result == NULL || !result->is_valid()) {
695         // placeholder clip
696         QString txt = "+" + i18n("Missing clip") + ".txt";
697         char *tmp = qstrdup(txt.toUtf8().constData());
698         result = new Mlt::Producer(*source->profile(), tmp);
699         delete[] tmp;
700         if (result == NULL || !result->is_valid())
701             result = new Mlt::Producer(*(source->profile()), "colour:red");
702         else {
703             result->set("bgcolour", "0xff0000ff");
704             result->set("pad", "10");
705         }
706         return result;
707     }
708     /*Mlt::Properties src_props(source->get_properties());
709     Mlt::Properties props(result->get_properties());
710     props.inherit(src_props);*/
711     return result;
712 }
713
714 void DocClipBase::setProducerProperty(const char *name, int data)
715 {
716     QMutexLocker locker(&m_producerMutex);
717     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
718         if (m_baseTrackProducers.at(i) != NULL)
719             m_baseTrackProducers[i]->set(name, data);
720     }
721 }
722
723 void DocClipBase::setProducerProperty(const char *name, double data)
724 {
725     QMutexLocker locker(&m_producerMutex);
726     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
727         if (m_baseTrackProducers.at(i) != NULL)
728             m_baseTrackProducers[i]->set(name, data);
729     }
730 }
731
732 void DocClipBase::setProducerProperty(const char *name, const char *data)
733 {
734     QMutexLocker locker(&m_producerMutex);
735     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
736         if (m_baseTrackProducers.at(i) != NULL)
737             m_baseTrackProducers[i]->set(name, data);
738     }
739 }
740
741 void DocClipBase::resetProducerProperty(const char *name)
742 {
743     QMutexLocker locker(&m_producerMutex);
744     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
745         if (m_baseTrackProducers.at(i) != NULL)
746             m_baseTrackProducers[i]->set(name, (const char*) NULL);
747     }
748 }
749
750 const char *DocClipBase::producerProperty(const char *name) const
751 {
752     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
753         if (m_baseTrackProducers.at(i) != NULL) {
754             return m_baseTrackProducers.at(i)->get(name);
755         }
756     }
757     return NULL;
758 }
759
760
761 void DocClipBase::slotRefreshProducer()
762 {
763     if (m_baseTrackProducers.count() == 0) return;
764     if (m_clipType == SLIDESHOW) {
765         setProducerProperty("ttl", getProperty("ttl").toInt());
766         //m_clipProducer->set("id", getProperty("id"));
767         if (!getProperty("animation").isEmpty()) {
768             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
769             int ct = 0;
770             Mlt::Filter *filter = clipService.filter(ct);
771             while (filter) {
772                 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
773                     break;
774                 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
775                     clipService.detach(*filter);
776                 } else ct++;
777                 filter = clipService.filter(ct);
778             }
779
780             if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
781                 // filter does not exist, create it.
782                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
783                 if (filter && filter->is_valid()) {
784                     int cycle = getProperty("ttl").toInt();
785                     QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
786                     if (!geometry.isEmpty()) {
787                         if (getProperty("animation").contains("low-pass")) {
788                             Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
789                             if (blur && blur->is_valid())
790                                 clipService.attach(*blur);
791                         }
792                         filter->set("transition.geometry", geometry.toUtf8().data());
793                         filter->set("transition.cycle", cycle);
794                         clipService.attach(*filter);
795                     }
796                 }
797             }
798         } else {
799             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
800             int ct = 0;
801             Mlt::Filter *filter = clipService.filter(0);
802             while (filter) {
803                 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
804                     clipService.detach(*filter);
805                 } else ct++;
806                 filter = clipService.filter(ct);
807             }
808         }
809         if (getProperty("fade") == "1") {
810             // we want a fade filter effect
811             kDebug() << "////////////   FADE WANTED";
812             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
813             int ct = 0;
814             Mlt::Filter *filter = clipService.filter(ct);
815             while (filter) {
816                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
817                     break;
818                 }
819                 ct++;
820                 filter = clipService.filter(ct);
821             }
822
823             if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
824                 filter->set("cycle", getProperty("ttl").toInt());
825                 filter->set("duration", getProperty("luma_duration").toInt());
826                 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
827                 if (!getProperty("softness").isEmpty()) {
828                     int soft = getProperty("softness").toInt();
829                     filter->set("luma.softness", (double) soft / 100.0);
830                 }
831             } else {
832                 // filter does not exist, create it...
833                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
834                 filter->set("cycle", getProperty("ttl").toInt());
835                 filter->set("duration", getProperty("luma_duration").toInt());
836                 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
837                 if (!getProperty("softness").isEmpty()) {
838                     int soft = getProperty("softness").toInt();
839                     filter->set("luma.softness", (double) soft / 100.0);
840                 }
841                 clipService.attach(*filter);
842             }
843         } else {
844             kDebug() << "////////////   FADE NOT WANTED!!!";
845             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
846             int ct = 0;
847             Mlt::Filter *filter = clipService.filter(0);
848             while (filter) {
849                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
850                     clipService.detach(*filter);
851                 } else ct++;
852                 filter = clipService.filter(ct);
853             }
854         }
855         if (getProperty("crop") == "1") {
856             // we want a center crop filter effect
857             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
858             int ct = 0;
859             Mlt::Filter *filter = clipService.filter(ct);
860             while (filter) {
861                 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
862                     break;
863                 }
864                 ct++;
865                 filter = clipService.filter(ct);
866             }
867
868             if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
869                 // filter does not exist, create it...
870                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
871                 filter->set("center", 1);
872                 clipService.attach(*filter);
873             }
874         } else {
875             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
876             int ct = 0;
877             Mlt::Filter *filter = clipService.filter(0);
878             while (filter) {
879                 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
880                     clipService.detach(*filter);
881                 } else ct++;
882                 filter = clipService.filter(ct);
883             }
884         }
885     }
886 }
887
888 void DocClipBase::setProperties(QMap <QString, QString> properties)
889 {
890     // changing clip type is not allowed
891     properties.remove("type");
892     QMapIterator<QString, QString> i(properties);
893     bool refreshProducer = false;
894     QStringList keys;
895     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
896     QString oldProxy = m_properties.value("proxy");
897     while (i.hasNext()) {
898         i.next();
899         setProperty(i.key(), i.value());
900         if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
901     }
902     if (properties.contains("proxy")) {
903         QString value = properties.value("proxy");
904         // If value is "-", that means user manually disabled proxy on this clip
905         if (value.isEmpty() || value == "-") {
906             // reset proxy
907             emit abortProxy(m_id, oldProxy);
908         }
909         else {
910             emit createProxy(m_id);
911         }
912     }
913     if (refreshProducer) slotRefreshProducer();
914 }
915
916 void DocClipBase::setMetadata(QMap <QString, QString> properties)
917 {
918     QMapIterator<QString, QString> i(properties);
919     while (i.hasNext()) {
920         i.next();
921         if (i.value().isEmpty() && m_metadata.contains(i.key())) {
922             m_metadata.remove(i.key());
923         } else {
924             m_metadata.insert(i.key(), i.value());
925         }
926     }
927 }
928
929 QMap <QString, QString> DocClipBase::metadata() const
930 {
931     return m_metadata;
932 }
933
934 void DocClipBase::clearProperty(const QString &key)
935 {
936     m_properties.remove(key);
937 }
938
939 void DocClipBase::getFileHash(const QString &url)
940 {
941     if (m_clipType == SLIDESHOW) return;
942     QFile file(url);
943     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
944         QByteArray fileData;
945         QByteArray fileHash;
946         //kDebug() << "SETTING HASH of" << value;
947         m_properties.insert("file_size", QString::number(file.size()));
948         /*
949                * 1 MB = 1 second per 450 files (or faster)
950                * 10 MB = 9 seconds per 450 files (or faster)
951                */
952         if (file.size() > 1000000*2) {
953             fileData = file.read(1000000);
954             if (file.seek(file.size() - 1000000))
955                 fileData.append(file.readAll());
956         } else
957             fileData = file.readAll();
958         file.close();
959         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
960         m_properties.insert("file_hash", QString(fileHash.toHex()));
961     }
962 }
963
964 bool DocClipBase::checkHash() const
965 {
966     KUrl url = fileURL();
967     if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
968     return true;
969 }
970
971 QString DocClipBase::getClipHash() const
972 {
973     QString hash;
974     if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
975     else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
976     else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
977     else {
978         if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
979         if (hash.isEmpty()) hash = getHash(fileURL().path());
980         
981     }
982     return hash;
983 }
984
985 void DocClipBase::setPlaceHolder(bool place)
986 {
987     m_placeHolder = place;
988 }
989
990 // static
991 QString DocClipBase::getHash(const QString &path)
992 {
993     QFile file(path);
994     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
995         QByteArray fileData;
996         QByteArray fileHash;
997         /*
998                * 1 MB = 1 second per 450 files (or faster)
999                * 10 MB = 9 seconds per 450 files (or faster)
1000                */
1001         if (file.size() > 1000000*2) {
1002             fileData = file.read(1000000);
1003             if (file.seek(file.size() - 1000000))
1004                 fileData.append(file.readAll());
1005         } else
1006             fileData = file.readAll();
1007         file.close();
1008         return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1009     }
1010     return QString();
1011 }
1012
1013 void DocClipBase::refreshThumbUrl()
1014 {
1015     if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1016 }
1017
1018 void DocClipBase::setProperty(const QString &key, const QString &value)
1019 {
1020     m_properties.insert(key, value);
1021     if (key == "resource") {
1022         getFileHash(value);
1023         if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1024     } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1025     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1026     else if (key == "colour") {
1027         setProducerProperty("colour", value.toUtf8().data());
1028     } else if (key == "templatetext") {
1029         setProducerProperty("templatetext", value.toUtf8().data());
1030         setProducerProperty("force_reload", 1);
1031     } else if (key == "xmldata") {
1032         setProducerProperty("xmldata", value.toUtf8().data());
1033         setProducerProperty("force_reload", 1);
1034     } else if (key == "force_aspect_num") {
1035         if (value.isEmpty()) {
1036             m_properties.remove("force_aspect_num");
1037             resetProducerProperty("force_aspect_ratio");
1038         } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1039     } else if (key == "force_aspect_den") {
1040         if (value.isEmpty()) {
1041             m_properties.remove("force_aspect_den");
1042             resetProducerProperty("force_aspect_ratio");
1043         } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1044     } else if (key == "force_fps") {
1045         if (value.isEmpty()) {
1046             m_properties.remove("force_fps");
1047             resetProducerProperty("force_fps");
1048         } else setProducerProperty("force_fps", value.toDouble());
1049     } else if (key == "force_progressive") {
1050         if (value.isEmpty()) {
1051             m_properties.remove("force_progressive");
1052             resetProducerProperty("force_progressive");
1053         } else setProducerProperty("force_progressive", value.toInt());
1054     } else if (key == "force_tff") {
1055         if (value.isEmpty()) {
1056             m_properties.remove("force_tff");
1057             resetProducerProperty("force_tff");
1058         } else setProducerProperty("force_tff", value.toInt());
1059     } else if (key == "threads") {
1060         if (value.isEmpty()) {
1061             m_properties.remove("threads");
1062             setProducerProperty("threads", 1);
1063         } else setProducerProperty("threads", value.toInt());
1064     } else if (key == "video_index") {
1065         if (value.isEmpty()) {
1066             m_properties.remove("video_index");
1067             setProducerProperty("video_index", m_properties.value("default_video").toInt());
1068         } else setProducerProperty("video_index", value.toInt());
1069     } else if (key == "audio_index") {
1070         if (value.isEmpty()) {
1071             m_properties.remove("audio_index");
1072             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1073         } else setProducerProperty("audio_index", value.toInt());
1074     } else if (key == "force_colorspace") {
1075         if (value.isEmpty()) {
1076             m_properties.remove("force_colorspace");
1077             resetProducerProperty("force_colorspace");
1078         } else setProducerProperty("force_colorspace", value.toInt());
1079     } else if (key == "full_luma") {
1080         if (value.isEmpty()) {
1081             m_properties.remove("full_luma");
1082             resetProducerProperty("set.force_full_luma");
1083         } else setProducerProperty("set.force_full_luma", value.toInt());
1084     }
1085 }
1086
1087 QMap <QString, QString> DocClipBase::properties() const
1088 {
1089     return m_properties;
1090 }
1091
1092 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1093 {
1094     QMap <QString, QString> currentProps;
1095     QMap<QString, QString>::const_iterator i = props.constBegin();
1096     while (i != props.constEnd()) {
1097         currentProps.insert(i.key(), m_properties.value(i.key()));
1098         ++i;
1099     }
1100     return currentProps;
1101 }
1102
1103 bool DocClipBase::getAudioThumbs()
1104 {
1105     if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1106     if (m_audioThumbCreated) {
1107         return false;
1108     }
1109     m_audioTimer.start();
1110     return true;
1111 }
1112
1113 bool DocClipBase::isPlaceHolder() const
1114 {
1115     return m_placeHolder;
1116 }
1117
1118 void DocClipBase::addCutZone(int in, int out, QString desc)
1119 {
1120     CutZoneInfo info;
1121     info.zone = QPoint(in, out);
1122     info.description = desc;
1123     for (int i = 0; i < m_cutZones.count(); i++)
1124         if (m_cutZones.at(i).zone == info.zone) {
1125             return;
1126         }
1127     m_cutZones.append(info);
1128 }
1129
1130 bool DocClipBase::hasCutZone(QPoint p) const
1131 {
1132     for (int i = 0; i < m_cutZones.count(); i++)
1133         if (m_cutZones.at(i).zone == p) return true;
1134     return false;
1135 }
1136
1137
1138 void DocClipBase::removeCutZone(int in, int out)
1139 {
1140     QPoint p(in, out);
1141     for (int i = 0; i < m_cutZones.count(); i++) {
1142         if (m_cutZones.at(i).zone == p) {
1143             m_cutZones.removeAt(i);
1144             i--;
1145         }
1146     }
1147 }
1148
1149 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1150 {
1151     QPoint old(oldin, oldout);
1152     for (int i = 0; i < m_cutZones.size(); ++i) {
1153         if (m_cutZones.at(i).zone == old) {
1154             CutZoneInfo info;
1155             info.zone = QPoint(in, out);
1156             info.description = desc;
1157             m_cutZones.replace(i, info);
1158             break;
1159         }
1160     }
1161 }
1162
1163 QList <CutZoneInfo> DocClipBase::cutZones() const
1164 {
1165     return m_cutZones;
1166 }
1167
1168 bool DocClipBase::hasVideoCodec(const QString &codec) const
1169 {
1170     Mlt::Producer *prod = NULL;
1171     if (m_baseTrackProducers.count() == 0) return false;
1172     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1173         if (m_baseTrackProducers.at(i) != NULL) {
1174             prod = m_baseTrackProducers.at(i);
1175             break;
1176         }
1177     }
1178
1179     if (!prod) return false;
1180     int default_video = prod->get_int("video_index");
1181     char property[200];
1182     snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1183     return prod->get(property) == codec;
1184 }
1185
1186 bool DocClipBase::hasAudioCodec(const QString &codec) const
1187 {
1188     Mlt::Producer *prod = NULL;
1189     if (m_baseTrackProducers.count() == 0) return false;
1190     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1191         if (m_baseTrackProducers.at(i) != NULL) {
1192             prod = m_baseTrackProducers.at(i);
1193             break;
1194         }
1195     }
1196     if (!prod) return false;
1197     int default_video = prod->get_int("audio_index");
1198     char property[200];
1199     snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1200     return prod->get(property) == codec;
1201 }
1202
1203
1204 void DocClipBase::slotExtractImage(QList <int> frames)
1205 {
1206     if (m_thumbProd == NULL) return;
1207     m_thumbProd->extractImage(frames);
1208 }
1209
1210 QImage DocClipBase::extractImage(int frame, int width, int height)
1211 {
1212     if (m_thumbProd == NULL) return QImage();
1213     QMutexLocker locker(&m_producerMutex);
1214     return m_thumbProd->extractImage(frame, width, height);
1215 }
1216
1217