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