]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
fc74fbf14a29dd36343dd1bb0fd80d2b275d6d09
[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.")) {
62             m_metadata.insert(name.section('.', 2, 3), attributes.item(i).nodeValue());
63         } else m_properties.insert(name, attributes.item(i).nodeValue());
64     }
65
66     if (xml.hasAttribute("cutzones")) {
67         QStringList cuts = xml.attribute("cutzones").split(";", QString::SkipEmptyParts);
68         for (int i = 0; i < cuts.count(); i++) {
69             QString z = cuts.at(i);
70             addCutZone(z.section('-', 0, 0).toInt(), z.section('-', 1, 1).toInt(), z.section('-', 2, 2));
71         }
72     }
73
74     KUrl url = KUrl(xml.attribute("resource"));
75     if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
76
77     if (xml.hasAttribute("duration")) {
78         setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
79     } else {
80         int out = xml.attribute("out").toInt();
81         int in = xml.attribute("in").toInt();
82         setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
83     }
84
85     if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
86
87     //if (!url.isEmpty() && QFile::exists(url.path()))
88     {
89         m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
90         if (m_clipType == AV || m_clipType == AUDIO || m_clipType == PLAYLIST) slotCreateAudioTimer();
91     }
92     //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
93 }
94
95 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
96     DocClipBase::operator=(clip);
97     m_id = clip.getId();
98     m_clipType = clip.clipType();
99     m_name = clip.name();
100     m_duration = clip.duration();
101     m_audioThumbCreated = clip.audioThumbCreated();
102     m_properties = clip.properties();
103     return *this;
104 }*/
105
106 DocClipBase::~DocClipBase()
107 {
108     kDebug() << "CLIP " << m_id << " DELETED******************************";
109     delete m_thumbProd;
110     if (m_audioTimer) {
111         m_audioTimer->stop();
112         delete m_audioTimer;
113     }
114     /*kDebug() <<" * * *CNT "<<m_baseTrackProducers.count();
115     if (m_baseTrackProducers.count() > 0) kDebug()<<"YOYO: "<<m_baseTrackProducers.at(0)->get_out()<<", CUT: "<<m_baseTrackProducers.at(0)->is_cut();*/
116     qDeleteAll(m_baseTrackProducers);
117     m_baseTrackProducers.clear();
118     qDeleteAll(m_audioTrackProducers);
119     m_audioTrackProducers.clear();
120     delete m_videoOnlyProducer;
121     m_videoOnlyProducer = NULL;
122 }
123
124 void DocClipBase::setZone(QPoint zone)
125 {
126     m_properties.insert("zone_in", QString::number(zone.x()));
127     m_properties.insert("zone_out", QString::number(zone.y()));
128 }
129
130 QPoint DocClipBase::zone() const
131 {
132     QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
133     return zone;
134 }
135
136 void DocClipBase::slotCreateAudioTimer()
137 {
138     connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
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) m_thumbProd->askForAudioThumbs(getId());
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
329 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
330 {
331     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
332     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
333         if ((*it).time() >= time)
334             break;
335     }
336
337     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
338         (*it).setComment(comment);
339         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
340     } else {
341         CommentedTime t(time, comment);
342         m_snapMarkers.insert(it, t);
343     }
344
345 }
346
347 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
348 {
349     QList < CommentedTime >::Iterator it;
350     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
351         if ((*it).time() == time)
352             break;
353     }
354     if (it != m_snapMarkers.end()) {
355         (*it).setComment(comment);
356     } else {
357         kError() << "trying to edit Snap Marker that does not already exists";
358     }
359 }
360
361 QString DocClipBase::deleteSnapMarker(const GenTime & time)
362 {
363     QString result = i18n("Marker");
364     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
365
366     while (itt != m_snapMarkers.end()) {
367         if ((*itt).time() == time)
368             break;
369         ++itt;
370     }
371
372     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
373         result = (*itt).comment();
374         m_snapMarkers.erase(itt);
375     }
376     return result;
377 }
378
379
380 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
381 {
382     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
383
384     while (itt != m_snapMarkers.end()) {
385         if ((*itt).time() == time)
386             return time;
387         ++itt;
388     }
389
390     return GenTime(0.0);
391 }
392
393 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
394 {
395     int it;
396     for (it = 0; it < m_snapMarkers.count(); it++) {
397         if (m_snapMarkers.at(it).time() >= currTime)
398             break;
399     }
400     if (it == 0) return GenTime();
401     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
402         return m_snapMarkers.at(it).time();
403     else return m_snapMarkers.at(it - 1).time();
404 }
405
406 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
407 {
408     int it;
409     for (it = 0; it < m_snapMarkers.count(); it++) {
410         if (m_snapMarkers.at(it).time() > currTime)
411             break;
412     }
413     if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
414     return duration();
415 }
416
417 QString DocClipBase::markerComment(GenTime t)
418 {
419     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
420
421     while (itt != m_snapMarkers.end()) {
422         if ((*itt).time() == t)
423             return (*itt).comment();
424         ++itt;
425     }
426     return QString();
427 }
428
429 void DocClipBase::clearProducers()
430 {
431     m_baseTrackProducers.clear();
432 }
433
434 void DocClipBase::deleteProducers(bool clearThumbCreator)
435 {
436     kDebug() << "// CLIP KILL PRODS ct: " << m_baseTrackProducers.count();
437     if (clearThumbCreator && m_thumbProd) m_thumbProd->clearProducer();
438     /*kDebug()<<"// CLIP KILL PRODS ct: "<<m_baseTrackProducers.count();
439     int max = m_baseTrackProducers.count();
440     for (int i = 0; i < max; i++) {
441         kDebug()<<"// CLIP KILL PROD "<<i;
442     Mlt::Producer *p = m_baseTrackProducers.takeAt(i);
443     if (p != NULL) {
444      delete p;
445      p = NULL;
446     }
447     m_baseTrackProducers.insert(i, NULL);
448     }*/
449
450     delete m_videoOnlyProducer;
451     m_videoOnlyProducer = NULL;
452
453     qDeleteAll(m_baseTrackProducers);
454     m_baseTrackProducers.clear();
455     qDeleteAll(m_audioTrackProducers);
456     m_audioTrackProducers.clear();
457 }
458
459 void DocClipBase::setValid()
460 {
461     m_placeHolder = false;
462 }
463
464 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset)
465 {
466     if (producer == NULL || (m_placeHolder && !reset)) return;
467     if (m_thumbProd && (reset || !m_thumbProd->hasProducer())) m_thumbProd->setProducer(producer);
468     if (reset) {
469         // Clear all previous producers
470         kDebug() << "/+++++++++++++++   DELETE ALL PRODS " << producer->get("id");
471         deleteProducers(false);
472     }
473     QString id = producer->get("id");
474     if (id.contains('_')) {
475         // this is a subtrack producer, insert it at correct place
476         id = id.section('_', 1);
477         if (id.endsWith("audio")) {
478             int pos = id.section('_', 0, 0).toInt();
479             if (pos >= m_audioTrackProducers.count()) {
480                 while (m_audioTrackProducers.count() - 1 < pos) {
481                     m_audioTrackProducers.append(NULL);
482                 }
483             }
484             if (m_audioTrackProducers.at(pos) == NULL) m_audioTrackProducers[pos] = producer;
485             return;
486         } else if (id.endsWith("video")) {
487             m_videoOnlyProducer = producer;
488             return;
489         }
490         int pos = id.toInt();
491         if (pos >= m_baseTrackProducers.count()) {
492             while (m_baseTrackProducers.count() - 1 < pos) {
493                 m_baseTrackProducers.append(NULL);
494             }
495         }
496         if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
497     } else {
498         if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
499         else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
500     }
501     //m_clipProducer = producer;
502     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
503 }
504
505 Mlt::Producer *DocClipBase::audioProducer(int track)
506 {
507     if (m_audioTrackProducers.count() <= track) {
508         while (m_audioTrackProducers.count() - 1 < track) {
509             m_audioTrackProducers.append(NULL);
510         }
511     }
512     if (m_audioTrackProducers.at(track) == NULL) {
513         Mlt::Producer *base = producer();
514         m_audioTrackProducers[track] = new Mlt::Producer(*(base->profile()), base->get("resource"));
515         if (m_properties.contains("force_aspect_ratio")) m_audioTrackProducers.at(track)->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
516         if (m_properties.contains("force_fps")) m_audioTrackProducers.at(track)->set("force_fps", m_properties.value("force_fps").toDouble());
517         if (m_properties.contains("force_progressive")) m_audioTrackProducers.at(track)->set("force_progressive", m_properties.value("force_progressive").toInt());
518         if (m_properties.contains("threads")) m_audioTrackProducers.at(track)->set("threads", m_properties.value("threads").toInt());
519         m_audioTrackProducers.at(track)->set("video_index", -1);
520         if (m_properties.contains("audio_index")) m_audioTrackProducers.at(track)->set("audio_index", m_properties.value("audio_index").toInt());
521         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track) + "_audio").toUtf8().data());
522         m_audioTrackProducers.at(track)->set("id", tmp);
523         delete[] tmp;
524     }
525     return m_audioTrackProducers.at(track);
526 }
527
528 Mlt::Producer *DocClipBase::videoProducer()
529 {
530     if (m_videoOnlyProducer == NULL) {
531         int i;
532         for (i = 0; i < m_baseTrackProducers.count(); i++)
533             if (m_baseTrackProducers.at(i) != NULL) break;
534         if (i >= m_baseTrackProducers.count()) return NULL;
535         m_videoOnlyProducer = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
536         if (m_properties.contains("force_aspect_ratio")) m_videoOnlyProducer->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
537         if (m_properties.contains("force_fps")) m_videoOnlyProducer->set("force_fps", m_properties.value("force_fps").toDouble());
538         if (m_properties.contains("force_progressive")) m_videoOnlyProducer->set("force_progressive", m_properties.value("force_progressive").toInt());
539         if (m_properties.contains("threads")) m_videoOnlyProducer->set("threads", m_properties.value("threads").toInt());
540         m_videoOnlyProducer->set("audio_index", -1);
541         if (m_properties.contains("video_index")) m_videoOnlyProducer->set("video_index", m_properties.value("video_index").toInt());
542         char *tmp = (char *) qstrdup(QString(getId() + "_video").toUtf8().data());
543         m_videoOnlyProducer->set("id", tmp);
544         delete[] tmp;
545     }
546     return m_videoOnlyProducer;
547 }
548
549 Mlt::Producer *DocClipBase::producer(int track)
550 {
551     /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
552         if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
553     }*/
554     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
555         if (m_baseTrackProducers.count() == 0) return NULL;
556         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
557             if (m_baseTrackProducers.at(i) != NULL)
558                 return m_baseTrackProducers.at(i);
559         }
560         return NULL;
561     }
562     if (track >= m_baseTrackProducers.count()) {
563         while (m_baseTrackProducers.count() - 1 < track) {
564             m_baseTrackProducers.append(NULL);
565         }
566     }
567     if (m_baseTrackProducers.at(track) == NULL) {
568         int i;
569         for (i = 0; i < m_baseTrackProducers.count(); i++)
570             if (m_baseTrackProducers.at(i) != NULL) break;
571
572         if (i >= m_baseTrackProducers.count()) return NULL;
573
574         if (KIO::NetAccess::exists(KUrl(m_baseTrackProducers.at(i)->get("resource")), KIO::NetAccess::SourceSide, 0))
575             m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
576         else { // special case for placeholder clips
577             m_baseTrackProducers[track] = NULL;
578             return NULL;
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         if (getProperty("crop") == "1") {
706             // we want a center crop filter effect
707             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
708             int ct = 0;
709             Mlt::Filter *filter = clipService.filter(ct);
710             while (filter) {
711                 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
712                     break;
713                 }
714                 ct++;
715                 filter = clipService.filter(ct);
716             }
717
718             if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
719                 // filter does not exist, create it...
720                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
721                 filter->set("center", 1);
722                 clipService.attach(*filter);
723             }
724         } else {
725             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
726             int ct = 0;
727             Mlt::Filter *filter = clipService.filter(0);
728             while (filter) {
729                 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
730                     clipService.detach(*filter);
731                 } else ct++;
732                 filter = clipService.filter(ct);
733             }
734         }
735     }
736 }
737
738 void DocClipBase::setProperties(QMap <QString, QString> properties)
739 {
740     // changing clip type is not allowed
741     properties.remove("type");
742     QMapIterator<QString, QString> i(properties);
743     bool refreshProducer = false;
744     QStringList keys;
745     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
746     while (i.hasNext()) {
747         i.next();
748         setProperty(i.key(), i.value());
749         if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
750     }
751     if (refreshProducer) slotRefreshProducer();
752 }
753
754 void DocClipBase::setMetadata(QMap <QString, QString> properties)
755 {
756     QMapIterator<QString, QString> i(properties);
757     while (i.hasNext()) {
758         i.next();
759         if (i.value().isEmpty() && m_metadata.contains(i.key())) {
760             m_metadata.remove(i.key());
761         } else {
762             m_metadata.insert(i.key(), i.value());
763         }
764     }
765 }
766
767 QMap <QString, QString> DocClipBase::metadata() const
768 {
769     return m_metadata;
770 }
771
772 void DocClipBase::clearProperty(const QString &key)
773 {
774     m_properties.remove(key);
775 }
776
777 void DocClipBase::getFileHash(const QString url)
778 {
779     if (m_clipType == SLIDESHOW) return;
780     QFile file(url);
781     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
782         QByteArray fileData;
783         QByteArray fileHash;
784         //kDebug() << "SETTING HASH of" << value;
785         m_properties.insert("file_size", QString::number(file.size()));
786         /*
787                * 1 MB = 1 second per 450 files (or faster)
788                * 10 MB = 9 seconds per 450 files (or faster)
789                */
790         if (file.size() > 1000000*2) {
791             fileData = file.read(1000000);
792             if (file.seek(file.size() - 1000000))
793                 fileData.append(file.readAll());
794         } else
795             fileData = file.readAll();
796         file.close();
797         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
798         m_properties.insert("file_hash", QString(fileHash.toHex()));
799     }
800 }
801
802 bool DocClipBase::checkHash() const
803 {
804     KUrl url = fileURL();
805     if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
806     return true;
807 }
808
809 QString DocClipBase::getClipHash() const
810 {
811     QString hash;
812     if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
813     else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
814     else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
815     else hash = m_properties.value("file_hash");
816     return hash;
817 }
818
819 void DocClipBase::setPlaceHolder(bool place)
820 {
821     m_placeHolder = place;
822 }
823
824 // static
825 QString DocClipBase::getHash(const QString &path)
826 {
827     QFile file(path);
828     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
829         QByteArray fileData;
830         QByteArray fileHash;
831         /*
832                * 1 MB = 1 second per 450 files (or faster)
833                * 10 MB = 9 seconds per 450 files (or faster)
834                */
835         if (file.size() > 1000000*2) {
836             fileData = file.read(1000000);
837             if (file.seek(file.size() - 1000000))
838                 fileData.append(file.readAll());
839         } else
840             fileData = file.readAll();
841         file.close();
842         return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
843     }
844     return QString();
845 }
846
847 void DocClipBase::refreshThumbUrl()
848 {
849     if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
850 }
851
852 void DocClipBase::setProperty(const QString &key, const QString &value)
853 {
854     m_properties.insert(key, value);
855     if (key == "resource") {
856         getFileHash(value);
857         if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
858     } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
859     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
860     else if (key == "colour") {
861         char *tmp = (char *) qstrdup(value.toUtf8().data());
862         setProducerProperty("colour", tmp);
863         delete[] tmp;
864     } else if (key == "templatetext") {
865         char *tmp = (char *) qstrdup(value.toUtf8().data());
866         setProducerProperty("templatetext", tmp);
867         delete[] tmp;
868         setProducerProperty("force_reload", 1);
869     } else if (key == "xmldata") {
870         char *tmp = (char *) qstrdup(value.toUtf8().data());
871         setProducerProperty("xmldata", tmp);
872         delete[] tmp;
873         setProducerProperty("force_reload", 1);
874     } else if (key == "force_aspect_ratio") {
875         if (value.isEmpty()) {
876             m_properties.remove("force_aspect_ratio");
877             resetProducerProperty("force_aspect_ratio");
878         } else setProducerProperty("force_aspect_ratio", value.toDouble());
879     } else if (key == "force_fps") {
880         if (value.isEmpty()) {
881             m_properties.remove("force_fps");
882             resetProducerProperty("force_fps");
883         } else setProducerProperty("force_fps", value.toDouble());
884     } else if (key == "force_progressive") {
885         if (value.isEmpty()) {
886             m_properties.remove("force_progressive");
887             resetProducerProperty("force_progressive");
888         } else setProducerProperty("force_progressive", value.toInt());
889     } else if (key == "threads") {
890         if (value.isEmpty()) {
891             m_properties.remove("threads");
892             setProducerProperty("threads", 1);
893         } else setProducerProperty("threads", value.toInt());
894     } else if (key == "video_index") {
895         if (value.isEmpty()) {
896             m_properties.remove("video_index");
897             setProducerProperty("video_index", m_properties.value("default_video").toInt());
898         } else setProducerProperty("video_index", value.toInt());
899     } else if (key == "audio_index") {
900         if (value.isEmpty()) {
901             m_properties.remove("audio_index");
902             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
903         } else setProducerProperty("audio_index", value.toInt());
904     }
905 }
906
907 QMap <QString, QString> DocClipBase::properties() const
908 {
909     return m_properties;
910 }
911
912 bool DocClipBase::slotGetAudioThumbs()
913 {
914     if (m_thumbProd == NULL || isPlaceHolder()) return false;
915     if (!KdenliveSettings::audiothumbnails() || m_audioTimer == NULL) {
916         if (m_audioTimer != NULL) m_audioTimer->stop();
917         return false;
918     }
919     if (m_audioThumbCreated) {
920         m_audioTimer->stop();
921         return false;
922     }
923     m_audioTimer->start(1500);
924     double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
925     m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
926     return true;
927 }
928
929 bool DocClipBase::isPlaceHolder() const
930 {
931     return m_placeHolder;
932 }
933
934 void DocClipBase::addCutZone(int in, int out, QString desc)
935 {
936     CutZoneInfo info;
937     info.zone = QPoint(in, out);
938     info.description = desc;
939     for (int i = 0; i < m_cutZones.count(); i++)
940         if (m_cutZones.at(i).zone == info.zone) {
941             return;
942         }
943     m_cutZones.append(info);
944 }
945
946 bool DocClipBase::hasCutZone(QPoint p) const
947 {
948     for (int i = 0; i < m_cutZones.count(); i++)
949         if (m_cutZones.at(i).zone == p) return true;
950     return false;
951 }
952
953
954 void DocClipBase::removeCutZone(int in, int out)
955 {
956     QPoint p(in, out);
957     for (int i = 0; i < m_cutZones.count(); i++) {
958         if (m_cutZones.at(i).zone == p) {
959             m_cutZones.removeAt(i);
960             i--;
961         }
962     }
963 }
964
965 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
966 {
967     QPoint old(oldin, oldout);
968     for (int i = 0; i < m_cutZones.size(); ++i) {
969         if (m_cutZones.at(i).zone == old) {
970             CutZoneInfo info;
971             info.zone = QPoint(in, out);
972             info.description = desc;
973             m_cutZones.replace(i, info);
974             break;
975         }
976     }
977 }
978
979 QList <CutZoneInfo> DocClipBase::cutZones() const
980 {
981     return m_cutZones;
982 }
983
984 bool DocClipBase::hasVideoCodec(const QString &codec) const
985 {
986     Mlt::Producer *prod = NULL;
987     if (m_baseTrackProducers.count() == 0) return false;
988     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
989         if (m_baseTrackProducers.at(i) != NULL) {
990             prod = m_baseTrackProducers.at(i);
991             break;
992         }
993     }
994
995     if (!prod) return false;
996     int default_video = prod->get_int("video_index");
997     char property[200];
998     snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
999     return prod->get(property) == codec;
1000 }
1001
1002 bool DocClipBase::hasAudioCodec(const QString &codec) const
1003 {
1004     Mlt::Producer *prod = NULL;
1005     if (m_baseTrackProducers.count() == 0) return false;
1006     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1007         if (m_baseTrackProducers.at(i) != NULL) {
1008             prod = m_baseTrackProducers.at(i);
1009             break;
1010         }
1011     }
1012     if (!prod) return false;
1013     int default_video = prod->get_int("audio_index");
1014     char property[200];
1015     snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1016     return prod->get(property) == codec;
1017 }
1018