]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
Fix crash on track deletion:
[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     /*kDebug()<<"// CLIP KILL PRODS ct: "<<m_baseTrackProducers.count();
436     int max = m_baseTrackProducers.count();
437     for (int i = 0; i < max; i++) {
438         kDebug()<<"// CLIP KILL PROD "<<i;
439     Mlt::Producer *p = m_baseTrackProducers.takeAt(i);
440     if (p != NULL) {
441      delete p;
442      p = NULL;
443     }
444     m_baseTrackProducers.insert(i, NULL);
445     }*/
446
447     delete m_videoOnlyProducer;
448     m_videoOnlyProducer = NULL;
449
450     qDeleteAll(m_baseTrackProducers);
451     m_baseTrackProducers.clear();
452     qDeleteAll(m_audioTrackProducers);
453     m_audioTrackProducers.clear();
454 }
455
456 void DocClipBase::setValid()
457 {
458     m_placeHolder = false;
459 }
460
461 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
462 {
463     if (producer == NULL || (m_placeHolder && !reset)) return;
464     if (m_thumbProd && (reset || !m_thumbProd->hasProducer())) m_thumbProd->setProducer(producer);
465     if (reset) {
466         // Clear all previous producers
467         kDebug() << "/+++++++++++++++   DELETE ALL PRODS " << producer->get("id");
468         deleteProducers(false);
469     }
470     QString id = producer->get("id");
471     bool updated = false;
472     if (id.contains('_')) {
473         // this is a subtrack producer, insert it at correct place
474         id = id.section('_', 1);
475         if (id.endsWith("audio")) {
476             int pos = id.section('_', 0, 0).toInt();
477             if (pos >= m_audioTrackProducers.count()) {
478                 while (m_audioTrackProducers.count() - 1 < pos) {
479                     m_audioTrackProducers.append(NULL);
480                 }
481             }
482             if (m_audioTrackProducers.at(pos) == NULL) {
483                 m_audioTrackProducers[pos] = producer;
484                 updated = true;
485             }
486             return;
487         } else if (id.endsWith("video")) {
488             if (m_videoOnlyProducer == NULL) {
489                 m_videoOnlyProducer = producer;
490                 updated = true;
491             }
492             return;
493         }
494         int pos = id.toInt();
495         if (pos >= m_baseTrackProducers.count()) {
496             while (m_baseTrackProducers.count() - 1 < pos) {
497                 m_baseTrackProducers.append(NULL);
498             }
499         }
500         if (m_baseTrackProducers.at(pos) == NULL) {
501             m_baseTrackProducers[pos] = producer;
502             updated = true;
503         }
504     } else {
505         if (m_baseTrackProducers.isEmpty()) {
506             m_baseTrackProducers.append(producer);
507             updated = true;
508         }
509         else if (m_baseTrackProducers.at(0) == NULL) {
510             m_baseTrackProducers[0] = producer;
511             updated = true;
512         }
513     }
514     if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
515         setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
516     //m_clipProducer = producer;
517     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
518 }
519
520 static double getPixelAspect(QMap<QString, QString>& props) {
521     int width = props.value("frame_size").section('x', 0, 0).toInt();
522     int height = props.value("frame_size").section('x', 1, 1).toInt();
523     int aspectNumerator = props.value("force_aspect_num").toInt();
524     int aspectDenominator = props.value("force_aspect_den").toInt();
525     if (aspectDenominator != 0 && width != 0)
526         return double(height) * aspectNumerator / aspectDenominator / width;    
527     else
528         return 1.0;
529 }
530
531 Mlt::Producer *DocClipBase::audioProducer(int track)
532 {
533     if (m_audioTrackProducers.count() <= track) {
534         while (m_audioTrackProducers.count() - 1 < track) {
535             m_audioTrackProducers.append(NULL);
536         }
537     }
538     if (m_audioTrackProducers.at(track) == NULL) {
539         int i;
540         for (i = 0; i < m_audioTrackProducers.count(); i++)
541             if (m_audioTrackProducers.at(i) != NULL) break;
542         if (i < m_audioTrackProducers.count()) {
543             // Could not find a valid producer for that clip, check in 
544             return m_audioTrackProducers.at(i);
545         }
546             
547         Mlt::Producer *base = producer();
548         m_audioTrackProducers[track] = new Mlt::Producer(*(base->profile()), base->get("resource"));
549         if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
550             m_audioTrackProducers.at(track)->set("force_aspect_ratio", getPixelAspect(m_properties));
551         if (m_properties.contains("force_fps")) m_audioTrackProducers.at(track)->set("force_fps", m_properties.value("force_fps").toDouble());
552         if (m_properties.contains("force_progressive")) m_audioTrackProducers.at(track)->set("force_progressive", m_properties.value("force_progressive").toInt());
553         if (m_properties.contains("force_tff")) m_audioTrackProducers.at(track)->set("force_tff", m_properties.value("force_tff").toInt());
554         if (m_properties.contains("threads")) m_audioTrackProducers.at(track)->set("threads", m_properties.value("threads").toInt());
555         m_audioTrackProducers.at(track)->set("video_index", -1);
556         if (m_properties.contains("audio_index")) m_audioTrackProducers.at(track)->set("audio_index", m_properties.value("audio_index").toInt());
557         m_audioTrackProducers.at(track)->set("id", QString(getId() + '_' + QString::number(track) + "_audio").toUtf8().data());
558         if (m_properties.contains("force_colorspace")) m_audioTrackProducers.at(track)->set("force_colorspace", m_properties.value("force_colorspace").toInt());
559         if (m_properties.contains("full_luma")) m_audioTrackProducers.at(track)->set("set.force_full_luma", m_properties.value("full_luma").toInt());
560     }
561     return m_audioTrackProducers.at(track);
562 }
563
564 Mlt::Producer *DocClipBase::videoProducer()
565 {
566     if (m_videoOnlyProducer == NULL) {
567         int i;
568         for (i = 0; i < m_baseTrackProducers.count(); i++)
569             if (m_baseTrackProducers.at(i) != NULL) break;
570         if (i >= m_baseTrackProducers.count()) return NULL;
571         m_videoOnlyProducer = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
572         if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
573             m_videoOnlyProducer->set("force_aspect_ratio", getPixelAspect(m_properties));
574         if (m_properties.contains("force_fps")) m_videoOnlyProducer->set("force_fps", m_properties.value("force_fps").toDouble());
575         if (m_properties.contains("force_progressive")) m_videoOnlyProducer->set("force_progressive", m_properties.value("force_progressive").toInt());
576         if (m_properties.contains("force_tff")) m_videoOnlyProducer->set("force_tff", m_properties.value("force_tff").toInt());
577         if (m_properties.contains("threads")) m_videoOnlyProducer->set("threads", m_properties.value("threads").toInt());
578         m_videoOnlyProducer->set("audio_index", -1);
579         if (m_properties.contains("video_index")) m_videoOnlyProducer->set("video_index", m_properties.value("video_index").toInt());
580         m_videoOnlyProducer->set("id", QString(getId() + "_video").toUtf8().data());
581         if (m_properties.contains("force_colorspace")) m_videoOnlyProducer->set("force_colorspace", m_properties.value("force_colorspace").toInt());
582         if (m_properties.contains("full_luma")) m_videoOnlyProducer->set("set.force_full_luma", m_properties.value("full_luma").toInt());
583     }
584     return m_videoOnlyProducer;
585 }
586
587 Mlt::Producer *DocClipBase::producer(int track)
588 {
589     /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
590         if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
591     }*/
592     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
593         if (m_baseTrackProducers.count() == 0) return NULL;
594         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
595             if (m_baseTrackProducers.at(i) != NULL)
596                 return m_baseTrackProducers.at(i);
597         }
598         return NULL;
599     }
600     if (track >= m_baseTrackProducers.count()) {
601         while (m_baseTrackProducers.count() - 1 < track) {
602             m_baseTrackProducers.append(NULL);
603         }
604     }
605     if (m_baseTrackProducers.at(track) == NULL) {
606         int i;
607         for (i = 0; i < m_baseTrackProducers.count(); i++)
608             if (m_baseTrackProducers.at(i) != NULL) break;
609
610         if (i >= m_baseTrackProducers.count()) {
611             // Could not find a valid producer for that clip, check in 
612             return NULL;
613         }
614
615         if (KIO::NetAccess::exists(KUrl(m_baseTrackProducers.at(i)->get("resource")), KIO::NetAccess::SourceSide, 0))
616             m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
617         else { // special case for placeholder clips
618             m_baseTrackProducers[track] = NULL;
619             return NULL;
620         }
621         if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
622             m_baseTrackProducers[track]->set("force_aspect_ratio", getPixelAspect(m_properties));
623         if (m_properties.contains("force_fps")) m_baseTrackProducers[track]->set("force_fps", m_properties.value("force_fps").toDouble());
624         if (m_properties.contains("force_progressive")) m_baseTrackProducers[track]->set("force_progressive", m_properties.value("force_progressive").toInt());
625         if (m_properties.contains("force_tff")) m_baseTrackProducers[track]->set("force_tff", m_properties.value("force_tff").toInt());
626         if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
627         if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
628         if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
629         m_baseTrackProducers[track]->set("id", QString(getId() + '_' + QString::number(track)).toUtf8().data());
630
631         if (m_properties.contains("force_colorspace")) m_baseTrackProducers[track]->set("force_colorspace", m_properties.value("force_colorspace").toInt());
632         if (m_properties.contains("full_luma")) m_baseTrackProducers[track]->set("set.force_full_luma", m_properties.value("full_luma").toInt());
633     }
634     return m_baseTrackProducers.at(track);
635 }
636
637 void DocClipBase::setProducerProperty(const char *name, int data)
638 {
639     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
640         if (m_baseTrackProducers.at(i) != NULL)
641             m_baseTrackProducers[i]->set(name, data);
642     }
643 }
644
645 void DocClipBase::setProducerProperty(const char *name, double data)
646 {
647     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
648         if (m_baseTrackProducers.at(i) != NULL)
649             m_baseTrackProducers[i]->set(name, data);
650     }
651 }
652
653 void DocClipBase::setProducerProperty(const char *name, const char *data)
654 {
655     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
656         if (m_baseTrackProducers.at(i) != NULL)
657             m_baseTrackProducers[i]->set(name, data);
658     }
659 }
660
661 void DocClipBase::resetProducerProperty(const char *name)
662 {
663     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
664         if (m_baseTrackProducers.at(i) != NULL)
665             m_baseTrackProducers[i]->set(name, (const char*) NULL);
666     }
667 }
668
669 const char *DocClipBase::producerProperty(const char *name) const
670 {
671     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
672         if (m_baseTrackProducers.at(i) != NULL) {
673             return m_baseTrackProducers.at(i)->get(name);
674         }
675     }
676     return NULL;
677 }
678
679
680 void DocClipBase::slotRefreshProducer()
681 {
682     if (m_baseTrackProducers.count() == 0) return;
683     if (m_clipType == SLIDESHOW) {
684         /*Mlt::Producer producer(*(m_clipProducer->profile()), getProperty("resource").toUtf8().data());
685         delete m_clipProducer;
686         m_clipProducer = new Mlt::Producer(producer.get_producer());
687         if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
688         setProducerProperty("ttl", getProperty("ttl").toInt());
689         //m_clipProducer->set("id", getProperty("id"));
690         if (!getProperty("animation").isEmpty()) {
691             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
692             int ct = 0;
693             Mlt::Filter *filter = clipService.filter(ct);
694             while (filter) {
695                 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
696                     break;
697                 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
698                     clipService.detach(*filter);
699                 } else ct++;
700                 filter = clipService.filter(ct);
701             }
702
703             if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
704                 // filter does not exist, create it.
705                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
706                 if (filter && filter->is_valid()) {
707                     int cycle = getProperty("ttl").toInt();
708                     QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
709                     if (!geometry.isEmpty()) {
710                         if (getProperty("animation").contains("low-pass")) {
711                             Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
712                             if (blur && blur->is_valid())
713                                 clipService.attach(*blur);
714                         }
715                         filter->set("transition.geometry", geometry.toUtf8().data());
716                         filter->set("transition.cycle", cycle);
717                         clipService.attach(*filter);
718                     }
719                 }
720             }
721         } else {
722             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
723             int ct = 0;
724             Mlt::Filter *filter = clipService.filter(0);
725             while (filter) {
726                 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
727                     clipService.detach(*filter);
728                 } else ct++;
729                 filter = clipService.filter(ct);
730             }
731         }
732         if (getProperty("fade") == "1") {
733             // we want a fade filter effect
734             kDebug() << "////////////   FADE WANTED";
735             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
736             int ct = 0;
737             Mlt::Filter *filter = clipService.filter(ct);
738             while (filter) {
739                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
740                     break;
741                 }
742                 ct++;
743                 filter = clipService.filter(ct);
744             }
745
746             if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
747                 filter->set("cycle", getProperty("ttl").toInt());
748                 filter->set("duration", getProperty("luma_duration").toInt());
749                 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
750                 if (!getProperty("softness").isEmpty()) {
751                     int soft = getProperty("softness").toInt();
752                     filter->set("luma.softness", (double) soft / 100.0);
753                 }
754             } else {
755                 // filter does not exist, create it...
756                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
757                 filter->set("cycle", getProperty("ttl").toInt());
758                 filter->set("duration", getProperty("luma_duration").toInt());
759                 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
760                 if (!getProperty("softness").isEmpty()) {
761                     int soft = getProperty("softness").toInt();
762                     filter->set("luma.softness", (double) soft / 100.0);
763                 }
764                 clipService.attach(*filter);
765             }
766         } else {
767             kDebug() << "////////////   FADE NOT WANTED!!!";
768             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
769             int ct = 0;
770             Mlt::Filter *filter = clipService.filter(0);
771             while (filter) {
772                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
773                     clipService.detach(*filter);
774                 } else ct++;
775                 filter = clipService.filter(ct);
776             }
777         }
778         if (getProperty("crop") == "1") {
779             // we want a center crop filter effect
780             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
781             int ct = 0;
782             Mlt::Filter *filter = clipService.filter(ct);
783             while (filter) {
784                 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
785                     break;
786                 }
787                 ct++;
788                 filter = clipService.filter(ct);
789             }
790
791             if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
792                 // filter does not exist, create it...
793                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
794                 filter->set("center", 1);
795                 clipService.attach(*filter);
796             }
797         } else {
798             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
799             int ct = 0;
800             Mlt::Filter *filter = clipService.filter(0);
801             while (filter) {
802                 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
803                     clipService.detach(*filter);
804                 } else ct++;
805                 filter = clipService.filter(ct);
806             }
807         }
808     }
809 }
810
811 void DocClipBase::setProperties(QMap <QString, QString> properties)
812 {
813     // changing clip type is not allowed
814     properties.remove("type");
815     QMapIterator<QString, QString> i(properties);
816     bool refreshProducer = false;
817     QStringList keys;
818     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
819     while (i.hasNext()) {
820         i.next();
821         setProperty(i.key(), i.value());
822         if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
823     }
824     if (refreshProducer) slotRefreshProducer();
825 }
826
827 void DocClipBase::setMetadata(QMap <QString, QString> properties)
828 {
829     QMapIterator<QString, QString> i(properties);
830     while (i.hasNext()) {
831         i.next();
832         if (i.value().isEmpty() && m_metadata.contains(i.key())) {
833             m_metadata.remove(i.key());
834         } else {
835             m_metadata.insert(i.key(), i.value());
836         }
837     }
838 }
839
840 QMap <QString, QString> DocClipBase::metadata() const
841 {
842     return m_metadata;
843 }
844
845 void DocClipBase::clearProperty(const QString &key)
846 {
847     m_properties.remove(key);
848 }
849
850 void DocClipBase::getFileHash(const QString url)
851 {
852     if (m_clipType == SLIDESHOW) return;
853     QFile file(url);
854     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
855         QByteArray fileData;
856         QByteArray fileHash;
857         //kDebug() << "SETTING HASH of" << value;
858         m_properties.insert("file_size", QString::number(file.size()));
859         /*
860                * 1 MB = 1 second per 450 files (or faster)
861                * 10 MB = 9 seconds per 450 files (or faster)
862                */
863         if (file.size() > 1000000*2) {
864             fileData = file.read(1000000);
865             if (file.seek(file.size() - 1000000))
866                 fileData.append(file.readAll());
867         } else
868             fileData = file.readAll();
869         file.close();
870         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
871         m_properties.insert("file_hash", QString(fileHash.toHex()));
872     }
873 }
874
875 bool DocClipBase::checkHash() const
876 {
877     KUrl url = fileURL();
878     if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
879     return true;
880 }
881
882 QString DocClipBase::getClipHash() const
883 {
884     QString hash;
885     if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
886     else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
887     else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
888     else {
889         if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
890         else hash = getHash(fileURL().path());
891         
892     }
893     return hash;
894 }
895
896 void DocClipBase::setPlaceHolder(bool place)
897 {
898     m_placeHolder = place;
899 }
900
901 // static
902 QString DocClipBase::getHash(const QString &path)
903 {
904     QFile file(path);
905     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
906         QByteArray fileData;
907         QByteArray fileHash;
908         /*
909                * 1 MB = 1 second per 450 files (or faster)
910                * 10 MB = 9 seconds per 450 files (or faster)
911                */
912         if (file.size() > 1000000*2) {
913             fileData = file.read(1000000);
914             if (file.seek(file.size() - 1000000))
915                 fileData.append(file.readAll());
916         } else
917             fileData = file.readAll();
918         file.close();
919         return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
920     }
921     return QString();
922 }
923
924 void DocClipBase::refreshThumbUrl()
925 {
926     if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
927 }
928
929 void DocClipBase::setProperty(const QString &key, const QString &value)
930 {
931     m_properties.insert(key, value);
932     if (key == "resource") {
933         getFileHash(value);
934         if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
935     } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
936     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
937     else if (key == "colour") {
938         setProducerProperty("colour", value.toUtf8().data());
939     } else if (key == "templatetext") {
940         setProducerProperty("templatetext", value.toUtf8().data());
941         setProducerProperty("force_reload", 1);
942     } else if (key == "xmldata") {
943         setProducerProperty("xmldata", value.toUtf8().data());
944         setProducerProperty("force_reload", 1);
945     } else if (key == "force_aspect_num") {
946         if (value.isEmpty()) {
947             m_properties.remove("force_aspect_num");
948             resetProducerProperty("force_aspect_ratio");
949         } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
950     } else if (key == "force_aspect_den") {
951         if (value.isEmpty()) {
952             m_properties.remove("force_aspect_den");
953             resetProducerProperty("force_aspect_ratio");
954         } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
955     } else if (key == "force_fps") {
956         if (value.isEmpty()) {
957             m_properties.remove("force_fps");
958             resetProducerProperty("force_fps");
959         } else setProducerProperty("force_fps", value.toDouble());
960     } else if (key == "force_progressive") {
961         if (value.isEmpty()) {
962             m_properties.remove("force_progressive");
963             resetProducerProperty("force_progressive");
964         } else setProducerProperty("force_progressive", value.toInt());
965     } else if (key == "force_tff") {
966         if (value.isEmpty()) {
967             m_properties.remove("force_tff");
968             resetProducerProperty("force_tff");
969         } else setProducerProperty("force_tff", value.toInt());
970     } else if (key == "threads") {
971         if (value.isEmpty()) {
972             m_properties.remove("threads");
973             setProducerProperty("threads", 1);
974         } else setProducerProperty("threads", value.toInt());
975     } else if (key == "video_index") {
976         if (value.isEmpty()) {
977             m_properties.remove("video_index");
978             setProducerProperty("video_index", m_properties.value("default_video").toInt());
979         } else setProducerProperty("video_index", value.toInt());
980     } else if (key == "audio_index") {
981         if (value.isEmpty()) {
982             m_properties.remove("audio_index");
983             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
984         } else setProducerProperty("audio_index", value.toInt());
985     } else if (key == "force_colorspace") {
986         if (value.isEmpty()) {
987             m_properties.remove("force_colorspace");
988             resetProducerProperty("force_colorspace");
989         } else setProducerProperty("force_colorspace", value.toInt());
990     } else if (key == "full_luma") {
991         if (value.isEmpty()) {
992             m_properties.remove("full_luma");
993             resetProducerProperty("set.force_full_luma");
994         } else setProducerProperty("set.force_full_luma", value.toInt());
995     }
996     else if (key == "proxy") {
997         // If value is "-", that means user manually disabled proxy on this clip
998         if (value.isEmpty() || value == "-") {
999             // reset proxy
1000             emit abortProxy(m_id);
1001         }
1002         else {
1003             emit createProxy(m_id);
1004         }
1005     }
1006 }
1007
1008 QMap <QString, QString> DocClipBase::properties() const
1009 {
1010     return m_properties;
1011 }
1012
1013 bool DocClipBase::slotGetAudioThumbs()
1014 {
1015     if (m_thumbProd == NULL || isPlaceHolder()) return false;
1016     if (!KdenliveSettings::audiothumbnails() || m_audioTimer == NULL) {
1017         if (m_audioTimer != NULL) m_audioTimer->stop();
1018         return false;
1019     }
1020     if (m_audioThumbCreated) {
1021         m_audioTimer->stop();
1022         return false;
1023     }
1024     m_audioTimer->start(1500);
1025     double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
1026     m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
1027     return true;
1028 }
1029
1030 bool DocClipBase::isPlaceHolder() const
1031 {
1032     return m_placeHolder;
1033 }
1034
1035 void DocClipBase::addCutZone(int in, int out, QString desc)
1036 {
1037     CutZoneInfo info;
1038     info.zone = QPoint(in, out);
1039     info.description = desc;
1040     for (int i = 0; i < m_cutZones.count(); i++)
1041         if (m_cutZones.at(i).zone == info.zone) {
1042             return;
1043         }
1044     m_cutZones.append(info);
1045 }
1046
1047 bool DocClipBase::hasCutZone(QPoint p) const
1048 {
1049     for (int i = 0; i < m_cutZones.count(); i++)
1050         if (m_cutZones.at(i).zone == p) return true;
1051     return false;
1052 }
1053
1054
1055 void DocClipBase::removeCutZone(int in, int out)
1056 {
1057     QPoint p(in, out);
1058     for (int i = 0; i < m_cutZones.count(); i++) {
1059         if (m_cutZones.at(i).zone == p) {
1060             m_cutZones.removeAt(i);
1061             i--;
1062         }
1063     }
1064 }
1065
1066 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1067 {
1068     QPoint old(oldin, oldout);
1069     for (int i = 0; i < m_cutZones.size(); ++i) {
1070         if (m_cutZones.at(i).zone == old) {
1071             CutZoneInfo info;
1072             info.zone = QPoint(in, out);
1073             info.description = desc;
1074             m_cutZones.replace(i, info);
1075             break;
1076         }
1077     }
1078 }
1079
1080 QList <CutZoneInfo> DocClipBase::cutZones() const
1081 {
1082     return m_cutZones;
1083 }
1084
1085 bool DocClipBase::hasVideoCodec(const QString &codec) const
1086 {
1087     Mlt::Producer *prod = NULL;
1088     if (m_baseTrackProducers.count() == 0) return false;
1089     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1090         if (m_baseTrackProducers.at(i) != NULL) {
1091             prod = m_baseTrackProducers.at(i);
1092             break;
1093         }
1094     }
1095
1096     if (!prod) return false;
1097     int default_video = prod->get_int("video_index");
1098     char property[200];
1099     snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1100     return prod->get(property) == codec;
1101 }
1102
1103 bool DocClipBase::hasAudioCodec(const QString &codec) const
1104 {
1105     Mlt::Producer *prod = NULL;
1106     if (m_baseTrackProducers.count() == 0) return false;
1107     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1108         if (m_baseTrackProducers.at(i) != NULL) {
1109             prod = m_baseTrackProducers.at(i);
1110             break;
1111         }
1112     }
1113     if (!prod) return false;
1114     int default_video = prod->get_int("audio_index");
1115     char property[200];
1116     snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1117     return prod->get(property) == codec;
1118 }
1119
1120
1121