]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
9ede6abbba82fd23dc808706dd439e60e773e0e1
[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;
130     zone.setX(m_properties.value("zone_in").toInt());
131     zone.setY(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     connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
139     m_audioTimer = new QTimer(this);
140     connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
141 }
142
143 void DocClipBase::askForAudioThumbs()
144 {
145     if (m_thumbProd && m_audioTimer) slotGetAudioThumbs();
146 }
147
148 void DocClipBase::slotClearAudioCache()
149 {
150     if (m_thumbProd) m_thumbProd->stopAudioThumbs();
151     if (m_audioTimer != NULL) m_audioTimer->stop();
152     m_audioFrameCache.clear();
153     m_audioThumbCreated = false;
154 }
155
156 /*void DocClipBase::getClipMainThumb() {
157     if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
158 }*/
159
160 KThumb *DocClipBase::thumbProducer()
161 {
162     return m_thumbProd;
163 }
164
165 bool DocClipBase::audioThumbCreated() const
166 {
167     return m_audioThumbCreated;
168 }
169
170 const QString DocClipBase::name() const
171 {
172
173     return m_properties.value("name");
174 }
175
176 const QString &DocClipBase::getId() const
177 {
178     return m_id;
179 }
180
181 void DocClipBase::setId(const QString &newId)
182 {
183     m_id = newId;
184 }
185
186 const CLIPTYPE & DocClipBase::clipType() const
187 {
188     return m_clipType;
189 }
190
191 void DocClipBase::setClipType(CLIPTYPE type)
192 {
193     m_clipType = type;
194
195     m_properties.insert("type", QString::number((int) type));
196     if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO || m_clipType == PLAYLIST))
197         slotCreateAudioTimer();
198 }
199
200 KUrl DocClipBase::fileURL() const
201 {
202     QString res = m_properties.value("resource");
203     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
204     return KUrl();
205 }
206
207 void DocClipBase::setClipThumbFrame(const uint &ix)
208 {
209     m_properties.insert("thumbnail", QString::number((int) ix));
210 }
211
212 uint DocClipBase::getClipThumbFrame() const
213 {
214     return (uint) m_properties.value("thumbnail").toInt();
215 }
216
217 const QString DocClipBase::description() const
218 {
219     return m_properties.value("description");
220 }
221
222 bool DocClipBase::isTransparent() const
223 {
224     return (m_properties.value("transparency") == "1");
225 }
226
227 const QString DocClipBase::getProperty(const QString prop) const
228 {
229     return m_properties.value(prop);
230 }
231
232 void DocClipBase::setDuration(GenTime dur)
233 {
234     m_duration = dur;
235     m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
236 }
237
238 const GenTime &DocClipBase::duration() const
239 {
240     return m_duration;
241 }
242
243 const GenTime DocClipBase::maxDuration() const
244 {
245     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
246         /*const GenTime dur(15000, KdenliveSettings::project_fps());
247         return dur;*/
248         return GenTime();
249     }
250     return m_duration;
251 }
252
253 bool DocClipBase::hasFileSize() const
254 {
255     return true;
256 }
257
258 qulonglong DocClipBase::fileSize() const
259 {
260     return m_properties.value("file_size").toULongLong();
261 }
262
263 // virtual
264 QDomElement DocClipBase::toXML() const
265 {
266     QDomDocument doc;
267     QDomElement clip = doc.createElement("producer");
268
269     QMapIterator<QString, QString> i(m_properties);
270     while (i.hasNext()) {
271         i.next();
272         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
273     }
274     doc.appendChild(clip);
275     if (!m_cutZones.isEmpty()) {
276         QStringList cuts;
277         for (int i = 0; i < m_cutZones.size(); i++) {
278             CutZoneInfo info = m_cutZones.at(i);
279             cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
280         }
281         clip.setAttribute("cutzones", cuts.join(";"));
282     }
283     //kDebug() << "/// CLIP XML: " << doc.toString();
284     return doc.documentElement();
285 }
286
287
288 void DocClipBase::setAudioThumbCreated(bool isDone)
289 {
290     m_audioThumbCreated = isDone;
291 }
292
293
294 void DocClipBase::setThumbnail(const QPixmap & pixmap)
295 {
296     m_thumbnail = pixmap;
297 }
298
299 const QPixmap & DocClipBase::thumbnail() const
300 {
301     return m_thumbnail;
302 }
303
304 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data)
305 {
306     //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
307     m_audioFrameCache = data;
308     m_audioThumbCreated = true;
309     emit gotAudioData();
310 }
311
312 QList < GenTime > DocClipBase::snapMarkers() const
313 {
314     QList < GenTime > markers;
315
316     for (int count = 0; count < m_snapMarkers.count(); ++count) {
317         markers.append(m_snapMarkers.at(count).time());
318     }
319
320     return markers;
321 }
322
323 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
324 {
325     return m_snapMarkers;
326 }
327
328 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers)
329 {
330     m_snapMarkers = markers;
331 }
332
333 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
334 {
335     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
336     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
337         if ((*it).time() >= time)
338             break;
339     }
340
341     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
342         (*it).setComment(comment);
343         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
344     } else {
345         CommentedTime t(time, comment);
346         m_snapMarkers.insert(it, t);
347     }
348
349 }
350
351 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
352 {
353     QList < CommentedTime >::Iterator it;
354     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
355         if ((*it).time() == time)
356             break;
357     }
358     if (it != m_snapMarkers.end()) {
359         (*it).setComment(comment);
360     } else {
361         kError() << "trying to edit Snap Marker that does not already exists";
362     }
363 }
364
365 QString DocClipBase::deleteSnapMarker(const GenTime & time)
366 {
367     QString result = i18n("Marker");
368     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
369
370     while (itt != m_snapMarkers.end()) {
371         if ((*itt).time() == time)
372             break;
373         ++itt;
374     }
375
376     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
377         result = (*itt).comment();
378         m_snapMarkers.erase(itt);
379     }
380     return result;
381 }
382
383
384 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
385 {
386     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
387
388     while (itt != m_snapMarkers.end()) {
389         if ((*itt).time() == time)
390             return time;
391         ++itt;
392     }
393
394     return GenTime(0.0);
395 }
396
397 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
398 {
399     int it;
400     for (it = 0; it < m_snapMarkers.count(); it++) {
401         if (m_snapMarkers.at(it).time() >= currTime)
402             break;
403     }
404     if (it == 0) return GenTime();
405     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
406         return m_snapMarkers.at(it).time();
407     else return m_snapMarkers.at(it - 1).time();
408 }
409
410 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
411 {
412     int it;
413     for (it = 0; it < m_snapMarkers.count(); it++) {
414         if (m_snapMarkers.at(it).time() > currTime)
415             break;
416     }
417     if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
418     return duration();
419 }
420
421 QString DocClipBase::markerComment(GenTime t)
422 {
423     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
424
425     while (itt != m_snapMarkers.end()) {
426         if ((*itt).time() == t)
427             return (*itt).comment();
428         ++itt;
429     }
430     return QString();
431 }
432
433 void DocClipBase::clearProducers()
434 {
435     m_baseTrackProducers.clear();
436 }
437
438 void DocClipBase::deleteProducers()
439 {
440     kDebug() << "// CLIP KILL PRODS ct: " << m_baseTrackProducers.count();
441     if (m_thumbProd) m_thumbProd->clearProducer();
442     /*kDebug()<<"// CLIP KILL PRODS ct: "<<m_baseTrackProducers.count();
443     int max = m_baseTrackProducers.count();
444     for (int i = 0; i < max; i++) {
445         kDebug()<<"// CLIP KILL PROD "<<i;
446     Mlt::Producer *p = m_baseTrackProducers.takeAt(i);
447     if (p != NULL) {
448      delete p;
449      p = NULL;
450     }
451     m_baseTrackProducers.insert(i, NULL);
452     }*/
453
454     delete m_videoOnlyProducer;
455     m_videoOnlyProducer = NULL;
456
457     qDeleteAll(m_baseTrackProducers);
458     m_baseTrackProducers.clear();
459     qDeleteAll(m_audioTrackProducers);
460     m_audioTrackProducers.clear();
461 }
462
463 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset)
464 {
465     if (producer == NULL) return;
466     if (reset) {
467         // Clear all previous producers
468         kDebug() << "/+++++++++++++++   DELETE ALL PRODS " << producer->get("id");
469         deleteProducers();
470     }
471     QString id = producer->get("id");
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) m_audioTrackProducers[pos] = producer;
483             return;
484         } else if (id.endsWith("video")) {
485             m_videoOnlyProducer = producer;
486             return;
487         }
488         int pos = id.toInt();
489         if (pos >= m_baseTrackProducers.count()) {
490             while (m_baseTrackProducers.count() - 1 < pos) {
491                 m_baseTrackProducers.append(NULL);
492             }
493         }
494         if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
495     } else {
496         if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
497         else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
498     }
499     //m_clipProducer = producer;
500     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
501     if (m_thumbProd && (reset || !m_thumbProd->hasProducer())) m_thumbProd->setProducer(producer);
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     m_metadata = properties;
727 }
728
729 QMap <QString, QString> DocClipBase::metadata() const
730 {
731     return m_metadata;
732 }
733
734 void DocClipBase::clearProperty(const QString &key)
735 {
736     m_properties.remove(key);
737 }
738
739 void DocClipBase::getFileHash(const QString url)
740 {
741     if (m_clipType == SLIDESHOW) return;
742     QFile file(url);
743     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
744         QByteArray fileData;
745         QByteArray fileHash;
746         //kDebug() << "SETTING HASH of" << value;
747         m_properties.insert("file_size", QString::number(file.size()));
748         /*
749                * 1 MB = 1 second per 450 files (or faster)
750                * 10 MB = 9 seconds per 450 files (or faster)
751                */
752         if (file.size() > 1000000*2) {
753             fileData = file.read(1000000);
754             if (file.seek(file.size() - 1000000))
755                 fileData.append(file.readAll());
756         } else
757             fileData = file.readAll();
758         file.close();
759         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
760         m_properties.insert("file_hash", QString(fileHash.toHex()));
761     }
762 }
763
764 QString DocClipBase::getClipHash() const
765 {
766     QString hash;
767     if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
768     else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
769     else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
770     else hash = m_properties.value("file_hash");
771     return hash;
772 }
773
774 // static
775 QString DocClipBase::getHash(const QString &path)
776 {
777     QFile file(path);
778     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
779         QByteArray fileData;
780         QByteArray fileHash;
781         /*
782                * 1 MB = 1 second per 450 files (or faster)
783                * 10 MB = 9 seconds per 450 files (or faster)
784                */
785         if (file.size() > 1000000*2) {
786             fileData = file.read(1000000);
787             if (file.seek(file.size() - 1000000))
788                 fileData.append(file.readAll());
789         } else
790             fileData = file.readAll();
791         file.close();
792         return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
793     }
794     return QString();
795 }
796
797 void DocClipBase::refreshThumbUrl()
798 {
799     if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
800 }
801
802 void DocClipBase::setProperty(const QString &key, const QString &value)
803 {
804     m_properties.insert(key, value);
805     if (key == "resource") {
806         getFileHash(value);
807         if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
808     } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
809     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
810     else if (key == "colour") {
811         char *tmp = (char *) qstrdup(value.toUtf8().data());
812         setProducerProperty("colour", tmp);
813         delete[] tmp;
814     } else if (key == "templatetext") {
815         char *tmp = (char *) qstrdup(value.toUtf8().data());
816         setProducerProperty("templatetext", tmp);
817         delete[] tmp;
818         setProducerProperty("force_reload", 1);
819     } else if (key == "xmldata") {
820         char *tmp = (char *) qstrdup(value.toUtf8().data());
821         setProducerProperty("xmldata", tmp);
822         delete[] tmp;
823         setProducerProperty("force_reload", 1);
824     } else if (key == "force_aspect_ratio") {
825         if (value.isEmpty()) {
826             m_properties.remove("force_aspect_ratio");
827             resetProducerProperty("force_aspect_ratio");
828         } else setProducerProperty("force_aspect_ratio", value.toDouble());
829     } else if (key == "force_fps") {
830         if (value.isEmpty()) {
831             m_properties.remove("force_fps");
832             resetProducerProperty("force_fps");
833         } else setProducerProperty("force_fps", value.toDouble());
834     } else if (key == "force_progressive") {
835         if (value.isEmpty()) {
836             m_properties.remove("force_progressive");
837             resetProducerProperty("force_progressive");
838         } else setProducerProperty("force_progressive", value.toInt());
839     } else if (key == "threads") {
840         if (value.isEmpty()) {
841             m_properties.remove("threads");
842             setProducerProperty("threads", 1);
843         } else setProducerProperty("threads", value.toInt());
844     } else if (key == "video_index") {
845         if (value.isEmpty()) {
846             m_properties.remove("video_index");
847             setProducerProperty("video_index", m_properties.value("default_video").toInt());
848         } else setProducerProperty("video_index", value.toInt());
849     } else if (key == "audio_index") {
850         if (value.isEmpty()) {
851             m_properties.remove("audio_index");
852             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
853         } else setProducerProperty("audio_index", value.toInt());
854     }
855 }
856
857 QMap <QString, QString> DocClipBase::properties() const
858 {
859     return m_properties;
860 }
861
862 bool DocClipBase::slotGetAudioThumbs()
863 {
864     if (m_thumbProd == NULL) return false;
865     if (!KdenliveSettings::audiothumbnails() || m_audioTimer == NULL) {
866         if (m_audioTimer != NULL) m_audioTimer->stop();
867         return false;
868     }
869     if (m_audioThumbCreated) {
870         m_audioTimer->stop();
871         return false;
872     }
873     m_audioTimer->start(1500);
874     double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
875     m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
876     return true;
877 }
878
879 bool DocClipBase::isPlaceHolder() const
880 {
881     return m_placeHolder;
882 }
883
884 void DocClipBase::addCutZone(int in, int out, QString desc)
885 {
886     CutZoneInfo info;
887     info.zone = QPoint(in, out);
888     info.description = desc;
889     for (int i = 0; i < m_cutZones.count(); i++)
890         if (m_cutZones.at(i).zone == info.zone) {
891             return;
892         }
893     m_cutZones.append(info);
894 }
895
896 bool DocClipBase::hasCutZone(QPoint p) const
897 {
898     for (int i = 0; i < m_cutZones.count(); i++)
899         if (m_cutZones.at(i).zone == p) return true;
900     return false;
901 }
902
903
904 void DocClipBase::removeCutZone(int in, int out)
905 {
906     QPoint p(in, out);
907     for (int i = 0; i < m_cutZones.count(); i++) {
908         if (m_cutZones.at(i).zone == p) {
909             m_cutZones.removeAt(i);
910             i--;
911         }
912     }
913 }
914
915 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
916 {
917     QPoint old(oldin, oldout);
918     for (int i = 0; i < m_cutZones.size(); ++i) {
919         if (m_cutZones.at(i).zone == old) {
920             CutZoneInfo info;
921             info.zone = QPoint(in, out);
922             info.description = desc;
923             m_cutZones.replace(i, info);
924             break;
925         }
926     }
927 }
928
929 QList <CutZoneInfo> DocClipBase::cutZones() const
930 {
931     return m_cutZones;
932 }
933
934 bool DocClipBase::hasVideoCodec(const QString &codec) const
935 {
936     Mlt::Producer *prod = NULL;
937     if (m_baseTrackProducers.count() == 0) return false;
938     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
939         if (m_baseTrackProducers.at(i) != NULL) {
940             prod = m_baseTrackProducers.at(i);
941             break;
942         }
943     }
944
945     if (!prod) return false;
946     int default_video = prod->get_int("video_index");
947     char property[200];
948     snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
949     return prod->get(property) == codec;
950 }
951
952 bool DocClipBase::hasAudioCodec(const QString &codec) const
953 {
954     Mlt::Producer *prod = NULL;
955     if (m_baseTrackProducers.count() == 0) return false;
956     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
957         if (m_baseTrackProducers.at(i) != NULL) {
958             prod = m_baseTrackProducers.at(i);
959             break;
960         }
961     }
962     if (!prod) return false;
963     int default_video = prod->get_int("audio_index");
964     char property[200];
965     snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
966     return prod->get(property) == codec;
967 }
968