]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
Add support for forcing a clip to be progressive or interlace.
[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 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id) :
38         QObject(),
39         m_audioFrameCache(),
40         m_refcount(0),
41         m_baseTrackProducers(),
42         m_audioTrackProducers(),
43         m_videoOnlyProducer(NULL),
44         m_snapMarkers(),
45         m_duration(),
46         m_audioTimer(NULL),
47         m_thumbProd(NULL),
48         m_audioThumbCreated(false),
49         m_id(id),
50         m_placeHolder(xml.hasAttribute("placeholder")),
51         m_properties()
52 {
53     int type = xml.attribute("type").toInt();
54     m_clipType = (CLIPTYPE) type;
55     if (m_placeHolder) xml.removeAttribute("placeholder");
56     QDomNamedNodeMap attributes = xml.attributes();
57     for (int i = 0; i < attributes.count(); i++) {
58         m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
59     }
60
61     if (xml.hasAttribute("cutzones")) {
62         QStringList cuts = xml.attribute("cutzones").split(";", QString::SkipEmptyParts);
63         for (int i = 0; i < cuts.count(); i++) {
64             QString z = cuts.at(i);
65             addCutZone(z.section('-', 0, 0).toInt(), z.section('-', 1, 1).toInt(), z.section('-', 2, 2));
66         }
67     }
68
69     KUrl url = KUrl(xml.attribute("resource"));
70     if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
71
72     if (xml.hasAttribute("duration")) {
73         setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
74     } else {
75         int out = xml.attribute("out").toInt();
76         int in = xml.attribute("in").toInt();
77         setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
78     }
79
80     if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
81
82     //if (!url.isEmpty() && QFile::exists(url.path()))
83     {
84         m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
85         if (m_clipType == AV || m_clipType == AUDIO || m_clipType == PLAYLIST) slotCreateAudioTimer();
86     }
87     //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
88 }
89
90 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
91     DocClipBase::operator=(clip);
92     m_id = clip.getId();
93     m_clipType = clip.clipType();
94     m_name = clip.name();
95     m_duration = clip.duration();
96     m_audioThumbCreated = clip.audioThumbCreated();
97     m_properties = clip.properties();
98     return *this;
99 }*/
100
101 DocClipBase::~DocClipBase()
102 {
103     kDebug() << "CLIP " << m_id << " DELETED******************************";
104     delete m_thumbProd;
105     if (m_audioTimer) {
106         m_audioTimer->stop();
107         delete m_audioTimer;
108     }
109     /*kDebug() <<" * * *CNT "<<m_baseTrackProducers.count();
110     if (m_baseTrackProducers.count() > 0) kDebug()<<"YOYO: "<<m_baseTrackProducers.at(0)->get_out()<<", CUT: "<<m_baseTrackProducers.at(0)->is_cut();*/
111     qDeleteAll(m_baseTrackProducers);
112     m_baseTrackProducers.clear();
113     qDeleteAll(m_audioTrackProducers);
114     m_audioTrackProducers.clear();
115     delete m_videoOnlyProducer;
116     m_videoOnlyProducer = NULL;
117 }
118
119 void DocClipBase::setZone(QPoint zone)
120 {
121     m_properties.insert("zone_in", QString::number(zone.x()));
122     m_properties.insert("zone_out", QString::number(zone.y()));
123 }
124
125 QPoint DocClipBase::zone() const
126 {
127     QPoint zone;
128     zone.setX(m_properties.value("zone_in").toInt());
129     zone.setY(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) slotGetAudioThumbs();
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 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers)
327 {
328     m_snapMarkers = markers;
329 }
330
331 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
332 {
333     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
334     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
335         if ((*it).time() >= time)
336             break;
337     }
338
339     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
340         (*it).setComment(comment);
341         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
342     } else {
343         CommentedTime t(time, comment);
344         m_snapMarkers.insert(it, t);
345     }
346
347 }
348
349 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
350 {
351     QList < CommentedTime >::Iterator it;
352     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
353         if ((*it).time() == time)
354             break;
355     }
356     if (it != m_snapMarkers.end()) {
357         (*it).setComment(comment);
358     } else {
359         kError() << "trying to edit Snap Marker that does not already exists";
360     }
361 }
362
363 QString DocClipBase::deleteSnapMarker(const GenTime & time)
364 {
365     QString result = i18n("Marker");
366     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
367
368     while (itt != m_snapMarkers.end()) {
369         if ((*itt).time() == time)
370             break;
371         ++itt;
372     }
373
374     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
375         result = (*itt).comment();
376         m_snapMarkers.erase(itt);
377     }
378     return result;
379 }
380
381
382 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
383 {
384     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
385
386     while (itt != m_snapMarkers.end()) {
387         if ((*itt).time() == time)
388             return time;
389         ++itt;
390     }
391
392     return GenTime(0.0);
393 }
394
395 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
396 {
397     int it;
398     for (it = 0; it < m_snapMarkers.count(); it++) {
399         if (m_snapMarkers.at(it).time() >= currTime)
400             break;
401     }
402     if (it == 0) return GenTime();
403     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
404         return m_snapMarkers.at(it).time();
405     else return m_snapMarkers.at(it - 1).time();
406 }
407
408 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
409 {
410     int it;
411     for (it = 0; it < m_snapMarkers.count(); it++) {
412         if (m_snapMarkers.at(it).time() > currTime)
413             break;
414     }
415     if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
416     return duration();
417 }
418
419 QString DocClipBase::markerComment(GenTime t)
420 {
421     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
422
423     while (itt != m_snapMarkers.end()) {
424         if ((*itt).time() == t)
425             return (*itt).comment();
426         ++itt;
427     }
428     return QString();
429 }
430
431 void DocClipBase::clearProducers()
432 {
433     m_baseTrackProducers.clear();
434 }
435
436 void DocClipBase::deleteProducers()
437 {
438     kDebug() << "// CLIP KILL PRODS ct: " << m_baseTrackProducers.count();
439     if (m_thumbProd) m_thumbProd->clearProducer();
440     /*kDebug()<<"// CLIP KILL PRODS ct: "<<m_baseTrackProducers.count();
441     int max = m_baseTrackProducers.count();
442     for (int i = 0; i < max; i++) {
443         kDebug()<<"// CLIP KILL PROD "<<i;
444     Mlt::Producer *p = m_baseTrackProducers.takeAt(i);
445     if (p != NULL) {
446      delete p;
447      p = NULL;
448     }
449     m_baseTrackProducers.insert(i, NULL);
450     }*/
451
452     delete m_videoOnlyProducer;
453     m_videoOnlyProducer = NULL;
454
455     qDeleteAll(m_baseTrackProducers);
456     m_baseTrackProducers.clear();
457     qDeleteAll(m_audioTrackProducers);
458     m_audioTrackProducers.clear();
459 }
460
461 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset)
462 {
463     if (producer == NULL) return;
464     if (reset) {
465         // Clear all previous producers
466         kDebug() << "/+++++++++++++++   DELETE ALL PRODS " << producer->get("id");
467         deleteProducers();
468     }
469     QString id = producer->get("id");
470     if (id.contains('_')) {
471         // this is a subtrack producer, insert it at correct place
472         id = id.section('_', 1);
473         if (id.endsWith("audio")) {
474             int pos = id.section('_', 0, 0).toInt();
475             if (pos >= m_audioTrackProducers.count()) {
476                 while (m_audioTrackProducers.count() - 1 < pos) {
477                     m_audioTrackProducers.append(NULL);
478                 }
479             }
480             if (m_audioTrackProducers.at(pos) == NULL) m_audioTrackProducers[pos] = producer;
481             return;
482         } else if (id.endsWith("video")) {
483             m_videoOnlyProducer = producer;
484             return;
485         }
486         int pos = id.toInt();
487         if (pos >= m_baseTrackProducers.count()) {
488             while (m_baseTrackProducers.count() - 1 < pos) {
489                 m_baseTrackProducers.append(NULL);
490             }
491         }
492         if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
493     } else {
494         if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
495         else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
496     }
497     //m_clipProducer = producer;
498     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
499     if (m_thumbProd && (reset || !m_thumbProd->hasProducer())) m_thumbProd->setProducer(producer);
500 }
501
502 Mlt::Producer *DocClipBase::audioProducer(int track)
503 {
504     if (m_audioTrackProducers.count() <= track) {
505         while (m_audioTrackProducers.count() - 1 < track) {
506             m_audioTrackProducers.append(NULL);
507         }
508     }
509     if (m_audioTrackProducers.at(track) == NULL) {
510         Mlt::Producer *base = producer();
511         m_audioTrackProducers[track] = new Mlt::Producer(*(base->profile()), base->get("resource"));
512         if (m_properties.contains("force_aspect_ratio")) m_audioTrackProducers.at(track)->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
513         if (m_properties.contains("force_progressive")) m_audioTrackProducers.at(track)->set("force_progressive", m_properties.value("force_progressive").toInt());
514         if (m_properties.contains("threads")) m_audioTrackProducers.at(track)->set("threads", m_properties.value("threads").toInt());
515         m_audioTrackProducers.at(track)->set("video_index", -1);
516         if (m_properties.contains("audio_index")) m_audioTrackProducers.at(track)->set("audio_index", m_properties.value("audio_index").toInt());
517         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track) + "_audio").toUtf8().data());
518         m_audioTrackProducers.at(track)->set("id", tmp);
519         delete[] tmp;
520     }
521     return m_audioTrackProducers.at(track);
522 }
523
524 Mlt::Producer *DocClipBase::videoProducer()
525 {
526     if (m_videoOnlyProducer == NULL) {
527         int i;
528         for (i = 0; i < m_baseTrackProducers.count(); i++)
529             if (m_baseTrackProducers.at(i) != NULL) break;
530         if (i >= m_baseTrackProducers.count()) return NULL;
531         m_videoOnlyProducer = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
532         if (m_properties.contains("force_aspect_ratio")) m_videoOnlyProducer->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
533         if (m_properties.contains("force_progressive")) m_videoOnlyProducer->set("force_progressive", m_properties.value("force_progressive").toInt());
534         if (m_properties.contains("threads")) m_videoOnlyProducer->set("threads", m_properties.value("threads").toInt());
535         m_videoOnlyProducer->set("audio_index", -1);
536         if (m_properties.contains("video_index")) m_videoOnlyProducer->set("video_index", m_properties.value("video_index").toInt());
537         char *tmp = (char *) qstrdup(QString(getId() + "_video").toUtf8().data());
538         m_videoOnlyProducer->set("id", tmp);
539         delete[] tmp;
540     }
541     return m_videoOnlyProducer;
542 }
543
544 Mlt::Producer *DocClipBase::producer(int track)
545 {
546     /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
547         if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
548     }*/
549     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
550         if (m_baseTrackProducers.count() == 0) return NULL;
551         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
552             if (m_baseTrackProducers.at(i) != NULL)
553                 return m_baseTrackProducers.at(i);
554         }
555         return NULL;
556     }
557     if (track >= m_baseTrackProducers.count()) {
558         while (m_baseTrackProducers.count() - 1 < track) {
559             m_baseTrackProducers.append(NULL);
560         }
561     }
562     if (m_baseTrackProducers.at(track) == NULL) {
563         int i;
564         for (i = 0; i < m_baseTrackProducers.count(); i++)
565             if (m_baseTrackProducers.at(i) != NULL) break;
566
567         if (i >= m_baseTrackProducers.count()) return NULL;
568
569         if (KIO::NetAccess::exists(KUrl(m_baseTrackProducers.at(i)->get("resource")), KIO::NetAccess::SourceSide, 0))
570             m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
571         else { // special case for placeholder clips
572             m_baseTrackProducers[track] = NULL;
573             return NULL;
574         }
575
576         if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
577         if (m_properties.contains("force_progressive")) m_baseTrackProducers[track]->set("force_progressive", m_properties.value("force_progressive").toInt());
578         if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
579         if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
580         if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
581         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
582         m_baseTrackProducers[track]->set("id", tmp);
583         delete[] tmp;
584         if (KdenliveSettings::dropbframes() && m_baseTrackProducers.at(i)->get("skip_loop_filter") && strcmp(m_baseTrackProducers.at(i)->get("skip_loop_filter"), "all") == 0) {
585             m_baseTrackProducers[track]->set("skip_loop_filter", "all");
586             m_baseTrackProducers[track]->set("skip_frame", "bidir");
587         }
588     }
589     return m_baseTrackProducers.at(track);
590 }
591
592 void DocClipBase::setProducerProperty(const char *name, int data)
593 {
594     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
595         if (m_baseTrackProducers.at(i) != NULL)
596             m_baseTrackProducers[i]->set(name, data);
597     }
598 }
599
600 void DocClipBase::setProducerProperty(const char *name, double data)
601 {
602     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
603         if (m_baseTrackProducers.at(i) != NULL)
604             m_baseTrackProducers[i]->set(name, data);
605     }
606 }
607
608 void DocClipBase::setProducerProperty(const char *name, const char *data)
609 {
610     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
611         if (m_baseTrackProducers.at(i) != NULL)
612             m_baseTrackProducers[i]->set(name, data);
613     }
614 }
615
616 const char *DocClipBase::producerProperty(const char *name) const
617 {
618     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
619         if (m_baseTrackProducers.at(i) != NULL) {
620             return m_baseTrackProducers.at(i)->get(name);
621         }
622     }
623     return NULL;
624 }
625
626
627 void DocClipBase::slotRefreshProducer()
628 {
629     if (m_baseTrackProducers.count() == 0) return;
630     kDebug() << "////////////   REFRESH CLIP !!!!!!!!!!!!!!!!";
631     if (m_clipType == SLIDESHOW) {
632         /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
633                Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
634                delete[] tmp;
635         delete m_clipProducer;
636         m_clipProducer = new Mlt::Producer(producer.get_producer());
637         if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
638         setProducerProperty("ttl", getProperty("ttl").toInt());
639         //m_clipProducer->set("id", getProperty("id"));
640         if (getProperty("fade") == "1") {
641             // we want a fade filter effect
642             kDebug() << "////////////   FADE WANTED";
643             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
644             int ct = 0;
645             Mlt::Filter *filter = clipService.filter(ct);
646             while (filter) {
647                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
648                     break;
649                 }
650                 ct++;
651                 filter = clipService.filter(ct);
652             }
653
654             if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
655                 filter->set("period", getProperty("ttl").toInt() - 1);
656                 filter->set("luma.out", getProperty("luma_duration").toInt());
657                 QString resource = getProperty("luma_file");
658                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
659                 filter->set("luma.resource", tmp);
660                 delete[] tmp;
661                 if (!getProperty("softness").isEmpty()) {
662                     int soft = getProperty("softness").toInt();
663                     filter->set("luma.softness", (double) soft / 100.0);
664                 }
665             } else {
666                 // filter does not exist, create it...
667                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
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                 clipService.attach(*filter);
679             }
680         } else {
681             kDebug() << "////////////   FADE NOT WANTED!!!";
682             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
683             int ct = 0;
684             Mlt::Filter *filter = clipService.filter(0);
685             while (filter) {
686                 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
687                     clipService.detach(*filter);
688                 } else ct++;
689                 filter = clipService.filter(ct);
690             }
691         }
692     }
693 }
694
695 void DocClipBase::setProperties(QMap <QString, QString> properties)
696 {
697     // changing clip type is not allowed
698     properties.remove("type");
699     QMapIterator<QString, QString> i(properties);
700     bool refreshProducer = false;
701     QStringList keys;
702     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
703     while (i.hasNext()) {
704         i.next();
705         setProperty(i.key(), i.value());
706         if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
707     }
708     if (refreshProducer) slotRefreshProducer();
709 }
710
711 void DocClipBase::setMetadata(QMap <QString, QString> properties)
712 {
713     m_metadata = properties;
714 }
715
716 QMap <QString, QString> DocClipBase::metadata() const
717 {
718     return m_metadata;
719 }
720
721 void DocClipBase::clearProperty(const QString &key)
722 {
723     m_properties.remove(key);
724 }
725
726 void DocClipBase::getFileHash(const QString url)
727 {
728     if (m_clipType == SLIDESHOW) return;
729     QFile file(url);
730     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
731         QByteArray fileData;
732         QByteArray fileHash;
733         //kDebug() << "SETTING HASH of" << value;
734         m_properties.insert("file_size", QString::number(file.size()));
735         /*
736                * 1 MB = 1 second per 450 files (or faster)
737                * 10 MB = 9 seconds per 450 files (or faster)
738                */
739         if (file.size() > 1000000*2) {
740             fileData = file.read(1000000);
741             if (file.seek(file.size() - 1000000))
742                 fileData.append(file.readAll());
743         } else
744             fileData = file.readAll();
745         file.close();
746         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
747         m_properties.insert("file_hash", QString(fileHash.toHex()));
748     }
749 }
750
751 QString DocClipBase::getClipHash() const
752 {
753     QString hash;
754     if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
755     else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
756     else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
757     else hash = m_properties.value("file_hash");
758     return hash;
759 }
760
761 // static
762 QString DocClipBase::getHash(const QString &path)
763 {
764     QFile file(path);
765     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
766         QByteArray fileData;
767         QByteArray fileHash;
768         /*
769                * 1 MB = 1 second per 450 files (or faster)
770                * 10 MB = 9 seconds per 450 files (or faster)
771                */
772         if (file.size() > 1000000*2) {
773             fileData = file.read(1000000);
774             if (file.seek(file.size() - 1000000))
775                 fileData.append(file.readAll());
776         } else
777             fileData = file.readAll();
778         file.close();
779         return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
780     }
781     return QString();
782 }
783
784 void DocClipBase::refreshThumbUrl()
785 {
786     if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
787 }
788
789 void DocClipBase::setProperty(const QString &key, const QString &value)
790 {
791     m_properties.insert(key, value);
792     if (key == "resource") {
793         getFileHash(value);
794         if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
795     } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
796     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
797     else if (key == "colour") {
798         char *tmp = (char *) qstrdup(value.toUtf8().data());
799         setProducerProperty("colour", tmp);
800         delete[] tmp;
801     } else if (key == "templatetext") {
802         char *tmp = (char *) qstrdup(value.toUtf8().data());
803         setProducerProperty("templatetext", tmp);
804         delete[] tmp;
805         setProducerProperty("force_reload", 1);
806     } else if (key == "xmldata") {
807         char *tmp = (char *) qstrdup(value.toUtf8().data());
808         setProducerProperty("xmldata", tmp);
809         delete[] tmp;
810         setProducerProperty("force_reload", 1);
811     } else if (key == "force_aspect_ratio") {
812         if (value.isEmpty()) {
813             m_properties.remove("force_aspect_ratio");
814             setProducerProperty("force_aspect_ratio", NULL);
815         } else setProducerProperty("force_aspect_ratio", value.toDouble());
816     } else if (key == "force_progressive") {
817         if (value.isEmpty()) {
818             m_properties.remove("force_progressive");
819             setProducerProperty("force_progressive", NULL);
820         } else setProducerProperty("force_progressive", value.toInt());
821     } else if (key == "threads") {
822         if (value.isEmpty()) {
823             m_properties.remove("threads");
824             setProducerProperty("threads", 1);
825         } else setProducerProperty("threads", value.toInt());
826     } else if (key == "video_index") {
827         if (value.isEmpty()) {
828             m_properties.remove("video_index");
829             setProducerProperty("video_index", m_properties.value("default_video").toInt());
830         } else setProducerProperty("video_index", value.toInt());
831     } else if (key == "audio_index") {
832         if (value.isEmpty()) {
833             m_properties.remove("audio_index");
834             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
835         } else setProducerProperty("audio_index", value.toInt());
836     }
837 }
838
839 QMap <QString, QString> DocClipBase::properties() const
840 {
841     return m_properties;
842 }
843
844 bool DocClipBase::slotGetAudioThumbs()
845 {
846     if (m_thumbProd == NULL) return false;
847     if (!KdenliveSettings::audiothumbnails() || m_audioTimer == NULL) {
848         if (m_audioTimer != NULL) m_audioTimer->stop();
849         return false;
850     }
851     if (m_audioThumbCreated) {
852         m_audioTimer->stop();
853         return false;
854     }
855     m_audioTimer->start(1500);
856     double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
857     m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
858     return true;
859 }
860
861 bool DocClipBase::isPlaceHolder() const
862 {
863     return m_placeHolder;
864 }
865
866 void DocClipBase::addCutZone(int in, int out, QString desc)
867 {
868     CutZoneInfo info;
869     info.zone = QPoint(in, out);
870     info.description = desc;
871     for (int i = 0; i < m_cutZones.count(); i++)
872         if (m_cutZones.at(i).zone == info.zone) {
873             return;
874         }
875     m_cutZones.append(info);
876 }
877
878 bool DocClipBase::hasCutZone(QPoint p) const
879 {
880     for (int i = 0; i < m_cutZones.count(); i++)
881         if (m_cutZones.at(i).zone == p) return true;
882     return false;
883 }
884
885
886 void DocClipBase::removeCutZone(int in, int out)
887 {
888     QPoint p(in, out);
889     for (int i = 0; i < m_cutZones.count(); i++) {
890         if (m_cutZones.at(i).zone == p) {
891             m_cutZones.removeAt(i);
892             i--;
893         }
894     }
895 }
896
897 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
898 {
899     QPoint old(oldin, oldout);
900     for (int i = 0; i < m_cutZones.size(); ++i) {
901         if (m_cutZones.at(i).zone == old) {
902             CutZoneInfo info;
903             info.zone = QPoint(in, out);
904             info.description = desc;
905             m_cutZones.replace(i, info);
906             break;
907         }
908     }
909 }
910
911 QList <CutZoneInfo> DocClipBase::cutZones() const
912 {
913     return m_cutZones;
914 }
915