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