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