]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
Fix audio thumbs not displayed on project opening and abortion if 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
32 #include <KIO/NetAccess>
33 #include <KDebug>
34
35 #include <QCryptographicHash>
36
37 #include <cstdio>
38
39 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id) :
40         QObject(),
41         m_audioFrameCache(),
42         m_refcount(0),
43         m_baseTrackProducers(),
44         m_audioTrackProducers(),
45         m_videoOnlyProducer(NULL),
46         m_snapMarkers(QList < CommentedTime >()),
47         m_duration(),
48         m_audioTimer(NULL),
49         m_thumbProd(NULL),
50         m_audioThumbCreated(false),
51         m_id(id),
52         m_placeHolder(xml.hasAttribute("placeholder")),
53         m_properties()
54 {
55     int type = xml.attribute("type").toInt();
56     m_clipType = (CLIPTYPE) type;
57     if (m_placeHolder) xml.removeAttribute("placeholder");
58     QDomNamedNodeMap attributes = xml.attributes();
59     for (int i = 0; i < attributes.count(); i++) {
60         m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
61     }
62
63     if (xml.hasAttribute("cutzones")) {
64         QStringList cuts = xml.attribute("cutzones").split(";", QString::SkipEmptyParts);
65         for (int i = 0; i < cuts.count(); i++) {
66             QString z = cuts.at(i);
67             addCutZone(z.section('-', 0, 0).toInt(), z.section('-', 1, 1).toInt(), z.section('-', 2, 2));
68         }
69     }
70
71     KUrl url = KUrl(xml.attribute("resource"));
72     if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
73
74     if (xml.hasAttribute("duration")) {
75         setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
76     } else {
77         int out = xml.attribute("out").toInt();
78         int in = xml.attribute("in").toInt();
79         setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
80     }
81
82     if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
83
84     //if (!url.isEmpty() && QFile::exists(url.path()))
85     {
86         m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
87         if (m_clipType == AV || m_clipType == AUDIO || m_clipType == PLAYLIST) slotCreateAudioTimer();
88     }
89     //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
90 }
91
92 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
93     DocClipBase::operator=(clip);
94     m_id = clip.getId();
95     m_clipType = clip.clipType();
96     m_name = clip.name();
97     m_duration = clip.duration();
98     m_audioThumbCreated = clip.audioThumbCreated();
99     m_properties = clip.properties();
100     return *this;
101 }*/
102
103 DocClipBase::~DocClipBase()
104 {
105     kDebug() << "CLIP " << m_id << " DELETED******************************";
106     delete m_thumbProd;
107     if (m_audioTimer) {
108         m_audioTimer->stop();
109         delete m_audioTimer;
110     }
111     /*kDebug() <<" * * *CNT "<<m_baseTrackProducers.count();
112     if (m_baseTrackProducers.count() > 0) kDebug()<<"YOYO: "<<m_baseTrackProducers.at(0)->get_out()<<", CUT: "<<m_baseTrackProducers.at(0)->is_cut();*/
113     qDeleteAll(m_baseTrackProducers);
114     m_baseTrackProducers.clear();
115     qDeleteAll(m_audioTrackProducers);
116     m_audioTrackProducers.clear();
117     delete m_videoOnlyProducer;
118     m_videoOnlyProducer = NULL;
119 }
120
121 void DocClipBase::setZone(QPoint zone)
122 {
123     m_properties.insert("zone_in", QString::number(zone.x()));
124     m_properties.insert("zone_out", QString::number(zone.y()));
125 }
126
127 QPoint DocClipBase::zone() const
128 {
129     QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
130     return zone;
131 }
132
133 void DocClipBase::slotCreateAudioTimer()
134 {
135     connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
136     m_audioTimer = new QTimer(this);
137     connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
138 }
139
140 void DocClipBase::askForAudioThumbs()
141 {
142     if (m_thumbProd && m_audioTimer) m_thumbProd->askForAudioThumbs(getId());
143 }
144
145 void DocClipBase::slotClearAudioCache()
146 {
147     if (m_thumbProd) m_thumbProd->stopAudioThumbs();
148     if (m_audioTimer != NULL) m_audioTimer->stop();
149     m_audioFrameCache.clear();
150     m_audioThumbCreated = false;
151 }
152
153 /*void DocClipBase::getClipMainThumb() {
154     if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
155 }*/
156
157 KThumb *DocClipBase::thumbProducer()
158 {
159     return m_thumbProd;
160 }
161
162 bool DocClipBase::audioThumbCreated() const
163 {
164     return m_audioThumbCreated;
165 }
166
167 const QString DocClipBase::name() const
168 {
169
170     return m_properties.value("name");
171 }
172
173 const QString &DocClipBase::getId() const
174 {
175     return m_id;
176 }
177
178 void DocClipBase::setId(const QString &newId)
179 {
180     m_id = newId;
181 }
182
183 const CLIPTYPE & DocClipBase::clipType() const
184 {
185     return m_clipType;
186 }
187
188 void DocClipBase::setClipType(CLIPTYPE type)
189 {
190     m_clipType = type;
191
192     m_properties.insert("type", QString::number((int) type));
193     if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO || m_clipType == PLAYLIST))
194         slotCreateAudioTimer();
195 }
196
197 KUrl DocClipBase::fileURL() const
198 {
199     QString res = m_properties.value("resource");
200     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
201     return KUrl();
202 }
203
204 void DocClipBase::setClipThumbFrame(const uint &ix)
205 {
206     m_properties.insert("thumbnail", QString::number((int) ix));
207 }
208
209 uint DocClipBase::getClipThumbFrame() const
210 {
211     return (uint) m_properties.value("thumbnail").toInt();
212 }
213
214 const QString DocClipBase::description() const
215 {
216     return m_properties.value("description");
217 }
218
219 bool DocClipBase::isTransparent() const
220 {
221     return (m_properties.value("transparency") == "1");
222 }
223
224 const QString DocClipBase::getProperty(const QString prop) const
225 {
226     return m_properties.value(prop);
227 }
228
229 void DocClipBase::setDuration(GenTime dur)
230 {
231     m_duration = dur;
232     m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
233 }
234
235 const GenTime &DocClipBase::duration() const
236 {
237     return m_duration;
238 }
239
240 const GenTime DocClipBase::maxDuration() const
241 {
242     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
243         /*const GenTime dur(15000, KdenliveSettings::project_fps());
244         return dur;*/
245         return GenTime();
246     }
247     return m_duration;
248 }
249
250 bool DocClipBase::hasFileSize() const
251 {
252     return true;
253 }
254
255 qulonglong DocClipBase::fileSize() const
256 {
257     return m_properties.value("file_size").toULongLong();
258 }
259
260 // virtual
261 QDomElement DocClipBase::toXML() const
262 {
263     QDomDocument doc;
264     QDomElement clip = doc.createElement("producer");
265
266     QMapIterator<QString, QString> i(m_properties);
267     while (i.hasNext()) {
268         i.next();
269         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
270     }
271     doc.appendChild(clip);
272     if (!m_cutZones.isEmpty()) {
273         QStringList cuts;
274         for (int i = 0; i < m_cutZones.size(); i++) {
275             CutZoneInfo info = m_cutZones.at(i);
276             cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
277         }
278         clip.setAttribute("cutzones", cuts.join(";"));
279     }
280     //kDebug() << "/// CLIP XML: " << doc.toString();
281     return doc.documentElement();
282 }
283
284
285 void DocClipBase::setAudioThumbCreated(bool isDone)
286 {
287     m_audioThumbCreated = isDone;
288 }
289
290
291 void DocClipBase::setThumbnail(const QPixmap & pixmap)
292 {
293     m_thumbnail = pixmap;
294 }
295
296 const QPixmap & DocClipBase::thumbnail() const
297 {
298     return m_thumbnail;
299 }
300
301 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data)
302 {
303     //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
304     m_audioFrameCache = data;
305     m_audioThumbCreated = true;
306     emit gotAudioData();
307 }
308
309 QList < GenTime > DocClipBase::snapMarkers() const
310 {
311     QList < GenTime > markers;
312
313     for (int count = 0; count < m_snapMarkers.count(); ++count) {
314         markers.append(m_snapMarkers.at(count).time());
315     }
316
317     return markers;
318 }
319
320 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
321 {
322     return m_snapMarkers;
323 }
324
325
326 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
327 {
328     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
329     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
330         if ((*it).time() >= time)
331             break;
332     }
333
334     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
335         (*it).setComment(comment);
336         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
337     } else {
338         CommentedTime t(time, comment);
339         m_snapMarkers.insert(it, t);
340     }
341
342 }
343
344 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
345 {
346     QList < CommentedTime >::Iterator it;
347     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
348         if ((*it).time() == time)
349             break;
350     }
351     if (it != m_snapMarkers.end()) {
352         (*it).setComment(comment);
353     } else {
354         kError() << "trying to edit Snap Marker that does not already exists";
355     }
356 }
357
358 QString DocClipBase::deleteSnapMarker(const GenTime & time)
359 {
360     QString result = i18n("Marker");
361     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
362
363     while (itt != m_snapMarkers.end()) {
364         if ((*itt).time() == time)
365             break;
366         ++itt;
367     }
368
369     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
370         result = (*itt).comment();
371         m_snapMarkers.erase(itt);
372     }
373     return result;
374 }
375
376
377 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
378 {
379     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
380
381     while (itt != m_snapMarkers.end()) {
382         if ((*itt).time() == time)
383             return time;
384         ++itt;
385     }
386
387     return GenTime(0.0);
388 }
389
390 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
391 {
392     int it;
393     for (it = 0; it < m_snapMarkers.count(); it++) {
394         if (m_snapMarkers.at(it).time() >= currTime)
395             break;
396     }
397     if (it == 0) return GenTime();
398     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
399         return m_snapMarkers.at(it).time();
400     else return m_snapMarkers.at(it - 1).time();
401 }
402
403 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
404 {
405     int it;
406     for (it = 0; it < m_snapMarkers.count(); it++) {
407         if (m_snapMarkers.at(it).time() > currTime)
408             break;
409     }
410     if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
411     return duration();
412 }
413
414 QString DocClipBase::markerComment(GenTime t)
415 {
416     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
417
418     while (itt != m_snapMarkers.end()) {
419         if ((*itt).time() == t)
420             return (*itt).comment();
421         ++itt;
422     }
423     return QString();
424 }
425
426 void DocClipBase::clearProducers()
427 {
428     m_baseTrackProducers.clear();
429 }
430
431 void DocClipBase::deleteProducers(bool clearThumbCreator)
432 {
433     kDebug() << "// CLIP KILL PRODS ct: " << m_baseTrackProducers.count();
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)
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     if (id.contains('_')) {
472         // this is a subtrack producer, insert it at correct place
473         id = id.section('_', 1);
474         if (id.endsWith("audio")) {
475             int pos = id.section('_', 0, 0).toInt();
476             if (pos >= m_audioTrackProducers.count()) {
477                 while (m_audioTrackProducers.count() - 1 < pos) {
478                     m_audioTrackProducers.append(NULL);
479                 }
480             }
481             if (m_audioTrackProducers.at(pos) == NULL) m_audioTrackProducers[pos] = producer;
482             return;
483         } else if (id.endsWith("video")) {
484             m_videoOnlyProducer = producer;
485             return;
486         }
487         int pos = id.toInt();
488         if (pos >= m_baseTrackProducers.count()) {
489             while (m_baseTrackProducers.count() - 1 < pos) {
490                 m_baseTrackProducers.append(NULL);
491             }
492         }
493         if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
494     } else {
495         if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
496         else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
497     }
498     //m_clipProducer = producer;
499     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
500 }
501
502 Mlt::Producer *DocClipBase::audioProducer(int track)
503 {
504     if (m_audioTrackProducers.count() <= track) {
505         while (m_audioTrackProducers.count() - 1 < track) {
506             m_audioTrackProducers.append(NULL);
507         }
508     }
509     if (m_audioTrackProducers.at(track) == NULL) {
510         Mlt::Producer *base = producer();
511         m_audioTrackProducers[track] = new Mlt::Producer(*(base->profile()), base->get("resource"));
512         if (m_properties.contains("force_aspect_ratio")) m_audioTrackProducers.at(track)->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
513         if (m_properties.contains("force_fps")) m_audioTrackProducers.at(track)->set("force_fps", m_properties.value("force_fps").toDouble());
514         if (m_properties.contains("force_progressive")) m_audioTrackProducers.at(track)->set("force_progressive", m_properties.value("force_progressive").toInt());
515         if (m_properties.contains("threads")) m_audioTrackProducers.at(track)->set("threads", m_properties.value("threads").toInt());
516         m_audioTrackProducers.at(track)->set("video_index", -1);
517         if (m_properties.contains("audio_index")) m_audioTrackProducers.at(track)->set("audio_index", m_properties.value("audio_index").toInt());
518         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track) + "_audio").toUtf8().data());
519         m_audioTrackProducers.at(track)->set("id", tmp);
520         delete[] tmp;
521     }
522     return m_audioTrackProducers.at(track);
523 }
524
525 Mlt::Producer *DocClipBase::videoProducer()
526 {
527     if (m_videoOnlyProducer == NULL) {
528         int i;
529         for (i = 0; i < m_baseTrackProducers.count(); i++)
530             if (m_baseTrackProducers.at(i) != NULL) break;
531         if (i >= m_baseTrackProducers.count()) return NULL;
532         m_videoOnlyProducer = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
533         if (m_properties.contains("force_aspect_ratio")) m_videoOnlyProducer->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
534         if (m_properties.contains("force_fps")) m_videoOnlyProducer->set("force_fps", m_properties.value("force_fps").toDouble());
535         if (m_properties.contains("force_progressive")) m_videoOnlyProducer->set("force_progressive", m_properties.value("force_progressive").toInt());
536         if (m_properties.contains("threads")) m_videoOnlyProducer->set("threads", m_properties.value("threads").toInt());
537         m_videoOnlyProducer->set("audio_index", -1);
538         if (m_properties.contains("video_index")) m_videoOnlyProducer->set("video_index", m_properties.value("video_index").toInt());
539         char *tmp = (char *) qstrdup(QString(getId() + "_video").toUtf8().data());
540         m_videoOnlyProducer->set("id", tmp);
541         delete[] tmp;
542     }
543     return m_videoOnlyProducer;
544 }
545
546 Mlt::Producer *DocClipBase::producer(int track)
547 {
548     /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
549         if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
550     }*/
551     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
552         if (m_baseTrackProducers.count() == 0) return NULL;
553         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
554             if (m_baseTrackProducers.at(i) != NULL)
555                 return m_baseTrackProducers.at(i);
556         }
557         return NULL;
558     }
559     if (track >= m_baseTrackProducers.count()) {
560         while (m_baseTrackProducers.count() - 1 < track) {
561             m_baseTrackProducers.append(NULL);
562         }
563     }
564     if (m_baseTrackProducers.at(track) == NULL) {
565         int i;
566         for (i = 0; i < m_baseTrackProducers.count(); i++)
567             if (m_baseTrackProducers.at(i) != NULL) break;
568
569         if (i >= m_baseTrackProducers.count()) return NULL;
570
571         if (KIO::NetAccess::exists(KUrl(m_baseTrackProducers.at(i)->get("resource")), KIO::NetAccess::SourceSide, 0))
572             m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
573         else { // special case for placeholder clips
574             m_baseTrackProducers[track] = NULL;
575             return NULL;
576         }
577
578         if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
579         if (m_properties.contains("force_fps")) m_baseTrackProducers[track]->set("force_fps", m_properties.value("force_fps").toDouble());
580         if (m_properties.contains("force_progressive")) m_baseTrackProducers[track]->set("force_progressive", m_properties.value("force_progressive").toInt());
581         if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
582         if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
583         if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
584         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
585         m_baseTrackProducers[track]->set("id", tmp);
586         delete[] tmp;
587         if (KdenliveSettings::dropbframes() && m_baseTrackProducers.at(i)->get("skip_loop_filter") && strcmp(m_baseTrackProducers.at(i)->get("skip_loop_filter"), "all") == 0) {
588             m_baseTrackProducers[track]->set("skip_loop_filter", "all");
589             m_baseTrackProducers[track]->set("skip_frame", "bidir");
590         }
591     }
592     return m_baseTrackProducers.at(track);
593 }
594
595 void DocClipBase::setProducerProperty(const char *name, int data)
596 {
597     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
598         if (m_baseTrackProducers.at(i) != NULL)
599             m_baseTrackProducers[i]->set(name, data);
600     }
601 }
602
603 void DocClipBase::setProducerProperty(const char *name, double data)
604 {
605     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
606         if (m_baseTrackProducers.at(i) != NULL)
607             m_baseTrackProducers[i]->set(name, data);
608     }
609 }
610
611 void DocClipBase::setProducerProperty(const char *name, const char *data)
612 {
613     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
614         if (m_baseTrackProducers.at(i) != NULL)
615             m_baseTrackProducers[i]->set(name, data);
616     }
617 }
618
619 void DocClipBase::resetProducerProperty(const char *name)
620 {
621     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
622         if (m_baseTrackProducers.at(i) != NULL)
623             m_baseTrackProducers[i]->set(name, (const char*) NULL);
624     }
625 }
626
627 const char *DocClipBase::producerProperty(const char *name) const
628 {
629     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
630         if (m_baseTrackProducers.at(i) != NULL) {
631             return m_baseTrackProducers.at(i)->get(name);
632         }
633     }
634     return NULL;
635 }
636
637
638 void DocClipBase::slotRefreshProducer()
639 {
640     if (m_baseTrackProducers.count() == 0) return;
641     kDebug() << "////////////   REFRESH CLIP !!!!!!!!!!!!!!!!";
642     if (m_clipType == SLIDESHOW) {
643         /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
644                Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
645                delete[] tmp;
646         delete m_clipProducer;
647         m_clipProducer = new Mlt::Producer(producer.get_producer());
648         if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
649         setProducerProperty("ttl", getProperty("ttl").toInt());
650         //m_clipProducer->set("id", getProperty("id"));
651         if (getProperty("fade") == "1") {
652             // we want a fade filter effect
653             kDebug() << "////////////   FADE WANTED";
654             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
655             int ct = 0;
656             Mlt::Filter *filter = clipService.filter(ct);
657             while (filter) {
658                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
659                     break;
660                 }
661                 ct++;
662                 filter = clipService.filter(ct);
663             }
664
665             if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
666                 filter->set("period", getProperty("ttl").toInt() - 1);
667                 filter->set("luma.out", getProperty("luma_duration").toInt());
668                 QString resource = getProperty("luma_file");
669                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
670                 filter->set("luma.resource", tmp);
671                 delete[] tmp;
672                 if (!getProperty("softness").isEmpty()) {
673                     int soft = getProperty("softness").toInt();
674                     filter->set("luma.softness", (double) soft / 100.0);
675                 }
676             } else {
677                 // filter does not exist, create it...
678                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
679                 filter->set("period", getProperty("ttl").toInt() - 1);
680                 filter->set("luma.out", getProperty("luma_duration").toInt());
681                 QString resource = getProperty("luma_file");
682                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
683                 filter->set("luma.resource", tmp);
684                 delete[] tmp;
685                 if (!getProperty("softness").isEmpty()) {
686                     int soft = getProperty("softness").toInt();
687                     filter->set("luma.softness", (double) soft / 100.0);
688                 }
689                 clipService.attach(*filter);
690             }
691         } else {
692             kDebug() << "////////////   FADE NOT WANTED!!!";
693             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
694             int ct = 0;
695             Mlt::Filter *filter = clipService.filter(0);
696             while (filter) {
697                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
698                     clipService.detach(*filter);
699                 } else ct++;
700                 filter = clipService.filter(ct);
701             }
702         }
703     }
704 }
705
706 void DocClipBase::setProperties(QMap <QString, QString> properties)
707 {
708     // changing clip type is not allowed
709     properties.remove("type");
710     QMapIterator<QString, QString> i(properties);
711     bool refreshProducer = false;
712     QStringList keys;
713     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
714     while (i.hasNext()) {
715         i.next();
716         setProperty(i.key(), i.value());
717         if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
718     }
719     if (refreshProducer) slotRefreshProducer();
720 }
721
722 void DocClipBase::setMetadata(QMap <QString, QString> properties)
723 {
724     m_metadata = properties;
725 }
726
727 QMap <QString, QString> DocClipBase::metadata() const
728 {
729     return m_metadata;
730 }
731
732 void DocClipBase::clearProperty(const QString &key)
733 {
734     m_properties.remove(key);
735 }
736
737 void DocClipBase::getFileHash(const QString url)
738 {
739     if (m_clipType == SLIDESHOW) return;
740     QFile file(url);
741     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
742         QByteArray fileData;
743         QByteArray fileHash;
744         //kDebug() << "SETTING HASH of" << value;
745         m_properties.insert("file_size", QString::number(file.size()));
746         /*
747                * 1 MB = 1 second per 450 files (or faster)
748                * 10 MB = 9 seconds per 450 files (or faster)
749                */
750         if (file.size() > 1000000*2) {
751             fileData = file.read(1000000);
752             if (file.seek(file.size() - 1000000))
753                 fileData.append(file.readAll());
754         } else
755             fileData = file.readAll();
756         file.close();
757         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
758         m_properties.insert("file_hash", QString(fileHash.toHex()));
759     }
760 }
761
762 bool DocClipBase::checkHash() const
763 {
764     KUrl url = fileURL();
765     if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
766     return true;
767 }
768
769 QString DocClipBase::getClipHash() const
770 {
771     QString hash;
772     if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
773     else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
774     else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
775     else hash = m_properties.value("file_hash");
776     return hash;
777 }
778
779 void DocClipBase::setPlaceHolder(bool place)
780 {
781     m_placeHolder = place;
782 }
783
784 // static
785 QString DocClipBase::getHash(const QString &path)
786 {
787     QFile file(path);
788     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
789         QByteArray fileData;
790         QByteArray fileHash;
791         /*
792                * 1 MB = 1 second per 450 files (or faster)
793                * 10 MB = 9 seconds per 450 files (or faster)
794                */
795         if (file.size() > 1000000*2) {
796             fileData = file.read(1000000);
797             if (file.seek(file.size() - 1000000))
798                 fileData.append(file.readAll());
799         } else
800             fileData = file.readAll();
801         file.close();
802         return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
803     }
804     return QString();
805 }
806
807 void DocClipBase::refreshThumbUrl()
808 {
809     if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
810 }
811
812 void DocClipBase::setProperty(const QString &key, const QString &value)
813 {
814     m_properties.insert(key, value);
815     if (key == "resource") {
816         getFileHash(value);
817         if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
818     } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
819     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
820     else if (key == "colour") {
821         char *tmp = (char *) qstrdup(value.toUtf8().data());
822         setProducerProperty("colour", tmp);
823         delete[] tmp;
824     } else if (key == "templatetext") {
825         char *tmp = (char *) qstrdup(value.toUtf8().data());
826         setProducerProperty("templatetext", tmp);
827         delete[] tmp;
828         setProducerProperty("force_reload", 1);
829     } else if (key == "xmldata") {
830         char *tmp = (char *) qstrdup(value.toUtf8().data());
831         setProducerProperty("xmldata", tmp);
832         delete[] tmp;
833         setProducerProperty("force_reload", 1);
834     } else if (key == "force_aspect_ratio") {
835         if (value.isEmpty()) {
836             m_properties.remove("force_aspect_ratio");
837             resetProducerProperty("force_aspect_ratio");
838         } else setProducerProperty("force_aspect_ratio", value.toDouble());
839     } else if (key == "force_fps") {
840         if (value.isEmpty()) {
841             m_properties.remove("force_fps");
842             resetProducerProperty("force_fps");
843         } else setProducerProperty("force_fps", value.toDouble());
844     } else if (key == "force_progressive") {
845         if (value.isEmpty()) {
846             m_properties.remove("force_progressive");
847             resetProducerProperty("force_progressive");
848         } else setProducerProperty("force_progressive", value.toInt());
849     } else if (key == "threads") {
850         if (value.isEmpty()) {
851             m_properties.remove("threads");
852             setProducerProperty("threads", 1);
853         } else setProducerProperty("threads", value.toInt());
854     } else if (key == "video_index") {
855         if (value.isEmpty()) {
856             m_properties.remove("video_index");
857             setProducerProperty("video_index", m_properties.value("default_video").toInt());
858         } else setProducerProperty("video_index", value.toInt());
859     } else if (key == "audio_index") {
860         if (value.isEmpty()) {
861             m_properties.remove("audio_index");
862             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
863         } else setProducerProperty("audio_index", value.toInt());
864     }
865 }
866
867 QMap <QString, QString> DocClipBase::properties() const
868 {
869     return m_properties;
870 }
871
872 bool DocClipBase::slotGetAudioThumbs()
873 {
874     if (m_thumbProd == NULL) return false;
875     if (!KdenliveSettings::audiothumbnails() || m_audioTimer == NULL) {
876         if (m_audioTimer != NULL) m_audioTimer->stop();
877         return false;
878     }
879     if (m_audioThumbCreated) {
880         m_audioTimer->stop();
881         return false;
882     }
883     m_audioTimer->start(1500);
884     double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
885     m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
886     return true;
887 }
888
889 bool DocClipBase::isPlaceHolder() const
890 {
891     return m_placeHolder;
892 }
893
894 void DocClipBase::addCutZone(int in, int out, QString desc)
895 {
896     CutZoneInfo info;
897     info.zone = QPoint(in, out);
898     info.description = desc;
899     for (int i = 0; i < m_cutZones.count(); i++)
900         if (m_cutZones.at(i).zone == info.zone) {
901             return;
902         }
903     m_cutZones.append(info);
904 }
905
906 bool DocClipBase::hasCutZone(QPoint p) const
907 {
908     for (int i = 0; i < m_cutZones.count(); i++)
909         if (m_cutZones.at(i).zone == p) return true;
910     return false;
911 }
912
913
914 void DocClipBase::removeCutZone(int in, int out)
915 {
916     QPoint p(in, out);
917     for (int i = 0; i < m_cutZones.count(); i++) {
918         if (m_cutZones.at(i).zone == p) {
919             m_cutZones.removeAt(i);
920             i--;
921         }
922     }
923 }
924
925 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
926 {
927     QPoint old(oldin, oldout);
928     for (int i = 0; i < m_cutZones.size(); ++i) {
929         if (m_cutZones.at(i).zone == old) {
930             CutZoneInfo info;
931             info.zone = QPoint(in, out);
932             info.description = desc;
933             m_cutZones.replace(i, info);
934             break;
935         }
936     }
937 }
938
939 QList <CutZoneInfo> DocClipBase::cutZones() const
940 {
941     return m_cutZones;
942 }
943
944 bool DocClipBase::hasVideoCodec(const QString &codec) const
945 {
946     Mlt::Producer *prod = NULL;
947     if (m_baseTrackProducers.count() == 0) return false;
948     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
949         if (m_baseTrackProducers.at(i) != NULL) {
950             prod = m_baseTrackProducers.at(i);
951             break;
952         }
953     }
954
955     if (!prod) return false;
956     int default_video = prod->get_int("video_index");
957     char property[200];
958     snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
959     return prod->get(property) == codec;
960 }
961
962 bool DocClipBase::hasAudioCodec(const QString &codec) const
963 {
964     Mlt::Producer *prod = NULL;
965     if (m_baseTrackProducers.count() == 0) return false;
966     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
967         if (m_baseTrackProducers.at(i) != NULL) {
968             prod = m_baseTrackProducers.at(i);
969             break;
970         }
971     }
972     if (!prod) return false;
973     int default_video = prod->get_int("audio_index");
974     char property[200];
975     snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
976     return prod->get(property) == codec;
977 }
978