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