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