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