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