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