]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
Clip markers can now have a category that is shown as a color.
[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_videoTrackProducers(),
48         m_audioTrackProducers(),
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     qDeleteAll(m_videoTrackProducers);
110     m_videoTrackProducers.clear();
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(bool hideTemporaryProperties) 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 (hideTemporaryProperties && i.key().startsWith('_')) continue;
247         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
248     }
249     doc.appendChild(clip);
250     if (!m_cutZones.isEmpty()) {
251         QStringList cuts;
252         for (int i = 0; i < m_cutZones.size(); i++) {
253             CutZoneInfo info = m_cutZones.at(i);
254             cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
255         }
256         clip.setAttribute("cutzones", cuts.join(";"));
257     }
258     //kDebug() << "/// CLIP XML: " << doc.toString();
259     return doc.documentElement();
260 }
261
262
263 void DocClipBase::setAudioThumbCreated(bool isDone)
264 {
265     m_audioThumbCreated = isDone;
266 }
267
268 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
269 {
270     //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
271     m_audioFrameCache = data;
272     m_audioThumbCreated = true;
273     emit gotAudioData();
274 }
275
276 QList < GenTime > DocClipBase::snapMarkers() const
277 {
278     QList < GenTime > markers;
279
280     for (int count = 0; count < m_snapMarkers.count(); ++count) {
281         markers.append(m_snapMarkers.at(count).time());
282     }
283
284     return markers;
285 }
286
287 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
288 {
289     return m_snapMarkers;
290 }
291
292
293 void DocClipBase::addSnapMarker(const CommentedTime marker)
294 {
295     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
296     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
297         if ((*it).time() >= marker.time())
298             break;
299     }
300
301     if ((it != m_snapMarkers.end()) && ((*it).time() == marker.time())) {
302         (*it).setComment(marker.comment());
303         (*it).setMarkerType(marker.markerType());
304         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
305     } else {
306         m_snapMarkers.insert(it, marker);
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) const
381 {
382     QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
383     while (itt != m_snapMarkers.end()) {
384         if ((*itt).time() == t)
385             return (*itt).comment();
386         ++itt;
387     }
388     return QString();
389 }
390
391 CommentedTime DocClipBase::markerAt(GenTime t) const
392 {
393     QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
394     while (itt != m_snapMarkers.end()) {
395         if ((*itt).time() == t)
396             return (*itt);
397         ++itt;
398     }
399     return CommentedTime();
400 }
401
402 void DocClipBase::clearThumbProducer()
403 {
404     if (m_thumbProd) m_thumbProd->clearProducer();
405 }
406
407 void DocClipBase::reloadThumbProducer()
408 {
409     if (m_thumbProd && !m_thumbProd->hasProducer())
410         m_thumbProd->setProducer(getProducer());
411 }
412
413 void DocClipBase::deleteProducers()
414 {
415     if (m_thumbProd) m_thumbProd->clearProducer();
416     
417     if (numReferences() > 0 && (!m_baseTrackProducers.isEmpty() || !m_videoTrackProducers.isEmpty() || !m_audioTrackProducers.isEmpty())) {
418         // Clip is used in timeline, delay producers deletion
419         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
420             m_toDeleteProducers.append(m_baseTrackProducers.at(i));
421         }
422         for (int i = 0; i < m_videoTrackProducers.count(); i++) {
423             m_toDeleteProducers.append(m_videoTrackProducers.at(i));
424         }
425         for (int i = 0; i < m_audioTrackProducers.count(); i++) {
426             m_toDeleteProducers.append(m_audioTrackProducers.at(i));
427         }
428     }
429     else {
430         qDeleteAll(m_baseTrackProducers);
431         qDeleteAll(m_videoTrackProducers);
432         qDeleteAll(m_audioTrackProducers);
433         m_replaceMutex.unlock();
434     }
435     m_baseTrackProducers.clear();
436     m_videoTrackProducers.clear();
437     m_audioTrackProducers.clear();
438 }
439
440 void DocClipBase::cleanupProducers()
441 {
442     /*
443     int ct = 0;
444     kDebug()<<"----------------------------------------------------------------------------------";
445     for (int i = 0; i < m_toDeleteProducers.count(); i++) {
446         if (m_toDeleteProducers.at(i) != NULL) {
447             Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
448             if (props.ref_count() > 2) {
449                 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
450                 //exit(1);
451             }
452             ct++;
453         }
454     }*/
455
456     if (!isClean()) {
457       qDeleteAll(m_toDeleteProducers);
458       m_toDeleteProducers.clear();
459       m_replaceMutex.unlock();
460     }
461 }
462
463 bool DocClipBase::isClean() const
464 {
465     return m_toDeleteProducers.isEmpty();
466 }
467
468 void DocClipBase::setValid()
469 {
470     m_placeHolder = false;
471 }
472
473 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
474 {
475     if (producer == NULL) return;
476     if (reset) {
477         QMutexLocker locker(&m_producerMutex);
478         m_replaceMutex.lock();
479         deleteProducers();
480     }
481     QString id = producer->get("id");
482     if (m_placeHolder || !producer->is_valid()) {
483         char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
484         producer->set("markup", tmp);
485         producer->set("bgcolour", "0xff0000ff");
486         producer->set("pad", "10");
487         delete[] tmp;
488     }
489     else if (m_thumbProd && !m_thumbProd->hasProducer()) {
490         if (m_clipType != AUDIO) {
491             if (!id.endsWith("_audio"))
492                 m_thumbProd->setProducer(producer);
493         }
494         else m_thumbProd->setProducer(producer);
495         getAudioThumbs();
496     }
497     bool updated = false;
498     if (id.contains('_')) {
499         // this is a subtrack producer, insert it at correct place
500         id = id.section('_', 1);
501         if (id.endsWith("audio")) {
502             int pos = id.section('_', 0, 0).toInt();
503             if (pos >= m_audioTrackProducers.count()) {
504                 while (m_audioTrackProducers.count() - 1 < pos) {
505                     m_audioTrackProducers.append(NULL);
506                 }
507             }
508             if (m_audioTrackProducers.at(pos) == NULL) {
509                 m_audioTrackProducers[pos] = producer;
510                 updated = true;
511             }
512             else delete producer;
513             return;
514         } else if (id.endsWith("video")) {
515             int pos = 0;
516             // Keep compatibility with older projects where video only producers were not track specific
517             if (id.contains('_')) pos = id.section('_', 0, 0).toInt();
518             if (pos >= m_videoTrackProducers.count()) {
519                 while (m_videoTrackProducers.count() - 1 < pos) {
520                     m_videoTrackProducers.append(NULL);
521                 }
522             }
523             if (m_videoTrackProducers.at(pos) == NULL) {
524                 m_videoTrackProducers[pos] = producer;
525                 updated = true;
526             }
527             else delete producer;
528             return;
529         }
530         int pos = id.toInt();
531         if (pos >= m_baseTrackProducers.count()) {
532             while (m_baseTrackProducers.count() - 1 < pos) {
533                 m_baseTrackProducers.append(NULL);
534             }
535         }
536         if (m_baseTrackProducers.at(pos) == NULL) {
537             m_baseTrackProducers[pos] = producer;
538             updated = true;
539         }
540         else delete producer;
541     } else {
542         if (m_baseTrackProducers.isEmpty()) {
543             m_baseTrackProducers.append(producer);
544             updated = true;
545         }
546         else if (m_baseTrackProducers.at(0) == NULL) {
547             m_baseTrackProducers[0] = producer;
548             updated = true;
549         }
550         else delete producer;
551     }
552     if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
553         setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
554 }
555
556 static double getPixelAspect(QMap<QString, QString>& props) {
557     int width = props.value("frame_size").section('x', 0, 0).toInt();
558     int height = props.value("frame_size").section('x', 1, 1).toInt();
559     int aspectNumerator = props.value("force_aspect_num").toInt();
560     int aspectDenominator = props.value("force_aspect_den").toInt();
561     if (aspectDenominator != 0 && width != 0)
562         return double(height) * aspectNumerator / aspectDenominator / width;    
563     else
564         return 1.0;
565 }
566
567 Mlt::Producer *DocClipBase::audioProducer(int track)
568 {
569     QMutexLocker locker(&m_producerMutex);
570     if (m_audioTrackProducers.count() <= track) {
571         while (m_audioTrackProducers.count() - 1 < track) {
572             m_audioTrackProducers.append(NULL);
573         }
574     }
575     if (m_audioTrackProducers.at(track) == NULL) {
576         int i;
577         for (i = 0; i < m_audioTrackProducers.count(); i++)
578             if (m_audioTrackProducers.at(i) != NULL) break;
579         Mlt::Producer *base;
580         if (i >= m_audioTrackProducers.count()) {
581             // Could not find a valid producer for that clip
582             locker.unlock();
583             base = getProducer();
584             if (base == NULL) {
585                 return NULL;
586             }
587             locker.relock();
588         }
589         else base = m_audioTrackProducers.at(i);
590         m_audioTrackProducers[track] = cloneProducer(base);
591         adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
592     }
593     return m_audioTrackProducers.at(track);
594 }
595
596
597 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
598 {
599         if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
600             prod->set("force_aspect_ratio", getPixelAspect(m_properties));
601         if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
602         if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
603         if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
604         if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
605         if (mute) prod->set("audio_index", -1);
606         else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
607         if (blind) prod->set("video_index", -1);
608         else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
609         prod->set("id", id.toUtf8().constData());
610         if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
611         if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
612         if (m_properties.contains("proxy_out")) {
613             // We have a proxy clip, make sure the proxy has same duration as original
614             prod->set("length", m_properties.value("duration").toInt());
615             prod->set("out", m_properties.value("proxy_out").toInt());
616         }
617
618 }
619
620 Mlt::Producer *DocClipBase::videoProducer(int track)
621 {
622     QMutexLocker locker(&m_producerMutex);
623     if (m_videoTrackProducers.count() <= track) {
624         while (m_videoTrackProducers.count() - 1 < track) {
625             m_videoTrackProducers.append(NULL);
626         }
627     }
628     if (m_videoTrackProducers.at(track) == NULL) {
629         int i;
630         for (i = 0; i < m_videoTrackProducers.count(); i++)
631             if (m_videoTrackProducers.at(i) != NULL) break;
632         Mlt::Producer *base;
633         if (i >= m_videoTrackProducers.count()) {
634             // Could not find a valid producer for that clip
635             locker.unlock();
636             base = getProducer();
637             if (base == NULL) {
638                 return NULL;
639             }
640             locker.relock();
641         }
642         else base = m_videoTrackProducers.at(i);
643         m_videoTrackProducers[track] = cloneProducer(base);
644         adjustProducerProperties(m_videoTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_video"), true, false);
645     }
646     return m_videoTrackProducers.at(track);
647 }
648
649 Mlt::Producer *DocClipBase::getCloneProducer()
650 {
651     Mlt::Producer *source = NULL;
652     Mlt::Producer *prod = NULL;
653     if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
654         source = getProducer();
655         if (!source) return NULL;
656     }
657     if (m_clipType == COLOR) {
658         prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
659     } else if (m_clipType == TEXT) {
660         prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
661         if (prod && prod->is_valid() && m_properties.contains("xmldata"))
662             prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
663     }
664     if (!prod) {
665         if (!source) {
666             QMutexLocker locker(&m_producerMutex);
667             for (int i = 0; i < m_baseTrackProducers.count(); i++) {
668                 if (m_baseTrackProducers.at(i) != NULL) {
669                     source = m_baseTrackProducers.at(i);
670                     break;
671                 }
672             }
673             if (!source) return NULL;
674         }
675         prod = cloneProducer(source);
676     }
677     if (prod) {
678         adjustProducerProperties(prod, getId() + "_", false, false);
679         if (!m_properties.contains("proxy_out")) {
680             // Adjust length in case...
681             if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
682             if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
683         }
684         if (m_clipType == AUDIO) {
685             prod->set("_audioclip", 1);
686         }
687     }
688     return prod;
689 }
690
691
692 Mlt::Producer *DocClipBase::getProducer(int track)
693 {
694     QMutexLocker locker(&m_producerMutex);
695     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
696         if (m_baseTrackProducers.count() == 0) {
697             return NULL;
698         }
699         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
700             if (m_baseTrackProducers.at(i) != NULL) {
701                 return m_baseTrackProducers.at(i);
702             }
703         }
704         return NULL;
705     }
706     if (track >= m_baseTrackProducers.count()) {
707         while (m_baseTrackProducers.count() - 1 < track) {
708             m_baseTrackProducers.append(NULL);
709         }
710     }
711     if (m_baseTrackProducers.at(track) == NULL) {
712         int i;
713         for (i = 0; i < m_baseTrackProducers.count(); i++)
714             if (m_baseTrackProducers.at(i) != NULL) break;
715
716         if (i >= m_baseTrackProducers.count()) {
717             // Could not find a valid producer for that clip, check in 
718             return NULL;
719         }
720         Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
721         adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
722         m_baseTrackProducers[track] = prod;
723     }
724     return m_baseTrackProducers.at(track);
725 }
726
727
728 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
729 {
730     Mlt::Producer *result = NULL;
731     QString url = QString::fromUtf8(source->get("resource"));
732     if (url == "<playlist>" || url == "<tractor>" || url == "<producer>") {
733         // Xml producer sometimes loses the correct url
734         url = m_properties.value("resource");
735     }
736     if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
737         result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
738     }
739     if (result == NULL || !result->is_valid()) {
740         // placeholder clip
741         QString txt = "+" + i18n("Missing clip") + ".txt";
742         char *tmp = qstrdup(txt.toUtf8().constData());
743         result = new Mlt::Producer(*source->profile(), tmp);
744         delete[] tmp;
745         if (result == NULL || !result->is_valid())
746             result = new Mlt::Producer(*(source->profile()), "colour:red");
747         else {
748             result->set("bgcolour", "0xff0000ff");
749             result->set("pad", "10");
750         }
751         return result;
752     }
753     /*Mlt::Properties src_props(source->get_properties());
754     Mlt::Properties props(result->get_properties());
755     props.inherit(src_props);*/
756     return result;
757 }
758
759 void DocClipBase::setProducerProperty(const char *name, int data)
760 {
761     QMutexLocker locker(&m_producerMutex);
762     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
763         if (m_baseTrackProducers.at(i) != NULL)
764             m_baseTrackProducers[i]->set(name, data);
765     }
766 }
767
768 void DocClipBase::setProducerProperty(const char *name, double data)
769 {
770     QMutexLocker locker(&m_producerMutex);
771     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
772         if (m_baseTrackProducers.at(i) != NULL)
773             m_baseTrackProducers[i]->set(name, data);
774     }
775 }
776
777 void DocClipBase::setProducerProperty(const char *name, const char *data)
778 {
779     QMutexLocker locker(&m_producerMutex);
780     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
781         if (m_baseTrackProducers.at(i) != NULL)
782             m_baseTrackProducers[i]->set(name, data);
783     }
784 }
785
786 void DocClipBase::resetProducerProperty(const char *name)
787 {
788     QMutexLocker locker(&m_producerMutex);
789     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
790         if (m_baseTrackProducers.at(i) != NULL)
791             m_baseTrackProducers[i]->set(name, (const char*) NULL);
792     }
793 }
794
795 const char *DocClipBase::producerProperty(const char *name) const
796 {
797     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
798         if (m_baseTrackProducers.at(i) != NULL) {
799             return m_baseTrackProducers.at(i)->get(name);
800         }
801     }
802     return NULL;
803 }
804
805
806 void DocClipBase::slotRefreshProducer()
807 {
808     if (m_baseTrackProducers.count() == 0) return;
809     if (m_clipType == SLIDESHOW) {
810         setProducerProperty("ttl", getProperty("ttl").toInt());
811         //m_clipProducer->set("id", getProperty("id"));
812         if (!getProperty("animation").isEmpty()) {
813             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
814             int ct = 0;
815             Mlt::Filter *filter = clipService.filter(ct);
816             while (filter) {
817                 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
818                     break;
819                 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
820                     clipService.detach(*filter);
821                 } else ct++;
822                 filter = clipService.filter(ct);
823             }
824
825             if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
826                 // filter does not exist, create it.
827                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
828                 if (filter && filter->is_valid()) {
829                     int cycle = getProperty("ttl").toInt();
830                     QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
831                     if (!geometry.isEmpty()) {
832                         if (getProperty("animation").contains("low-pass")) {
833                             Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
834                             if (blur && blur->is_valid())
835                                 clipService.attach(*blur);
836                         }
837                         filter->set("transition.geometry", geometry.toUtf8().data());
838                         filter->set("transition.cycle", cycle);
839                         clipService.attach(*filter);
840                     }
841                 }
842             }
843         } else {
844             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
845             int ct = 0;
846             Mlt::Filter *filter = clipService.filter(0);
847             while (filter) {
848                 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
849                     clipService.detach(*filter);
850                 } else ct++;
851                 filter = clipService.filter(ct);
852             }
853         }
854         if (getProperty("fade") == "1") {
855             // we want a fade filter effect
856             kDebug() << "////////////   FADE WANTED";
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"), "luma") == 0) {
862                     break;
863                 }
864                 ct++;
865                 filter = clipService.filter(ct);
866             }
867
868             if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
869                 filter->set("cycle", getProperty("ttl").toInt());
870                 filter->set("duration", getProperty("luma_duration").toInt());
871                 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
872                 if (!getProperty("softness").isEmpty()) {
873                     int soft = getProperty("softness").toInt();
874                     filter->set("luma.softness", (double) soft / 100.0);
875                 }
876             } else {
877                 // filter does not exist, create it...
878                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
879                 filter->set("cycle", getProperty("ttl").toInt());
880                 filter->set("duration", getProperty("luma_duration").toInt());
881                 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
882                 if (!getProperty("softness").isEmpty()) {
883                     int soft = getProperty("softness").toInt();
884                     filter->set("luma.softness", (double) soft / 100.0);
885                 }
886                 clipService.attach(*filter);
887             }
888         } else {
889             kDebug() << "////////////   FADE NOT WANTED!!!";
890             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
891             int ct = 0;
892             Mlt::Filter *filter = clipService.filter(0);
893             while (filter) {
894                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
895                     clipService.detach(*filter);
896                 } else ct++;
897                 filter = clipService.filter(ct);
898             }
899         }
900         if (getProperty("crop") == "1") {
901             // we want a center crop filter effect
902             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
903             int ct = 0;
904             Mlt::Filter *filter = clipService.filter(ct);
905             while (filter) {
906                 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
907                     break;
908                 }
909                 ct++;
910                 filter = clipService.filter(ct);
911             }
912
913             if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
914                 // filter does not exist, create it...
915                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
916                 filter->set("center", 1);
917                 clipService.attach(*filter);
918             }
919         } else {
920             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
921             int ct = 0;
922             Mlt::Filter *filter = clipService.filter(0);
923             while (filter) {
924                 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
925                     clipService.detach(*filter);
926                 } else ct++;
927                 filter = clipService.filter(ct);
928             }
929         }
930     }
931 }
932
933 void DocClipBase::setProperties(QMap <QString, QString> properties)
934 {
935     // changing clip type is not allowed
936     properties.remove("type");
937     QMapIterator<QString, QString> i(properties);
938     bool refreshProducer = false;
939     QStringList keys;
940     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
941     QString oldProxy = m_properties.value("proxy");
942     while (i.hasNext()) {
943         i.next();
944         setProperty(i.key(), i.value());
945         if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
946     }
947     if (properties.contains("proxy")) {
948         QString value = properties.value("proxy");
949         // If value is "-", that means user manually disabled proxy on this clip
950         if (value.isEmpty() || value == "-") {
951             // reset proxy
952             emit abortProxy(m_id, oldProxy);
953         }
954         else {
955             emit createProxy(m_id);
956         }
957     }
958     if (refreshProducer) slotRefreshProducer();
959 }
960
961 void DocClipBase::setMetadata(QMap <QString, QString> properties)
962 {
963     QMapIterator<QString, QString> i(properties);
964     while (i.hasNext()) {
965         i.next();
966         if (i.value().isEmpty() && m_metadata.contains(i.key())) {
967             m_metadata.remove(i.key());
968         } else {
969             m_metadata.insert(i.key(), i.value());
970         }
971     }
972 }
973
974 QMap <QString, QString> DocClipBase::metadata() const
975 {
976     return m_metadata;
977 }
978
979 void DocClipBase::clearProperty(const QString &key)
980 {
981     m_properties.remove(key);
982 }
983
984 void DocClipBase::getFileHash(const QString &url)
985 {
986     if (m_clipType == SLIDESHOW) return;
987     QFile file(url);
988     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
989         QByteArray fileData;
990         QByteArray fileHash;
991         //kDebug() << "SETTING HASH of" << value;
992         m_properties.insert("file_size", QString::number(file.size()));
993         /*
994                * 1 MB = 1 second per 450 files (or faster)
995                * 10 MB = 9 seconds per 450 files (or faster)
996                */
997         if (file.size() > 1000000*2) {
998             fileData = file.read(1000000);
999             if (file.seek(file.size() - 1000000))
1000                 fileData.append(file.readAll());
1001         } else
1002             fileData = file.readAll();
1003         file.close();
1004         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1005         m_properties.insert("file_hash", QString(fileHash.toHex()));
1006     }
1007 }
1008
1009 bool DocClipBase::checkHash() const
1010 {
1011     KUrl url = fileURL();
1012     if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
1013     return true;
1014 }
1015
1016 QString DocClipBase::getClipHash() const
1017 {
1018     QString hash;
1019     if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
1020     else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
1021     else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
1022     else {
1023         if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
1024         if (hash.isEmpty()) hash = getHash(fileURL().path());
1025         
1026     }
1027     return hash;
1028 }
1029
1030 void DocClipBase::setPlaceHolder(bool place)
1031 {
1032     m_placeHolder = place;
1033 }
1034
1035 // static
1036 QString DocClipBase::getHash(const QString &path)
1037 {
1038     QFile file(path);
1039     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1040         QByteArray fileData;
1041         QByteArray fileHash;
1042         /*
1043                * 1 MB = 1 second per 450 files (or faster)
1044                * 10 MB = 9 seconds per 450 files (or faster)
1045                */
1046         if (file.size() > 1000000*2) {
1047             fileData = file.read(1000000);
1048             if (file.seek(file.size() - 1000000))
1049                 fileData.append(file.readAll());
1050         } else
1051             fileData = file.readAll();
1052         file.close();
1053         return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1054     }
1055     return QString();
1056 }
1057
1058 void DocClipBase::refreshThumbUrl()
1059 {
1060     if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1061 }
1062
1063 void DocClipBase::setProperty(const QString &key, const QString &value)
1064 {
1065     m_properties.insert(key, value);
1066     if (key == "resource") {
1067         getFileHash(value);
1068         if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1069     } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1070     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1071     else if (key == "colour") {
1072         setProducerProperty("colour", value.toUtf8().data());
1073     } else if (key == "templatetext") {
1074         setProducerProperty("templatetext", value.toUtf8().data());
1075         setProducerProperty("force_reload", 1);
1076     } else if (key == "xmldata") {
1077         setProducerProperty("xmldata", value.toUtf8().data());
1078         setProducerProperty("force_reload", 1);
1079     } else if (key == "force_aspect_num") {
1080         if (value.isEmpty()) {
1081             m_properties.remove("force_aspect_num");
1082             resetProducerProperty("force_aspect_ratio");
1083         } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1084     } else if (key == "force_aspect_den") {
1085         if (value.isEmpty()) {
1086             m_properties.remove("force_aspect_den");
1087             resetProducerProperty("force_aspect_ratio");
1088         } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1089     } else if (key == "force_fps") {
1090         if (value.isEmpty()) {
1091             m_properties.remove("force_fps");
1092             resetProducerProperty("force_fps");
1093         } else setProducerProperty("force_fps", value.toDouble());
1094     } else if (key == "force_progressive") {
1095         if (value.isEmpty()) {
1096             m_properties.remove("force_progressive");
1097             resetProducerProperty("force_progressive");
1098         } else setProducerProperty("force_progressive", value.toInt());
1099     } else if (key == "force_tff") {
1100         if (value.isEmpty()) {
1101             m_properties.remove("force_tff");
1102             resetProducerProperty("force_tff");
1103         } else setProducerProperty("force_tff", value.toInt());
1104     } else if (key == "threads") {
1105         if (value.isEmpty()) {
1106             m_properties.remove("threads");
1107             setProducerProperty("threads", 1);
1108         } else setProducerProperty("threads", value.toInt());
1109     } else if (key == "video_index") {
1110         if (value.isEmpty()) {
1111             m_properties.remove("video_index");
1112             setProducerProperty("video_index", m_properties.value("default_video").toInt());
1113         } else setProducerProperty("video_index", value.toInt());
1114     } else if (key == "audio_index") {
1115         if (value.isEmpty()) {
1116             m_properties.remove("audio_index");
1117             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1118         } else setProducerProperty("audio_index", value.toInt());
1119     } else if (key == "force_colorspace") {
1120         if (value.isEmpty()) {
1121             m_properties.remove("force_colorspace");
1122             resetProducerProperty("force_colorspace");
1123         } else setProducerProperty("force_colorspace", value.toInt());
1124     } else if (key == "full_luma") {
1125         if (value.isEmpty()) {
1126             m_properties.remove("full_luma");
1127             resetProducerProperty("set.force_full_luma");
1128         } else setProducerProperty("set.force_full_luma", value.toInt());
1129     }
1130 }
1131
1132 QMap <QString, QString> DocClipBase::properties() const
1133 {
1134     return m_properties;
1135 }
1136
1137 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1138 {
1139     QMap <QString, QString> currentProps;
1140     QMap<QString, QString>::const_iterator i = props.constBegin();
1141     while (i != props.constEnd()) {
1142         currentProps.insert(i.key(), m_properties.value(i.key()));
1143         ++i;
1144     }
1145     return currentProps;
1146 }
1147
1148 bool DocClipBase::getAudioThumbs()
1149 {
1150     if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1151     if (m_audioThumbCreated) {
1152         return false;
1153     }
1154     m_audioTimer.start();
1155     return true;
1156 }
1157
1158 bool DocClipBase::isPlaceHolder() const
1159 {
1160     return m_placeHolder;
1161 }
1162
1163 void DocClipBase::addCutZone(int in, int out, QString desc)
1164 {
1165     CutZoneInfo info;
1166     info.zone = QPoint(in, out);
1167     info.description = desc;
1168     for (int i = 0; i < m_cutZones.count(); i++)
1169         if (m_cutZones.at(i).zone == info.zone) {
1170             return;
1171         }
1172     m_cutZones.append(info);
1173 }
1174
1175 bool DocClipBase::hasCutZone(QPoint p) const
1176 {
1177     for (int i = 0; i < m_cutZones.count(); i++)
1178         if (m_cutZones.at(i).zone == p) return true;
1179     return false;
1180 }
1181
1182
1183 void DocClipBase::removeCutZone(int in, int out)
1184 {
1185     QPoint p(in, out);
1186     for (int i = 0; i < m_cutZones.count(); i++) {
1187         if (m_cutZones.at(i).zone == p) {
1188             m_cutZones.removeAt(i);
1189             i--;
1190         }
1191     }
1192 }
1193
1194 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1195 {
1196     QPoint old(oldin, oldout);
1197     for (int i = 0; i < m_cutZones.size(); ++i) {
1198         if (m_cutZones.at(i).zone == old) {
1199             CutZoneInfo info;
1200             info.zone = QPoint(in, out);
1201             info.description = desc;
1202             m_cutZones.replace(i, info);
1203             break;
1204         }
1205     }
1206 }
1207
1208 QList <CutZoneInfo> DocClipBase::cutZones() const
1209 {
1210     return m_cutZones;
1211 }
1212
1213 bool DocClipBase::hasVideoCodec(const QString &codec) const
1214 {
1215     Mlt::Producer *prod = NULL;
1216     if (m_baseTrackProducers.count() == 0) return false;
1217     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1218         if (m_baseTrackProducers.at(i) != NULL) {
1219             prod = m_baseTrackProducers.at(i);
1220             break;
1221         }
1222     }
1223
1224     if (!prod) return false;
1225     int default_video = prod->get_int("video_index");
1226     char property[200];
1227     snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1228     return prod->get(property) == codec;
1229 }
1230
1231 bool DocClipBase::hasAudioCodec(const QString &codec) const
1232 {
1233     Mlt::Producer *prod = NULL;
1234     if (m_baseTrackProducers.count() == 0) return false;
1235     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1236         if (m_baseTrackProducers.at(i) != NULL) {
1237             prod = m_baseTrackProducers.at(i);
1238             break;
1239         }
1240     }
1241     if (!prod) return false;
1242     int default_video = prod->get_int("audio_index");
1243     char property[200];
1244     snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1245     return prod->get(property) == codec;
1246 }
1247
1248
1249 void DocClipBase::slotExtractImage(QList <int> frames)
1250 {
1251     if (m_thumbProd == NULL) return;
1252     m_thumbProd->extractImage(frames);
1253 }
1254
1255 QImage DocClipBase::extractImage(int frame, int width, int height)
1256 {
1257     if (m_thumbProd == NULL) return QImage();
1258     QMutexLocker locker(&m_producerMutex);
1259     return m_thumbProd->extractImage(frame, width, height);
1260 }
1261
1262