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