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