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