]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
New: allow user to modifiy a clip by saying if he wants to keep only video or 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
32 #include <KDebug>
33
34 #include <QCryptographicHash>
35
36 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id):
37         m_id(id), m_description(QString()), m_refcount(0), m_audioThumbCreated(false), m_duration(GenTime()), m_thumbProd(NULL), m_audioTimer(NULL), m_properties(QMap <QString, QString> ()), audioFrameChache(QMap<int, QMap<int, QByteArray> > ()), m_baseTrackProducers(QList <Mlt::Producer *>()), m_snapMarkers(QList < CommentedTime > ()), m_videoOnlyProducer(NULL), m_audioTrackProducers(QList <Mlt::Producer *>())  {
38     int type = xml.attribute("type").toInt();
39     m_clipType = (CLIPTYPE) type;
40
41     QDomNamedNodeMap attributes = xml.attributes();
42     for (unsigned int i = 0; i < attributes.count(); i++) {
43         m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
44     }
45
46     KUrl url = KUrl(xml.attribute("resource"));
47     if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
48
49     if (xml.hasAttribute("duration")) {
50         setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
51     } else {
52         int out = xml.attribute("out").toInt();
53         int in = xml.attribute("in").toInt();
54         setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
55     }
56
57     if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
58
59     //if (!url.isEmpty() && QFile::exists(url.path()))
60     {
61         m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
62         if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
63     }
64     //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
65 }
66
67 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
68     DocClipBase::operator=(clip);
69     m_id = clip.getId();
70     m_clipType = clip.clipType();
71     m_name = clip.name();
72     m_duration = clip.duration();
73     m_audioThumbCreated = clip.audioThumbCreated();
74     m_properties = clip.properties();
75     return *this;
76 }*/
77
78 DocClipBase::~DocClipBase() {
79     if (m_thumbProd) {
80         delete m_thumbProd;
81     }
82     if (m_audioTimer) {
83         m_audioTimer->stop();
84         delete m_audioTimer;
85     }
86     qDeleteAll(m_baseTrackProducers);
87     m_baseTrackProducers.clear();
88 }
89
90 void DocClipBase::setZone(QPoint zone) {
91     m_properties.insert("zone_in", QString::number(zone.x()));
92     m_properties.insert("zone_out", QString::number(zone.y()));
93 }
94
95 QPoint DocClipBase::zone() const {
96     QPoint zone;
97     zone.setX(m_properties.value("zone_in").toInt());
98     zone.setY(m_properties.value("zone_out", "50").toInt());
99     return zone;
100 }
101
102 void DocClipBase::slotCreateAudioTimer() {
103     connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
104     connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
105     m_audioTimer = new QTimer(this);
106     connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
107 }
108
109 void DocClipBase::askForAudioThumbs() {
110     if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
111 }
112
113 void DocClipBase::slotClearAudioCache() {
114     if (m_thumbProd) m_thumbProd->stopAudioThumbs();
115     if (m_audioTimer != NULL) m_audioTimer->stop();
116     audioFrameChache.clear();
117     m_audioThumbCreated = false;
118 }
119
120 /*void DocClipBase::getClipMainThumb() {
121     if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
122 }*/
123
124 KThumb *DocClipBase::thumbProducer() {
125     return m_thumbProd;
126 }
127
128 bool DocClipBase::audioThumbCreated() const {
129     return m_audioThumbCreated;
130 }
131
132 const QString DocClipBase::name() const {
133
134     return m_properties.value("name");
135 }
136
137 const QString &DocClipBase::getId() const {
138     return m_id;
139 }
140
141 void DocClipBase::setId(const QString &newId) {
142     m_id = newId;
143 }
144
145 const CLIPTYPE & DocClipBase::clipType() const {
146     return m_clipType;
147 }
148
149 void DocClipBase::setClipType(CLIPTYPE type) {
150     m_clipType = type;
151
152     m_properties.insert("type", QString::number((int) type));
153     if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
154         slotCreateAudioTimer();
155 }
156
157 KUrl DocClipBase::fileURL() const {
158     QString res = m_properties.value("resource");
159     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
160     return KUrl();
161 }
162
163 void DocClipBase::setClipThumbFrame(const uint &ix) {
164     m_properties.insert("thumbnail", QString::number((int) ix));
165 }
166
167 uint DocClipBase::getClipThumbFrame() const {
168     return (uint) m_properties.value("thumbnail").toInt();
169 }
170
171 const QString DocClipBase::description() const {
172     return m_properties.value("description");
173 }
174
175 bool DocClipBase::isTransparent() const {
176     return (m_properties.value("transparency") == "1");
177 }
178
179 const QString DocClipBase::getProperty(const QString prop) const {
180     return m_properties.value(prop);
181 }
182
183 void DocClipBase::setDuration(GenTime dur) {
184     m_duration = dur;
185     m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
186 }
187
188 const GenTime &DocClipBase::duration() const {
189     return m_duration;
190 }
191
192 const GenTime DocClipBase::maxDuration() const {
193     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
194         const GenTime dur(15000, KdenliveSettings::project_fps());
195         return dur;
196     }
197     return m_duration;
198 }
199
200 bool DocClipBase::hasFileSize() const {
201     return true;
202 }
203
204
205 // virtual
206 QDomElement DocClipBase::toXML() const {
207     QDomDocument doc;
208     QDomElement clip = doc.createElement("producer");
209
210     QMapIterator<QString, QString> i(m_properties);
211     while (i.hasNext()) {
212         i.next();
213         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
214     }
215     doc.appendChild(clip);
216     //kDebug()<<"/// CLIP XML: "<<doc.toString();
217     return doc.documentElement();
218 }
219
220
221 void DocClipBase::setAudioThumbCreated(bool isDone) {
222     m_audioThumbCreated = isDone;
223 }
224
225
226 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
227     m_thumbnail = pixmap;
228 }
229
230 const QPixmap & DocClipBase::thumbnail() const {
231     return m_thumbnail;
232 }
233
234 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
235     //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
236     audioFrameChache = data;
237     m_audioThumbCreated = true;
238     emit gotAudioData();
239 }
240
241 QList < GenTime > DocClipBase::snapMarkers() const {
242     QList < GenTime > markers;
243
244     for (uint count = 0; count < m_snapMarkers.count(); ++count) {
245         markers.append(m_snapMarkers[count].time());
246     }
247
248     return markers;
249 }
250
251 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
252     return m_snapMarkers;
253 }
254
255 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
256     m_snapMarkers = markers;
257 }
258
259 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
260     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
261     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
262         if ((*it).time() >= time)
263             break;
264     }
265
266     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
267         (*it).setComment(comment);
268         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
269     } else {
270         CommentedTime t(time, comment);
271         m_snapMarkers.insert(it, t);
272     }
273
274 }
275
276 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
277     QList < CommentedTime >::Iterator it;
278     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
279         if ((*it).time() == time)
280             break;
281     }
282     if (it != m_snapMarkers.end()) {
283         (*it).setComment(comment);
284     } else {
285         kError() << "trying to edit Snap Marker that does not already exists";
286     }
287 }
288
289 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
290     QString result = i18n("Marker");
291     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
292
293     while (itt != m_snapMarkers.end()) {
294         if ((*itt).time() == time)
295             break;
296         ++itt;
297     }
298
299     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
300         result = (*itt).comment();
301         m_snapMarkers.erase(itt);
302     }
303     return result;
304 }
305
306
307 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
308     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
309
310     while (itt != m_snapMarkers.end()) {
311         if ((*itt).time() == time)
312             return time;
313         ++itt;
314     }
315
316     return GenTime(0.0);
317 }
318
319 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
320     int it;
321     for (it = 0; it < m_snapMarkers.count(); it++) {
322         if (m_snapMarkers[it].time() >= currTime)
323             break;
324     }
325     if (it == 0) return GenTime();
326     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
327         return m_snapMarkers[it].time();
328     else return m_snapMarkers[it-1].time();
329 }
330
331 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
332     int it;
333     for (it = 0; it < m_snapMarkers.count(); it++) {
334         if (m_snapMarkers[it].time() > currTime)
335             break;
336     }
337     if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
338     return duration();
339 }
340
341 QString DocClipBase::markerComment(GenTime t) {
342     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
343
344     while (itt != m_snapMarkers.end()) {
345         if ((*itt).time() == t)
346             return (*itt).comment();
347         ++itt;
348     }
349     return QString();
350 }
351
352 void DocClipBase::deleteProducers() {
353     qDeleteAll(m_baseTrackProducers);
354     m_baseTrackProducers.clear();
355     if (m_thumbProd) m_thumbProd->clearProducer();
356 }
357
358 void DocClipBase::setProducer(Mlt::Producer *producer) {
359     if (producer == NULL) return;
360     QString id = producer->get("id");
361     if (id.contains('_')) {
362         // this is a subtrack producer, insert it at correct place
363         id = id.section('_', 1);
364         if (id.endsWith("audio")) {
365             int pos = id.section('_', 0, 0).toInt();
366             if (pos >= m_audioTrackProducers.count()) {
367                 while (m_audioTrackProducers.count() - 1 < pos) {
368                     m_audioTrackProducers.append(NULL);
369                 }
370             }
371             if (m_audioTrackProducers.at(pos) == NULL) m_audioTrackProducers[pos] = producer;
372             return;
373         }
374         if (id.endsWith("video")) {
375             m_videoOnlyProducer = producer;
376             return;
377         }
378         int pos = id.toInt();
379         if (pos >= m_baseTrackProducers.count()) {
380             while (m_baseTrackProducers.count() - 1 < pos) {
381                 m_baseTrackProducers.append(NULL);
382             }
383         }
384         if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
385     } else {
386         if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
387         else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
388     }
389     //m_clipProducer = producer;
390     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
391     if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
392 }
393
394 Mlt::Producer *DocClipBase::audioProducer(int track) {
395     if (m_audioTrackProducers.count() <= track) {
396         while (m_audioTrackProducers.count() - 1 < track) {
397             m_audioTrackProducers.append(NULL);
398         }
399     }
400     if (m_audioTrackProducers.at(track) == NULL) {
401         Mlt::Producer *base = producer();
402         m_audioTrackProducers[track] = new Mlt::Producer(*(base->profile()), base->get("resource"));
403         if (m_properties.contains("force_aspect_ratio")) m_audioTrackProducers.at(track)->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
404         if (m_properties.contains("threads")) m_audioTrackProducers.at(track)->set("threads", m_properties.value("threads").toInt());
405         m_audioTrackProducers.at(track)->set("video_index", -1);
406         if (m_properties.contains("audio_index")) m_audioTrackProducers.at(track)->set("audio_index", m_properties.value("audio_index").toInt());
407         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track) + "_audio").toUtf8().data());
408         m_audioTrackProducers.at(track)->set("id", tmp);
409         delete[] tmp;
410     }
411     return m_audioTrackProducers.at(track);
412 }
413
414 Mlt::Producer *DocClipBase::videoProducer() {
415     if (m_videoOnlyProducer == NULL) {
416         int i;
417         for (i = 0; i < m_baseTrackProducers.count(); i++)
418             if (m_baseTrackProducers.at(i) != NULL) break;
419         if (i >= m_baseTrackProducers.count()) return NULL;
420         m_videoOnlyProducer = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
421         if (m_properties.contains("force_aspect_ratio")) m_videoOnlyProducer->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
422         if (m_properties.contains("threads")) m_videoOnlyProducer->set("threads", m_properties.value("threads").toInt());
423         m_videoOnlyProducer->set("audio_index", -1);
424         if (m_properties.contains("video_index")) m_videoOnlyProducer->set("video_index", m_properties.value("video_index").toInt());
425         char *tmp = (char *) qstrdup(QString(getId() + "_video").toUtf8().data());
426         m_videoOnlyProducer->set("id", tmp);
427         delete[] tmp;
428     }
429     return m_videoOnlyProducer;
430 }
431
432 Mlt::Producer *DocClipBase::producer(int track) {
433     /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
434         if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
435     }*/
436     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
437         if (m_baseTrackProducers.count() == 0) return NULL;
438         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
439             if (m_baseTrackProducers.at(i) != NULL)
440                 return m_baseTrackProducers.at(i);
441         }
442         return NULL;
443     }
444     if (track >= m_baseTrackProducers.count()) {
445         while (m_baseTrackProducers.count() - 1 < track) {
446             m_baseTrackProducers.append(NULL);
447         }
448     }
449     if (m_baseTrackProducers.at(track) == NULL) {
450         int i;
451         for (i = 0; i < m_baseTrackProducers.count(); i++)
452             if (m_baseTrackProducers.at(i) != NULL) break;
453         if (i >= m_baseTrackProducers.count()) return NULL;
454         m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
455         if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
456         if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
457         if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
458         if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
459         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
460         m_baseTrackProducers[track]->set("id", tmp);
461         delete[] tmp;
462         if (KdenliveSettings::dropbframes() && m_baseTrackProducers.at(i)->get("skip_loop_filter") && strcmp(m_baseTrackProducers.at(i)->get("skip_loop_filter"), "all") == 0) {
463             m_baseTrackProducers[track]->set("skip_loop_filter", "all");
464             m_baseTrackProducers[track]->set("skip_frame", "bidir");
465         }
466     }
467     return m_baseTrackProducers.at(track);
468 }
469
470 void DocClipBase::setProducerProperty(const char *name, int data) {
471     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
472         if (m_baseTrackProducers.at(i) != NULL)
473             m_baseTrackProducers[i]->set(name, data);
474     }
475 }
476
477 void DocClipBase::setProducerProperty(const char *name, const char *data) {
478     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
479         if (m_baseTrackProducers.at(i) != NULL)
480             m_baseTrackProducers[i]->set(name, data);
481     }
482 }
483
484 const char *DocClipBase::producerProperty(const char *name) const {
485     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
486         if (m_baseTrackProducers.at(i) != NULL) {
487             return m_baseTrackProducers.at(i)->get(name);
488         }
489     }
490     return NULL;
491 }
492
493
494 void DocClipBase::slotRefreshProducer() {
495     if (m_baseTrackProducers.count() == 0) return;
496     kDebug() << "////////////   REFRESH CLIP !!!!!!!!!!!!!!!!";
497     if (m_clipType == SLIDESHOW) {
498         /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
499                Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
500                delete[] tmp;
501         delete m_clipProducer;
502         m_clipProducer = new Mlt::Producer(producer.get_producer());
503         if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
504         setProducerProperty("ttl", getProperty("ttl").toInt());
505         //m_clipProducer->set("id", getProperty("id"));
506         if (getProperty("fade") == "1") {
507             // we want a fade filter effect
508             kDebug() << "////////////   FADE WANTED";
509             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
510             int ct = 0;
511             Mlt::Filter *filter = clipService.filter(ct);
512             while (filter) {
513                 if (filter->get("mlt_service") == "luma") {
514                     break;
515                 }
516                 ct++;
517                 filter = clipService.filter(ct);
518             }
519
520             if (filter && filter->get("mlt_service") == "luma") {
521                 filter->set("period", getProperty("ttl").toInt() - 1);
522                 filter->set("luma.out", getProperty("luma_duration").toInt());
523                 QString resource = getProperty("luma_file");
524                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
525                 filter->set("luma.resource", tmp);
526                 delete[] tmp;
527                 if (!getProperty("softness").isEmpty()) {
528                     int soft = getProperty("softness").toInt();
529                     filter->set("luma.softness", (double) soft / 100.0);
530                 }
531             } else {
532                 // filter does not exist, create it...
533                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
534                 filter->set("period", getProperty("ttl").toInt() - 1);
535                 filter->set("luma.out", getProperty("luma_duration").toInt());
536                 QString resource = getProperty("luma_file");
537                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
538                 filter->set("luma.resource", tmp);
539                 delete[] tmp;
540                 if (!getProperty("softness").isEmpty()) {
541                     int soft = getProperty("softness").toInt();
542                     filter->set("luma.softness", (double) soft / 100.0);
543                 }
544                 clipService.attach(*filter);
545             }
546         } else {
547             kDebug() << "////////////   FADE NOT WANTED!!!";
548             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
549             int ct = 0;
550             Mlt::Filter *filter = clipService.filter(0);
551             while (filter) {
552                 if (filter->get("mlt_service") == "luma") {
553                     clipService.detach(*filter);
554                 } else ct++;
555                 filter = clipService.filter(ct);
556             }
557         }
558     }
559 }
560
561 void DocClipBase::setProperties(QMap <QString, QString> properties) {
562     // changing clip type is not allowed
563     properties.remove("type");
564     QMapIterator<QString, QString> i(properties);
565     bool refreshProducer = false;
566     QStringList keys;
567     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
568     while (i.hasNext()) {
569         i.next();
570         setProperty(i.key(), i.value());
571         if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
572     }
573     if (refreshProducer) slotRefreshProducer();
574 }
575
576 void DocClipBase::setMetadata(QMap <QString, QString> properties) {
577     m_metadata = properties;
578 }
579
580 QMap <QString, QString> DocClipBase::metadata() const {
581     return m_metadata;
582 }
583
584 void DocClipBase::clearProperty(const QString &key) {
585     m_properties.remove(key);
586 }
587
588 void DocClipBase::getFileHash(const QString &url) {
589     if (m_clipType == SLIDESHOW) return;
590     QFile file(url);
591     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
592         QByteArray fileData;
593         QByteArray fileHash;
594         //kDebug() << "SETTING HASH of" << value;
595         m_properties.insert("file_size", QString::number(file.size()));
596         /*
597                * 1 MB = 1 second per 450 files (or faster)
598                * 10 MB = 9 seconds per 450 files (or faster)
599                */
600         if (file.size() > 1000000*2) {
601             fileData = file.read(1000000);
602             if (file.seek(file.size() - 1000000))
603                 fileData.append(file.readAll());
604         } else
605             fileData = file.readAll();
606         file.close();
607         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
608         m_properties.insert("file_hash", QString(fileHash.toHex()));
609         //kDebug() << file.fileName() << file.size() << fileHash.toHex();
610     }
611 }
612
613 QString DocClipBase::getClipHash() const {
614     QString hash;
615     if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
616     else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
617     else hash = m_properties.value("file_hash");
618     return hash;
619 }
620
621 void DocClipBase::refreshThumbUrl() {
622     if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
623 }
624
625 void DocClipBase::setProperty(const QString &key, const QString &value) {
626     m_properties.insert(key, value);
627     if (key == "resource") {
628         getFileHash(value);
629         if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
630     } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
631     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
632     else if (key == "colour") {
633         char *tmp = (char *) qstrdup(value.toUtf8().data());
634         setProducerProperty("colour", tmp);
635         delete[] tmp;
636     } else if (key == "xmldata") {
637         setProducerProperty("force_reload", 1);
638     } else if (key == "force_aspect_ratio") {
639         if (value.isEmpty()) {
640             m_properties.remove("force_aspect_ratio");
641             setProducerProperty("force_aspect_ratio", 0);
642         } else setProducerProperty("force_aspect_ratio", value.toDouble());
643     } else if (key == "threads") {
644         if (value.isEmpty()) {
645             m_properties.remove("threads");
646             setProducerProperty("threads", 1);
647         } else setProducerProperty("threads", value.toInt());
648     } else if (key == "video_index") {
649         if (value.isEmpty()) {
650             m_properties.remove("video_index");
651             setProducerProperty("video_index", m_properties.value("default_video").toInt());
652         } else setProducerProperty("video_index", value.toInt());
653     } else if (key == "audio_index") {
654         if (value.isEmpty()) {
655             m_properties.remove("audio_index");
656             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
657         } else setProducerProperty("audio_index", value.toInt());
658     }
659 }
660
661 QMap <QString, QString> DocClipBase::properties() const {
662     return m_properties;
663 }
664
665 bool DocClipBase::slotGetAudioThumbs() {
666     if (m_thumbProd == NULL) return false;
667     if (!KdenliveSettings::audiothumbnails()) {
668         if (m_audioTimer != NULL) m_audioTimer->stop();
669         return false;
670     }
671     if (m_audioThumbCreated) {
672         if (m_audioTimer != NULL) m_audioTimer->stop();
673         return false;
674     }
675     if (m_audioTimer != NULL)
676         m_audioTimer->start(1500);
677     double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
678     m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
679     return true;
680 }
681
682
683