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