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