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