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