]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
Cleanup some mess introduced by last commit, looking for missing slideshow clips...
[kdenlive] / src / docclipbase.cpp
1 /**************************1*************************************************
2                           DocClipBase.cpp  -  description
3                              -------------------
4     begin                : Fri Apr 12 2002
5     copyright            : (C) 2002 by Jason Wood
6     email                : jasonwood@blueyonder.co.uk
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include <QCryptographicHash>
19
20 #include <KDebug>
21
22 #include "kdenlivesettings.h"
23 #include "docclipbase.h"
24 #include "kthumb.h"
25 #include "clipmanager.h"
26
27 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id):
28         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 *>())  {
29     int type = xml.attribute("type").toInt();
30     m_clipType = (CLIPTYPE) type;
31     m_name = xml.attribute("name");
32
33     QDomNamedNodeMap attributes = xml.attributes();
34     for (unsigned int i = 0; i < attributes.count(); i++) {
35         m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
36     }
37
38     KUrl url = KUrl(xml.attribute("resource"));
39     if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
40     int out = xml.attribute("out").toInt();
41     if (out != 0) {
42         setDuration(GenTime(out, KdenliveSettings::project_fps()));
43     } else {
44         out = xml.attribute("duration").toInt();
45         if (out != 0) setDuration(GenTime(out, KdenliveSettings::project_fps()));
46     }
47     if (m_name.isEmpty()) m_name = url.fileName();
48
49     //if (!url.isEmpty() && QFile::exists(url.path()))
50     {
51         m_thumbProd = new KThumb(clipManager, url, m_id);
52         if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
53     }
54     //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
55 }
56
57 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
58     DocClipBase::operator=(clip);
59     m_id = clip.getId();
60     m_clipType = clip.clipType();
61     m_name = clip.name();
62     m_duration = clip.duration();
63     m_audioThumbCreated = clip.audioThumbCreated();
64     m_properties = clip.properties();
65     return *this;
66 }*/
67
68 DocClipBase::~DocClipBase() {
69     if (m_thumbProd) delete m_thumbProd;
70     qDeleteAll(m_baseTrackProducers);
71 }
72
73 void DocClipBase::slotCreateAudioTimer() {
74     connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
75     connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
76     m_audioTimer = new QTimer(this);
77     connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
78 }
79
80 void DocClipBase::askForAudioThumbs() {
81     if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
82 }
83
84 void DocClipBase::slotClearAudioCache() {
85     if (m_thumbProd) m_thumbProd->stopAudioThumbs();
86     if (m_audioTimer != NULL) m_audioTimer->stop();
87     audioFrameChache.clear();
88     m_audioThumbCreated = false;
89 }
90
91 /*void DocClipBase::getClipMainThumb() {
92     if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
93 }*/
94
95 KThumb *DocClipBase::thumbProducer() {
96     return m_thumbProd;
97 }
98
99 bool DocClipBase::audioThumbCreated() const {
100     return m_audioThumbCreated;
101 }
102
103 void DocClipBase::setName(const QString name) {
104     m_name = name;
105 }
106
107 const QString & DocClipBase::name() const {
108
109     return m_name;
110 }
111
112 const QString &DocClipBase::getId() const {
113     return m_id;
114 }
115
116 void DocClipBase::setId(const QString &newId) {
117     m_id = newId;
118 }
119
120 const CLIPTYPE & DocClipBase::clipType() const {
121     return m_clipType;
122 }
123
124 void DocClipBase::setClipType(CLIPTYPE type) {
125     m_clipType = type;
126
127     m_properties.insert("type", QString::number((int) type));
128     if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
129         slotCreateAudioTimer();
130 }
131
132 KUrl DocClipBase::fileURL() const {
133     QString res = m_properties.value("resource");
134     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
135     return KUrl();
136 }
137
138 void DocClipBase::setClipThumbFrame(const uint &ix) {
139     m_properties.insert("thumbnail", QString::number((int) ix));
140 }
141
142 uint DocClipBase::getClipThumbFrame() const {
143     return (uint) m_properties.value("thumbnail").toInt();
144 }
145
146 const QString DocClipBase::description() const {
147     return m_properties.value("description");
148 }
149
150 bool DocClipBase::isTransparent() const {
151     return (m_properties.value("transparency") == "1");
152 }
153
154 const QString DocClipBase::getProperty(const QString prop) const {
155     return m_properties.value(prop);
156 }
157
158 void DocClipBase::setDuration(GenTime dur) {
159     m_duration = dur;
160     m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
161 }
162
163 const GenTime &DocClipBase::duration() const {
164     return m_duration;
165 }
166
167 const GenTime &DocClipBase::maxDuration() const {
168     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
169         const GenTime dur(10000, KdenliveSettings::project_fps());
170         return dur;
171     }
172     return m_duration;
173 }
174
175 bool DocClipBase::hasFileSize() const {
176     return true;
177 }
178
179
180 // virtual
181 QDomElement DocClipBase::toXML() const {
182     QDomDocument doc;
183
184     QDomElement clip = doc.createElement("producer");
185
186     QMapIterator<QString, QString> i(m_properties);
187     while (i.hasNext()) {
188         i.next();
189         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
190     }
191     //doc.appendChild(clip);
192     //kDebug()<<"/// CLIP XML: "<<doc.toString();
193     return clip;
194 }
195
196 DocClipBase *DocClipBase::
197 createClip(KdenliveDoc *doc, const QDomElement & element) {
198     DocClipBase *clip = 0;
199     QString description;
200     QDomNode node = element;
201     node.normalize();
202     if (element.tagName() != "kdenliveclip") {
203         kWarning() <<
204         "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
205         return 0;
206     }
207
208     QDomNode n = element.firstChild();
209
210     while (!n.isNull()) {
211         QDomElement e = n.toElement();
212         if (!e.isNull()) {
213             QString tagName = e.tagName();
214             if (e.tagName() == "avfile") {
215                 // clip = DocClipAVFile::createClip(e);
216             } else if (e.tagName() == "DocTrackBaseList") {
217                 // clip = DocClipProject::createClip(doc, e);
218             }
219         } else {
220             QDomText text = n.toText();
221             if (!text.isNull()) {
222                 description = text.nodeValue();
223             }
224         }
225
226         n = n.nextSibling();
227     }
228     if (clip == 0) {
229         kWarning() << "DocClipBase::createClip() unable to create clip";
230     } else {
231         // setup DocClipBase specifics of the clip.
232         QMap <QString, QString> props;
233         props.insert("description", description);
234         clip->setProperties(props);
235         clip->setAudioThumbCreated(false);
236     }
237     return clip;
238 }
239
240 void DocClipBase::setAudioThumbCreated(bool isDone) {
241     m_audioThumbCreated = isDone;
242 }
243
244
245 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
246     m_thumbnail = pixmap;
247 }
248
249 const QPixmap & DocClipBase::thumbnail() const {
250     return m_thumbnail;
251 }
252
253 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
254     kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
255     audioFrameChache = data;
256     m_audioThumbCreated = true;
257     emit gotAudioData();
258 }
259
260 QList < GenTime > DocClipBase::snapMarkers() const {
261     QList < GenTime > markers;
262
263     for (uint count = 0; count < m_snapMarkers.count(); ++count) {
264         markers.append(m_snapMarkers[count].time());
265     }
266
267     return markers;
268 }
269
270 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
271     return m_snapMarkers;
272 }
273
274 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
275     m_snapMarkers = markers;
276 }
277
278 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
279     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
280     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
281         if ((*it).time() >= time)
282             break;
283     }
284
285     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
286         (*it).setComment(comment);
287         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
288     } else {
289         CommentedTime t(time, comment);
290         m_snapMarkers.insert(it, t);
291     }
292
293 }
294
295 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
296     QList < CommentedTime >::Iterator it;
297     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
298         if ((*it).time() == time)
299             break;
300     }
301     if (it != m_snapMarkers.end()) {
302         (*it).setComment(comment);
303     } else {
304         kError() << "trying to edit Snap Marker that does not already exists";
305     }
306 }
307
308 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
309     QString result = i18n("Marker");
310     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
311
312     while (itt != m_snapMarkers.end()) {
313         if ((*itt).time() == time)
314             break;
315         ++itt;
316     }
317
318     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
319         result = (*itt).comment();
320         m_snapMarkers.erase(itt);
321     }
322     return result;
323 }
324
325
326 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
327     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
328
329     while (itt != m_snapMarkers.end()) {
330         if ((*itt).time() == time)
331             return time;
332         ++itt;
333     }
334
335     return GenTime(0.0);
336 }
337
338 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
339     int it;
340     for (it = 0; it < m_snapMarkers.count(); it++) {
341         if (m_snapMarkers[it].time() >= currTime)
342             break;
343     }
344     if (it == 0) return GenTime();
345     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
346         return m_snapMarkers[it].time();
347     else return m_snapMarkers[it-1].time();
348 }
349
350 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
351     int it;
352     for (it = 0; it < m_snapMarkers.count(); it++) {
353         if (m_snapMarkers[it].time() > currTime)
354             break;
355     }
356     if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
357     return duration();
358 }
359
360 QString DocClipBase::markerComment(GenTime t) {
361     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
362
363     while (itt != m_snapMarkers.end()) {
364         if ((*itt).time() == t)
365             return (*itt).comment();
366         ++itt;
367     }
368     return QString::null;
369 }
370
371 void DocClipBase::setProducer(Mlt::Producer *producer) {
372     if (producer == NULL) return;
373     QString id = producer->get("id");
374     kDebug() << "// SET PRODUCER: " << id;
375     if (id.contains('_')) {
376         // this is a subtrack producer, insert it at correct place
377         int pos = id.section('_', 1, 1).toInt();
378         kDebug() << "// POS = " << pos << ", MAX: " << m_baseTrackProducers.count();
379         if (pos >= m_baseTrackProducers.count()) {
380             while (m_baseTrackProducers.count() - 1 < pos) {
381                 m_baseTrackProducers.append(NULL);
382             }
383         }
384         kDebug() << "// POS = " << pos << ", NEW MAX: " << m_baseTrackProducers.count();
385         if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
386     } else m_baseTrackProducers.append(producer);
387     //m_clipProducer = producer;
388     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
389     if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
390 }
391
392 Mlt::Producer *DocClipBase::producer(int track) {
393     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
394         if (m_baseTrackProducers.count() == 0) return NULL;
395         int i;
396         for (i = 0; i < m_baseTrackProducers.count(); i++)
397             if (m_baseTrackProducers.at(i) != NULL) break;
398         if (i < m_baseTrackProducers.count()) return m_baseTrackProducers.at(i);
399         return NULL;
400     }
401     if (track >= m_baseTrackProducers.count()) {
402         while (m_baseTrackProducers.count() - 1 < track) {
403             m_baseTrackProducers.append(NULL);
404         }
405     }
406     if (m_baseTrackProducers.at(track) == NULL) {
407         int i;
408         for (i = 0; i < m_baseTrackProducers.count(); i++)
409             if (m_baseTrackProducers.at(i) != NULL) break;
410         if (i >= m_baseTrackProducers.count()) return NULL;
411         m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
412         if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
413         if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
414         if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
415         if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
416         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
417         m_baseTrackProducers[track]->set("id", tmp);
418         delete[] tmp;
419     }
420     return m_baseTrackProducers.at(track);
421 }
422
423 void DocClipBase::setProducerProperty(const char *name, int data) {
424     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
425         if (m_baseTrackProducers.at(i) != NULL)
426             m_baseTrackProducers[i]->set(name, data);
427     }
428 }
429
430 void DocClipBase::setProducerProperty(const char *name, const char *data) {
431     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
432         if (m_baseTrackProducers.at(i) != NULL)
433             m_baseTrackProducers[i]->set(name, data);
434     }
435 }
436
437 void DocClipBase::slotRefreshProducer() {
438     if (m_baseTrackProducers.count() == 0) return;
439     kDebug() << "////////////   REFRESH CLIP !!!!!!!!!!!!!!!!";
440     if (m_clipType == SLIDESHOW) {
441         /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
442                Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
443                delete[] tmp;
444         delete m_clipProducer;
445         m_clipProducer = new Mlt::Producer(producer.get_producer());
446         if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
447         setProducerProperty("ttl", getProperty("ttl").toInt());
448         //m_clipProducer->set("id", getProperty("id"));
449         if (getProperty("fade") == "1") {
450             // we want a fade filter effect
451             kDebug() << "////////////   FADE WANTED";
452             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
453             int ct = 0;
454             Mlt::Filter *filter = clipService.filter(ct);
455             while (filter) {
456                 if (filter->get("mlt_service") == "luma") {
457                     break;
458                 }
459                 ct++;
460                 filter = clipService.filter(ct);
461             }
462
463             if (filter && filter->get("mlt_service") == "luma") {
464                 filter->set("period", getProperty("ttl").toInt() - 1);
465                 filter->set("luma.out", getProperty("luma_duration").toInt());
466                 QString resource = getProperty("luma_file");
467                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
468                 filter->set("luma.resource", tmp);
469                 delete[] tmp;
470                 if (getProperty("softness") != QString()) {
471                     int soft = getProperty("softness").toInt();
472                     filter->set("luma.softness", (double) soft / 100.0);
473                 }
474             } else {
475                 // filter does not exist, create it...
476                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
477                 filter->set("period", getProperty("ttl").toInt() - 1);
478                 filter->set("luma.out", getProperty("luma_duration").toInt());
479                 QString resource = getProperty("luma_file");
480                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
481                 filter->set("luma.resource", tmp);
482                 delete[] tmp;
483                 if (getProperty("softness") != QString()) {
484                     int soft = getProperty("softness").toInt();
485                     filter->set("luma.softness", (double) soft / 100.0);
486                 }
487                 clipService.attach(*filter);
488             }
489         } else {
490             kDebug() << "////////////   FADE NOT WANTED!!!";
491             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
492             int ct = 0;
493             Mlt::Filter *filter = clipService.filter(0);
494             while (filter) {
495                 if (filter->get("mlt_service") == "luma") {
496                     clipService.detach(*filter);
497                 } else ct++;
498                 filter = clipService.filter(ct);
499             }
500         }
501     }
502 }
503
504 void DocClipBase::setProperties(QMap <QString, QString> properties) {
505     // changing clip type is not allowed
506     properties.remove("type");
507     QMapIterator<QString, QString> i(properties);
508     bool refreshProducer = false;
509     QStringList keys;
510     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
511     while (i.hasNext()) {
512         i.next();
513         setProperty(i.key(), i.value());
514         if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
515     }
516     if (refreshProducer) slotRefreshProducer();
517 }
518
519 void DocClipBase::clearProperty(const QString &key) {
520     m_properties.remove(key);
521 }
522
523 void DocClipBase::getFileHash(const QString &url) {
524     if (m_clipType == SLIDESHOW) return;
525     QFile file(url);
526     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
527         QByteArray fileData;
528         QByteArray fileHash;
529         //kDebug() << "SETTING HASH of" << value;
530         m_properties.insert("file_size", QString::number(file.size()));
531         /*
532                * 1 MB = 1 second per 450 files (or faster)
533                * 10 MB = 9 seconds per 450 files (or faster)
534                */
535         if (file.size() > 1000000*2) {
536             fileData = file.read(1000000);
537             if (file.seek(file.size() - 1000000))
538                 fileData.append(file.readAll());
539         } else
540             fileData = file.readAll();
541         file.close();
542         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
543         m_properties.insert("file_hash", QString(fileHash.toHex()));
544         //kDebug() << file.fileName() << file.size() << fileHash.toHex();
545     }
546 }
547
548 void DocClipBase::setProperty(const QString &key, const QString &value) {
549     m_properties.insert(key, value);
550     if (key == "resource") {
551         m_thumbProd->updateClipUrl(KUrl(value));
552         getFileHash(value);
553     } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
554     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
555     else if (key == "colour") {
556         char *tmp = (char *) qstrdup(value.toUtf8().data());
557         setProducerProperty("colour", tmp);
558         delete[] tmp;
559     } else if (key == "xmldata") {
560         setProducerProperty("force_reload", 1);
561     } else if (key == "force_aspect_ratio") {
562         if (value.isEmpty()) {
563             m_properties.remove("force_aspect_ratio");
564             setProducerProperty("force_aspect_ratio", 0);
565         } else setProducerProperty("force_aspect_ratio", value.toDouble());
566     } else if (key == "threads") {
567         if (value.isEmpty()) {
568             m_properties.remove("threads");
569             setProducerProperty("threads", 1);
570         } else setProducerProperty("threads", value.toInt());
571     } else if (key == "video_index") {
572         if (value.isEmpty()) {
573             m_properties.remove("video_index");
574             setProducerProperty("video_index", m_properties.value("default_video").toInt());
575         } else setProducerProperty("video_index", value.toInt());
576     } else if (key == "audio_index") {
577         if (value.isEmpty()) {
578             m_properties.remove("audio_index");
579             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
580         } else setProducerProperty("audio_index", value.toInt());
581     }
582 }
583
584 QMap <QString, QString> DocClipBase::properties() const {
585     return m_properties;
586 }
587
588 bool DocClipBase::slotGetAudioThumbs() {
589     if (m_thumbProd == NULL) return false;
590     if (m_audioThumbCreated) {
591         if (m_audioTimer != NULL) m_audioTimer->stop();
592         return false;
593     }
594     if (m_audioTimer != NULL)
595         m_audioTimer->start(1500);
596     double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
597     m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
598     return true;
599 }
600
601
602