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