]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
* Allow to edit transparent background for images in group properties
[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         m_abortProxy(false)
58 {
59     int type = xml.attribute("type").toInt();
60     m_clipType = (CLIPTYPE) type;
61     if (m_placeHolder) xml.removeAttribute("placeholder");
62     QDomNamedNodeMap attributes = xml.attributes();
63     for (int i = 0; i < attributes.count(); i++) {
64         QString name = attributes.item(i).nodeName();
65         if (name.startsWith("meta.attr.")) {
66             m_metadata.insert(name.section('.', 2, 3), attributes.item(i).nodeValue());
67         } else m_properties.insert(name, attributes.item(i).nodeValue());
68     }
69
70     if (xml.hasAttribute("cutzones")) {
71         QStringList cuts = xml.attribute("cutzones").split(";", QString::SkipEmptyParts);
72         for (int i = 0; i < cuts.count(); i++) {
73             QString z = cuts.at(i);
74             addCutZone(z.section('-', 0, 0).toInt(), z.section('-', 1, 1).toInt(), z.section('-', 2, 2));
75         }
76     }
77
78     KUrl url = KUrl(xml.attribute("resource"));
79     if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
80
81     if (xml.hasAttribute("duration")) {
82         setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
83     } else {
84         int out = xml.attribute("out").toInt();
85         int in = xml.attribute("in").toInt();
86         setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
87     }
88
89     if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
90
91     //if (!url.isEmpty() && QFile::exists(url.path()))
92     {
93         m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
94         if (m_clipType == AV || m_clipType == AUDIO || m_clipType == PLAYLIST) slotCreateAudioTimer();
95     }
96     //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
97 }
98
99 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
100     DocClipBase::operator=(clip);
101     m_id = clip.getId();
102     m_clipType = clip.clipType();
103     m_name = clip.name();
104     m_duration = clip.duration();
105     m_audioThumbCreated = clip.audioThumbCreated();
106     m_properties = clip.properties();
107     return *this;
108 }*/
109
110 DocClipBase::~DocClipBase()
111 {
112     kDebug() << "CLIP " << m_id << " DELETED******************************";
113     delete m_thumbProd;
114     if (m_audioTimer) {
115         m_audioTimer->stop();
116         delete m_audioTimer;
117     }
118     /*kDebug() <<" * * *CNT "<<m_baseTrackProducers.count();
119     if (m_baseTrackProducers.count() > 0) kDebug()<<"YOYO: "<<m_baseTrackProducers.at(0)->get_out()<<", CUT: "<<m_baseTrackProducers.at(0)->is_cut();*/
120     qDeleteAll(m_baseTrackProducers);
121     m_baseTrackProducers.clear();
122     qDeleteAll(m_audioTrackProducers);
123     m_audioTrackProducers.clear();
124     delete m_videoOnlyProducer;
125     m_videoOnlyProducer = NULL;
126 }
127
128 void DocClipBase::setZone(QPoint zone)
129 {
130     m_properties.insert("zone_in", QString::number(zone.x()));
131     m_properties.insert("zone_out", QString::number(zone.y()));
132 }
133
134 QPoint DocClipBase::zone() const
135 {
136     QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
137     return zone;
138 }
139
140 void DocClipBase::slotCreateAudioTimer()
141 {
142     connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
143     m_audioTimer = new QTimer(this);
144     connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
145 }
146
147 void DocClipBase::askForAudioThumbs()
148 {
149     if (m_thumbProd && m_audioTimer) m_thumbProd->askForAudioThumbs(getId());
150 }
151
152 void DocClipBase::slotClearAudioCache()
153 {
154     if (m_thumbProd) m_thumbProd->stopAudioThumbs();
155     if (m_audioTimer != NULL) m_audioTimer->stop();
156     m_audioFrameCache.clear();
157     m_audioThumbCreated = false;
158 }
159
160 /*void DocClipBase::getClipMainThumb() {
161     if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
162 }*/
163
164 KThumb *DocClipBase::thumbProducer()
165 {
166     return m_thumbProd;
167 }
168
169 bool DocClipBase::audioThumbCreated() const
170 {
171     return m_audioThumbCreated;
172 }
173
174 const QString DocClipBase::name() const
175 {
176
177     return m_properties.value("name");
178 }
179
180 const QString &DocClipBase::getId() const
181 {
182     return m_id;
183 }
184
185 void DocClipBase::setId(const QString &newId)
186 {
187     m_id = newId;
188 }
189
190 const CLIPTYPE & DocClipBase::clipType() const
191 {
192     return m_clipType;
193 }
194
195 void DocClipBase::setClipType(CLIPTYPE type)
196 {
197     m_clipType = type;
198
199     m_properties.insert("type", QString::number((int) type));
200     if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO || m_clipType == PLAYLIST))
201         slotCreateAudioTimer();
202 }
203
204 KUrl DocClipBase::fileURL() const
205 {
206     QString res = m_properties.value("resource");
207     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
208     return KUrl();
209 }
210
211 void DocClipBase::setClipThumbFrame(const uint &ix)
212 {
213     m_properties.insert("thumbnail", QString::number((int) ix));
214 }
215
216 uint DocClipBase::getClipThumbFrame() const
217 {
218     return (uint) m_properties.value("thumbnail").toInt();
219 }
220
221 const QString DocClipBase::description() const
222 {
223     return m_properties.value("description");
224 }
225
226 bool DocClipBase::isTransparent() const
227 {
228     return (m_properties.value("transparency") == "1");
229 }
230
231 const QString DocClipBase::getProperty(const QString prop) const
232 {
233     return m_properties.value(prop);
234 }
235
236 void DocClipBase::setDuration(GenTime dur)
237 {
238     m_duration = dur;
239     m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
240 }
241
242 const GenTime &DocClipBase::duration() const
243 {
244     return m_duration;
245 }
246
247 const GenTime DocClipBase::maxDuration() const
248 {
249     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
250         /*const GenTime dur(15000, KdenliveSettings::project_fps());
251         return dur;*/
252         return GenTime();
253     }
254     return m_duration;
255 }
256
257 bool DocClipBase::hasFileSize() const
258 {
259     return true;
260 }
261
262 qulonglong DocClipBase::fileSize() const
263 {
264     return m_properties.value("file_size").toULongLong();
265 }
266
267 // virtual
268 QDomElement DocClipBase::toXML() const
269 {
270     QDomDocument doc;
271     QDomElement clip = doc.createElement("producer");
272
273     QMapIterator<QString, QString> i(m_properties);
274     while (i.hasNext()) {
275         i.next();
276         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
277     }
278     doc.appendChild(clip);
279     if (!m_cutZones.isEmpty()) {
280         QStringList cuts;
281         for (int i = 0; i < m_cutZones.size(); i++) {
282             CutZoneInfo info = m_cutZones.at(i);
283             cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
284         }
285         clip.setAttribute("cutzones", cuts.join(";"));
286     }
287     //kDebug() << "/// CLIP XML: " << doc.toString();
288     return doc.documentElement();
289 }
290
291
292 void DocClipBase::setAudioThumbCreated(bool isDone)
293 {
294     m_audioThumbCreated = isDone;
295 }
296
297
298 void DocClipBase::setThumbnail(const QPixmap & pixmap)
299 {
300     m_thumbnail = pixmap;
301 }
302
303 const QPixmap & DocClipBase::thumbnail() const
304 {
305     return m_thumbnail;
306 }
307
308 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data)
309 {
310     //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
311     m_audioFrameCache = data;
312     m_audioThumbCreated = true;
313     emit gotAudioData();
314 }
315
316 QList < GenTime > DocClipBase::snapMarkers() const
317 {
318     QList < GenTime > markers;
319
320     for (int count = 0; count < m_snapMarkers.count(); ++count) {
321         markers.append(m_snapMarkers.at(count).time());
322     }
323
324     return markers;
325 }
326
327 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
328 {
329     return m_snapMarkers;
330 }
331
332
333 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
334 {
335     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
336     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
337         if ((*it).time() >= time)
338             break;
339     }
340
341     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
342         (*it).setComment(comment);
343         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
344     } else {
345         CommentedTime t(time, comment);
346         m_snapMarkers.insert(it, t);
347     }
348
349 }
350
351 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
352 {
353     QList < CommentedTime >::Iterator it;
354     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
355         if ((*it).time() == time)
356             break;
357     }
358     if (it != m_snapMarkers.end()) {
359         (*it).setComment(comment);
360     } else {
361         kError() << "trying to edit Snap Marker that does not already exists";
362     }
363 }
364
365 QString DocClipBase::deleteSnapMarker(const GenTime & time)
366 {
367     QString result = i18n("Marker");
368     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
369
370     while (itt != m_snapMarkers.end()) {
371         if ((*itt).time() == time)
372             break;
373         ++itt;
374     }
375
376     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
377         result = (*itt).comment();
378         m_snapMarkers.erase(itt);
379     }
380     return result;
381 }
382
383
384 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
385 {
386     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
387
388     while (itt != m_snapMarkers.end()) {
389         if ((*itt).time() == time)
390             return time;
391         ++itt;
392     }
393
394     return GenTime(0.0);
395 }
396
397 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
398 {
399     int it;
400     for (it = 0; it < m_snapMarkers.count(); it++) {
401         if (m_snapMarkers.at(it).time() >= currTime)
402             break;
403     }
404     if (it == 0) return GenTime();
405     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
406         return m_snapMarkers.at(it).time();
407     else return m_snapMarkers.at(it - 1).time();
408 }
409
410 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
411 {
412     int it;
413     for (it = 0; it < m_snapMarkers.count(); it++) {
414         if (m_snapMarkers.at(it).time() > currTime)
415             break;
416     }
417     if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
418     return duration();
419 }
420
421 QString DocClipBase::markerComment(GenTime t)
422 {
423     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
424
425     while (itt != m_snapMarkers.end()) {
426         if ((*itt).time() == t)
427             return (*itt).comment();
428         ++itt;
429     }
430     return QString();
431 }
432
433 void DocClipBase::clearProducers()
434 {
435     m_baseTrackProducers.clear();
436 }
437
438 void DocClipBase::deleteProducers(bool clearThumbCreator)
439 {
440     kDebug() << "// CLIP KILL PRODS ct: " << m_baseTrackProducers.count();
441     if (clearThumbCreator && m_thumbProd) m_thumbProd->clearProducer();
442     /*kDebug()<<"// CLIP KILL PRODS ct: "<<m_baseTrackProducers.count();
443     int max = m_baseTrackProducers.count();
444     for (int i = 0; i < max; i++) {
445         kDebug()<<"// CLIP KILL PROD "<<i;
446     Mlt::Producer *p = m_baseTrackProducers.takeAt(i);
447     if (p != NULL) {
448      delete p;
449      p = NULL;
450     }
451     m_baseTrackProducers.insert(i, NULL);
452     }*/
453
454     delete m_videoOnlyProducer;
455     m_videoOnlyProducer = NULL;
456
457     qDeleteAll(m_baseTrackProducers);
458     m_baseTrackProducers.clear();
459     qDeleteAll(m_audioTrackProducers);
460     m_audioTrackProducers.clear();
461 }
462
463 void DocClipBase::setValid()
464 {
465     m_placeHolder = false;
466 }
467
468 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset)
469 {
470     if (producer == NULL || (m_placeHolder && !reset)) return;
471     if (m_thumbProd && (reset || !m_thumbProd->hasProducer())) m_thumbProd->setProducer(producer);
472     if (reset) {
473         // Clear all previous producers
474         kDebug() << "/+++++++++++++++   DELETE ALL PRODS " << producer->get("id");
475         deleteProducers(false);
476     }
477     QString id = producer->get("id");
478     if (id.contains('_')) {
479         // this is a subtrack producer, insert it at correct place
480         id = id.section('_', 1);
481         if (id.endsWith("audio")) {
482             int pos = id.section('_', 0, 0).toInt();
483             if (pos >= m_audioTrackProducers.count()) {
484                 while (m_audioTrackProducers.count() - 1 < pos) {
485                     m_audioTrackProducers.append(NULL);
486                 }
487             }
488             if (m_audioTrackProducers.at(pos) == NULL) m_audioTrackProducers[pos] = producer;
489             return;
490         } else if (id.endsWith("video")) {
491             m_videoOnlyProducer = producer;
492             return;
493         }
494         int pos = id.toInt();
495         if (pos >= m_baseTrackProducers.count()) {
496             while (m_baseTrackProducers.count() - 1 < pos) {
497                 m_baseTrackProducers.append(NULL);
498             }
499         }
500         if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
501     } else {
502         if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
503         else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
504     }
505     //m_clipProducer = producer;
506     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
507 }
508
509 static double getPixelAspect(QMap<QString, QString>& props) {
510     int width = props.value("frame_size").section('x', 0, 0).toInt();
511     int height = props.value("frame_size").section('x', 1, 1).toInt();
512     int aspectNumerator = props.value("force_aspect_num").toInt();
513     int aspectDenominator = props.value("force_aspect_den").toInt();
514     if (aspectDenominator != 0 && width != 0)
515         return double(height) * aspectNumerator / aspectDenominator / width;    
516     else
517         return 1.0;
518 }
519
520 Mlt::Producer *DocClipBase::audioProducer(int track)
521 {
522     if (m_audioTrackProducers.count() <= track) {
523         while (m_audioTrackProducers.count() - 1 < track) {
524             m_audioTrackProducers.append(NULL);
525         }
526     }
527     if (m_audioTrackProducers.at(track) == NULL) {
528         Mlt::Producer *base = producer();
529         m_audioTrackProducers[track] = new Mlt::Producer(*(base->profile()), base->get("resource"));
530         if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
531             m_audioTrackProducers.at(track)->set("force_aspect_ratio", getPixelAspect(m_properties));
532         if (m_properties.contains("force_fps")) m_audioTrackProducers.at(track)->set("force_fps", m_properties.value("force_fps").toDouble());
533         if (m_properties.contains("force_progressive")) m_audioTrackProducers.at(track)->set("force_progressive", m_properties.value("force_progressive").toInt());
534         if (m_properties.contains("force_tff")) m_audioTrackProducers.at(track)->set("force_tff", m_properties.value("force_tff").toInt());
535         if (m_properties.contains("threads")) m_audioTrackProducers.at(track)->set("threads", m_properties.value("threads").toInt());
536         m_audioTrackProducers.at(track)->set("video_index", -1);
537         if (m_properties.contains("audio_index")) m_audioTrackProducers.at(track)->set("audio_index", m_properties.value("audio_index").toInt());
538         m_audioTrackProducers.at(track)->set("id", QString(getId() + '_' + QString::number(track) + "_audio").toUtf8().data());
539         if (m_properties.contains("force_colorspace")) m_audioTrackProducers.at(track)->set("force_colorspace", m_properties.value("force_colorspace").toInt());
540         if (m_properties.contains("full_luma")) m_audioTrackProducers.at(track)->set("set.force_full_luma", m_properties.value("full_luma").toInt());
541     }
542     return m_audioTrackProducers.at(track);
543 }
544
545 Mlt::Producer *DocClipBase::videoProducer()
546 {
547     if (m_videoOnlyProducer == NULL) {
548         int i;
549         for (i = 0; i < m_baseTrackProducers.count(); i++)
550             if (m_baseTrackProducers.at(i) != NULL) break;
551         if (i >= m_baseTrackProducers.count()) return NULL;
552         m_videoOnlyProducer = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
553         if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
554             m_videoOnlyProducer->set("force_aspect_ratio", getPixelAspect(m_properties));
555         if (m_properties.contains("force_fps")) m_videoOnlyProducer->set("force_fps", m_properties.value("force_fps").toDouble());
556         if (m_properties.contains("force_progressive")) m_videoOnlyProducer->set("force_progressive", m_properties.value("force_progressive").toInt());
557         if (m_properties.contains("force_tff")) m_videoOnlyProducer->set("force_tff", m_properties.value("force_tff").toInt());
558         if (m_properties.contains("threads")) m_videoOnlyProducer->set("threads", m_properties.value("threads").toInt());
559         m_videoOnlyProducer->set("audio_index", -1);
560         if (m_properties.contains("video_index")) m_videoOnlyProducer->set("video_index", m_properties.value("video_index").toInt());
561         m_videoOnlyProducer->set("id", QString(getId() + "_video").toUtf8().data());
562         if (m_properties.contains("force_colorspace")) m_videoOnlyProducer->set("force_colorspace", m_properties.value("force_colorspace").toInt());
563         if (m_properties.contains("full_luma")) m_videoOnlyProducer->set("set.force_full_luma", m_properties.value("full_luma").toInt());
564     }
565     return m_videoOnlyProducer;
566 }
567
568 Mlt::Producer *DocClipBase::producer(int track)
569 {
570     /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
571         if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
572     }*/
573     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
574         if (m_baseTrackProducers.count() == 0) return NULL;
575         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
576             if (m_baseTrackProducers.at(i) != NULL)
577                 return m_baseTrackProducers.at(i);
578         }
579         return NULL;
580     }
581     if (track >= m_baseTrackProducers.count()) {
582         while (m_baseTrackProducers.count() - 1 < track) {
583             m_baseTrackProducers.append(NULL);
584         }
585     }
586     if (m_baseTrackProducers.at(track) == NULL) {
587         int i;
588         for (i = 0; i < m_baseTrackProducers.count(); i++)
589             if (m_baseTrackProducers.at(i) != NULL) break;
590
591         if (i >= m_baseTrackProducers.count()) return NULL;
592
593         if (KIO::NetAccess::exists(KUrl(m_baseTrackProducers.at(i)->get("resource")), KIO::NetAccess::SourceSide, 0))
594             m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
595         else { // special case for placeholder clips
596             m_baseTrackProducers[track] = NULL;
597             return NULL;
598         }
599         if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
600             m_baseTrackProducers[track]->set("force_aspect_ratio", getPixelAspect(m_properties));
601         if (m_properties.contains("force_fps")) m_baseTrackProducers[track]->set("force_fps", m_properties.value("force_fps").toDouble());
602         if (m_properties.contains("force_progressive")) m_baseTrackProducers[track]->set("force_progressive", m_properties.value("force_progressive").toInt());
603         if (m_properties.contains("force_tff")) m_baseTrackProducers[track]->set("force_tff", m_properties.value("force_tff").toInt());
604         if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
605         if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
606         if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
607         m_baseTrackProducers[track]->set("id", QString(getId() + '_' + QString::number(track)).toUtf8().data());
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     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, QString params)
1085 {
1086     if (m_proxyThread.isRunning()) return;
1087     QStringList parameters;
1088     parameters << "-i" << m_properties.value("resource");
1089     foreach(QString s, params.split(' '))
1090     parameters << s;
1091     // Make sure we don't block when proxy file already exists
1092     parameters << "-y";
1093     if (m_properties.value("file_hash").isEmpty()) getFileHash(m_properties.value("resource"));
1094     QString proxydir=proxyFolder.path( KUrl::AddTrailingSlash) + "proxy/";
1095     KStandardDirs::makeDir(proxydir);
1096     QString path = proxydir + m_properties.value("file_hash") + ".avi";
1097     setProperty("proxy", path.toUtf8().data());
1098     if (QFile::exists(path)) {
1099         emit proxyReady(m_id, true);
1100         return;
1101     }
1102     parameters << path;
1103     m_proxyThread = QtConcurrent::run(this, &DocClipBase::slotGenerateProxy, parameters);
1104 }
1105
1106 void DocClipBase::slotGenerateProxy(QStringList parameters)
1107 {
1108     QProcess myProcess;
1109     myProcess.start("ffmpeg", parameters);
1110     myProcess.waitForStarted();
1111     int result = -1;
1112     while (myProcess.state() != QProcess::NotRunning) {
1113         // building proxy file
1114         if (m_abortProxy) {
1115             QString path = getProperty("proxy");
1116             myProcess.close();
1117             myProcess.waitForFinished();
1118             QFile::remove(path);
1119             m_abortProxy = false;
1120             result = 1;
1121         }
1122         myProcess.waitForFinished(500);
1123     }
1124     myProcess.waitForFinished();
1125     if (result == -1) result = myProcess.exitStatus();
1126     if (result == 0) emit proxyReady(m_id, true);
1127     else {
1128         clearProperty("proxy");
1129         emit proxyReady(m_id, false);
1130     }
1131 }
1132
1133 void DocClipBase::abortProxy()
1134 {
1135     if (m_proxyThread.isRunning()) {
1136         // Proxy is being created, abort
1137         m_abortProxy = true;
1138     }
1139     else {
1140         clearProperty("proxy");
1141         emit proxyReady(m_id, false);
1142     }
1143 }
1144