]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
Try to fix audio mixing bug ( http://www.kdenlive.org:80/mantis/view.php?id=228 )
[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_properties(QMap <QString, QString> ()), audioFrameChache(QMap<int, QMap<int, QByteArray> > ()), m_baseTrackProducers(QList <Mlt::Producer *>())  {
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     qDeleteAll(m_baseTrackProducers);
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
124     m_properties.insert("type", QString::number((int) type));
125     if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
126         slotCreateAudioTimer();
127 }
128
129 KUrl DocClipBase::fileURL() const {
130     QString res = m_properties.value("resource");
131     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
132     return KUrl();
133 }
134
135 void DocClipBase::setClipThumbFrame(const uint &ix) {
136     m_properties.insert("thumbnail", QString::number((int) ix));
137 }
138
139 uint DocClipBase::getClipThumbFrame() const {
140     return (uint) m_properties.value("thumbnail").toInt();
141 }
142
143 const QString DocClipBase::description() const {
144     return m_properties.value("description");
145 }
146
147 bool DocClipBase::isTransparent() const {
148     return (m_properties.value("transparency") == "1");
149 }
150
151 const QString DocClipBase::getProperty(const QString prop) const {
152     return m_properties.value(prop);
153 }
154
155 void DocClipBase::setDuration(GenTime dur) {
156     m_duration = dur;
157     m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
158 }
159
160 const GenTime &DocClipBase::duration() const {
161     return m_duration;
162 }
163
164 const GenTime &DocClipBase::maxDuration() const {
165     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
166         const GenTime dur(10000, KdenliveSettings::project_fps());
167         return dur;
168     }
169     return m_duration;
170 }
171
172 bool DocClipBase::hasFileSize() const {
173     return true;
174 }
175
176
177 // virtual
178 QDomElement DocClipBase::toXML() const {
179     QDomDocument doc;
180
181     QDomElement clip = doc.createElement("producer");
182
183     QMapIterator<QString, QString> i(m_properties);
184     while (i.hasNext()) {
185         i.next();
186         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
187     }
188     //doc.appendChild(clip);
189     //kDebug()<<"/// CLIP XML: "<<doc.toString();
190     return clip;
191 }
192
193 DocClipBase *DocClipBase::
194 createClip(KdenliveDoc *doc, const QDomElement & element) {
195     DocClipBase *clip = 0;
196     QString description;
197     QDomNode node = element;
198     node.normalize();
199     if (element.tagName() != "kdenliveclip") {
200         kWarning() <<
201         "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
202         return 0;
203     }
204
205     QDomNode n = element.firstChild();
206
207     while (!n.isNull()) {
208         QDomElement e = n.toElement();
209         if (!e.isNull()) {
210             QString tagName = e.tagName();
211             if (e.tagName() == "avfile") {
212                 // clip = DocClipAVFile::createClip(e);
213             } else if (e.tagName() == "DocTrackBaseList") {
214                 // clip = DocClipProject::createClip(doc, e);
215             }
216         } else {
217             QDomText text = n.toText();
218             if (!text.isNull()) {
219                 description = text.nodeValue();
220             }
221         }
222
223         n = n.nextSibling();
224     }
225     if (clip == 0) {
226         kWarning() << "DocClipBase::createClip() unable to create clip";
227     } else {
228         // setup DocClipBase specifics of the clip.
229         QMap <QString, QString> props;
230         props.insert("description", description);
231         clip->setProperties(props);
232         clip->setAudioThumbCreated(false);
233     }
234     return clip;
235 }
236
237 void DocClipBase::setAudioThumbCreated(bool isDone) {
238     m_audioThumbCreated = isDone;
239 }
240
241
242 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
243     m_thumbnail = pixmap;
244 }
245
246 const QPixmap & DocClipBase::thumbnail() const {
247     return m_thumbnail;
248 }
249
250 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
251     kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
252     audioFrameChache = data;
253     m_audioThumbCreated = true;
254     emit gotAudioData();
255 }
256
257 QList < GenTime > DocClipBase::snapMarkers() const {
258     QList < GenTime > markers;
259
260     for (uint count = 0; count < m_snapMarkers.count(); ++count) {
261         markers.append(m_snapMarkers[count].time());
262     }
263
264     return markers;
265 }
266
267 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
268     return m_snapMarkers;
269 }
270
271 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
272     m_snapMarkers = markers;
273 }
274
275 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
276     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
277     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
278         if ((*it).time() >= time)
279             break;
280     }
281
282     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
283         (*it).setComment(comment);
284         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
285     } else {
286         CommentedTime t(time, comment);
287         m_snapMarkers.insert(it, t);
288     }
289
290 }
291
292 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
293     QList < CommentedTime >::Iterator it;
294     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
295         if ((*it).time() == time)
296             break;
297     }
298     if (it != m_snapMarkers.end()) {
299         (*it).setComment(comment);
300     } else {
301         kError() << "trying to edit Snap Marker that does not already exists";
302     }
303 }
304
305 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
306     QString result = i18n("Marker");
307     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
308
309     while (itt != m_snapMarkers.end()) {
310         if ((*itt).time() == time)
311             break;
312         ++itt;
313     }
314
315     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
316         result = (*itt).comment();
317         m_snapMarkers.erase(itt);
318     }
319     return result;
320 }
321
322
323 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
324     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
325
326     while (itt != m_snapMarkers.end()) {
327         if ((*itt).time() == time)
328             return time;
329         ++itt;
330     }
331
332     return GenTime(0.0);
333 }
334
335 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
336     int it;
337     for (it = 0; it < m_snapMarkers.count(); it++) {
338         if (m_snapMarkers[it].time() >= currTime)
339             break;
340     }
341     if (it == 0) return GenTime();
342     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
343         return m_snapMarkers[it].time();
344     else return m_snapMarkers[it-1].time();
345 }
346
347 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
348     int it;
349     for (it = 0; it < m_snapMarkers.count(); it++) {
350         if (m_snapMarkers[it].time() > currTime)
351             break;
352     }
353     if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
354     return duration();
355 }
356
357 QString DocClipBase::markerComment(GenTime t) {
358     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
359
360     while (itt != m_snapMarkers.end()) {
361         if ((*itt).time() == t)
362             return (*itt).comment();
363         ++itt;
364     }
365     return QString::null;
366 }
367
368 void DocClipBase::setProducer(Mlt::Producer *producer) {
369     if (producer == NULL) return;
370     QString id = producer->get("id");
371     kDebug() << "// SET PRODUCER: " << id;
372     if (id.contains('_')) {
373         // this is a subtrack producer, insert it at correct place
374         int pos = id.section('_', 1, 1).toInt();
375         kDebug() << "// POS = " << pos << ", MAX: " << m_baseTrackProducers.count();
376         if (pos >= m_baseTrackProducers.count()) {
377             while (m_baseTrackProducers.count() - 1 < pos) {
378                 m_baseTrackProducers.append(NULL);
379             }
380         }
381         kDebug() << "// POS = " << pos << ", NEW MAX: " << m_baseTrackProducers.count();
382         if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
383     } else m_baseTrackProducers.append(producer);
384     //m_clipProducer = producer;
385     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
386     if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
387 }
388
389 Mlt::Producer *DocClipBase::producer(int track) {
390     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
391         if (m_baseTrackProducers.count() == 0) return NULL;
392         int i;
393         for (i = 0; i < m_baseTrackProducers.count(); i++)
394             if (m_baseTrackProducers.at(i) != NULL) break;
395         if (i < m_baseTrackProducers.count()) return m_baseTrackProducers.at(i);
396         return NULL;
397     }
398     if (track >= m_baseTrackProducers.count()) {
399         while (m_baseTrackProducers.count() - 1 < track) {
400             m_baseTrackProducers.append(NULL);
401         }
402     }
403     if (m_baseTrackProducers.at(track) == NULL) {
404         int i;
405         for (i = 0; i < m_baseTrackProducers.count(); i++)
406             if (m_baseTrackProducers.at(i) != NULL) break;
407         if (i >= m_baseTrackProducers.count()) return NULL;
408         m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
409         if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
410         if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
411         if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
412         if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
413         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
414         m_baseTrackProducers[track]->set("id", tmp);
415         delete[] tmp;
416     }
417     return m_baseTrackProducers.at(track);
418 }
419
420 void DocClipBase::setProducerProperty(const char *name, int data) {
421     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
422         if (m_baseTrackProducers.at(i) != NULL)
423             m_baseTrackProducers[i]->set(name, data);
424     }
425 }
426
427 void DocClipBase::setProducerProperty(const char *name, const char *data) {
428     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
429         if (m_baseTrackProducers.at(i) != NULL)
430             m_baseTrackProducers[i]->set(name, data);
431     }
432 }
433
434 void DocClipBase::slotRefreshProducer() {
435     if (m_baseTrackProducers.count() == 0) return;
436     kDebug() << "////////////   REFRESH CLIP !!!!!!!!!!!!!!!!";
437     if (m_clipType == SLIDESHOW) {
438         /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
439                Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
440                delete[] tmp;
441         delete m_clipProducer;
442         m_clipProducer = new Mlt::Producer(producer.get_producer());
443         if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
444         setProducerProperty("ttl", getProperty("ttl").toInt());
445         //m_clipProducer->set("id", getProperty("id"));
446         if (getProperty("fade") == "1") {
447             // we want a fade filter effect
448             kDebug() << "////////////   FADE WANTED";
449             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
450             int ct = 0;
451             Mlt::Filter *filter = clipService.filter(ct);
452             while (filter) {
453                 if (filter->get("mlt_service") == "luma") {
454                     break;
455                 }
456                 ct++;
457                 filter = clipService.filter(ct);
458             }
459
460             if (filter && filter->get("mlt_service") == "luma") {
461                 filter->set("period", getProperty("ttl").toInt() - 1);
462                 filter->set("luma.out", getProperty("luma_duration").toInt());
463                 QString resource = getProperty("luma_file");
464                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
465                 filter->set("luma.resource", tmp);
466                 delete[] tmp;
467                 if (getProperty("softness") != QString()) {
468                     int soft = getProperty("softness").toInt();
469                     filter->set("luma.softness", (double) soft / 100.0);
470                 }
471             } else {
472                 // filter does not exist, create it...
473                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
474                 filter->set("period", getProperty("ttl").toInt() - 1);
475                 filter->set("luma.out", getProperty("luma_duration").toInt());
476                 QString resource = getProperty("luma_file");
477                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
478                 filter->set("luma.resource", tmp);
479                 delete[] tmp;
480                 if (getProperty("softness") != QString()) {
481                     int soft = getProperty("softness").toInt();
482                     filter->set("luma.softness", (double) soft / 100.0);
483                 }
484                 clipService.attach(*filter);
485             }
486         } else {
487             kDebug() << "////////////   FADE NOT WANTED!!!";
488             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
489             int ct = 0;
490             Mlt::Filter *filter = clipService.filter(0);
491             while (filter) {
492                 if (filter->get("mlt_service") == "luma") {
493                     clipService.detach(*filter);
494                 } else ct++;
495                 filter = clipService.filter(ct);
496             }
497         }
498     }
499 }
500
501 void DocClipBase::setProperties(QMap <QString, QString> properties) {
502     // changing clip type is not allowed
503     properties.remove("type");
504     QMapIterator<QString, QString> i(properties);
505     bool refreshProducer = false;
506     QStringList keys;
507     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
508     while (i.hasNext()) {
509         i.next();
510         setProperty(i.key(), i.value());
511         if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
512     }
513     if (refreshProducer) slotRefreshProducer();
514 }
515
516 void DocClipBase::clearProperty(const QString &key) {
517     m_properties.remove(key);
518 }
519
520 void DocClipBase::setProperty(const QString &key, const QString &value) {
521     m_properties.insert(key, value);
522     if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
523     else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
524     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
525     else if (key == "colour") {
526         char *tmp = (char *) qstrdup(value.toUtf8().data());
527         setProducerProperty("colour", tmp);
528         delete[] tmp;
529     } else if (key == "xmldata") {
530         setProducerProperty("force_reload", 1);
531     } else if (key == "force_aspect_ratio") {
532         if (value.isEmpty()) {
533             m_properties.remove("force_aspect_ratio");
534             setProducerProperty("force_aspect_ratio", 0);
535         } else setProducerProperty("force_aspect_ratio", value.toDouble());
536     } else if (key == "threads") {
537         if (value.isEmpty()) {
538             m_properties.remove("threads");
539             setProducerProperty("threads", 1);
540         } else setProducerProperty("threads", value.toInt());
541     } else if (key == "video_index") {
542         if (value.isEmpty()) {
543             m_properties.remove("video_index");
544             setProducerProperty("video_index", m_properties.value("default_video").toInt());
545         } else setProducerProperty("video_index", value.toInt());
546     } else if (key == "audio_index") {
547         if (value.isEmpty()) {
548             m_properties.remove("audio_index");
549             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
550         } else setProducerProperty("audio_index", value.toInt());
551     }
552 }
553
554 QMap <QString, QString> DocClipBase::properties() const {
555     return m_properties;
556 }
557
558 bool DocClipBase::slotGetAudioThumbs() {
559     if (m_thumbProd == NULL) return false;
560     if (m_audioThumbCreated) {
561         if (m_audioTimer != NULL) m_audioTimer->stop();
562         return false;
563     }
564     if (m_audioTimer != NULL)
565         m_audioTimer->start(1500);
566     double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
567     m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
568     return true;
569 }
570
571
572