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