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