1 /***************************************************************************
2 krender.cpp - description
4 begin : Fri Nov 22 2002
5 copyright : (C) 2002 by Jason Wood
6 email : jasonwood@blueyonder.co.uk
7 copyright : (C) 2005 Lucio Flavio Correa
8 email : lucio.correa@gmail.com
9 copyright : (C) Marco Gittler
10 email : g.marco@freenet.de
11 copyright : (C) 2006 Jean-Baptiste Mardelle
12 email : jb@kdenlive.org
14 ***************************************************************************/
16 /***************************************************************************
18 * This program is free software; you can redistribute it and/or modify *
19 * it under the terms of the GNU General Public License as published by *
20 * the Free Software Foundation; either version 2 of the License, or *
21 * (at your option) any later version. *
23 ***************************************************************************/
27 #include "kdenlivesettings.h"
29 #include "definitions.h"
31 #include <mlt++/Mlt.h>
34 #include <KStandardDirs>
35 #include <KMessageBox>
37 #include <KTemporaryFile>
41 #include <QApplication>
47 static void kdenlive_callback(void* /*ptr*/, int level, const char* fmt, va_list vl)
49 if (level > MLT_LOG_ERROR) return;
51 QApplication::postEvent(qApp->activeWindow() , new MltErrorEvent(error.vsprintf(fmt, vl).simplified()));
55 static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr)
57 // detect if the producer has finished playing. Is there a better way to do it?
58 if (self->m_isBlocked) return;
59 if (mlt_properties_get_double(MLT_FRAME_PROPERTIES(frame_ptr), "_speed") == 0.0) {
60 self->emitConsumerStopped();
62 self->emitFrameNumber(mlt_frame_get_position(frame_ptr));
66 Render::Render(const QString & rendererName, int winid, int /* extid */, QWidget *parent) :
79 kDebug() << "////////// USING PROFILE: " << (char*)KdenliveSettings::current_profile().toUtf8().data();
80 m_refreshTimer = new QTimer(this);
81 connect(m_refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
83 /*if (rendererName == "project") m_monitorId = 10000;
84 else m_monitorId = 10001;*/
85 m_osdTimer = new QTimer(this);
86 connect(m_osdTimer, SIGNAL(timeout()), this, SLOT(slotOsdTimeout()));
90 m_mltProducer = m_blackClip->cut(0, 50);
91 m_mltConsumer->connect(*m_mltProducer);
92 m_mltProducer->set_speed(0.0);
102 void Render::closeMlt()
105 delete m_refreshTimer;
106 delete m_mltConsumer;
107 delete m_mltProducer;
113 void Render::buildConsumer()
116 m_activeProfile = KdenliveSettings::current_profile();
117 tmp = decodedString(m_activeProfile);
118 setenv("MLT_PROFILE", tmp, 1);
122 m_mltProfile = new Mlt::Profile(tmp);
125 QString videoDriver = KdenliveSettings::videodrivername();
126 if (!videoDriver.isEmpty()) {
127 if (videoDriver == "x11_noaccel") {
128 setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
131 unsetenv("SDL_VIDEO_YUV_HWACCEL");
134 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
136 m_mltConsumer = new Mlt::Consumer(*m_mltProfile , "sdl_preview");
137 m_mltConsumer->set("resize", 1);
138 m_mltConsumer->set("window_id", m_winid);
139 m_mltConsumer->set("terminate_on_pause", 1);
140 tmp = decodedString(KdenliveSettings::window_background().name());
141 m_mltConsumer->set("window_background", tmp);
144 // FIXME: the event object returned by the listen gets leaked...
145 m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_show);
146 m_mltConsumer->set("rescale", "nearest");
147 mlt_log_set_callback(kdenlive_callback);
149 QString audioDevice = KdenliveSettings::audiodevicename();
150 if (!audioDevice.isEmpty()) {
151 tmp = decodedString(audioDevice);
152 m_mltConsumer->set("audio_device", tmp);
156 if (!videoDriver.isEmpty()) {
157 tmp = decodedString(videoDriver);
158 m_mltConsumer->set("video_driver", tmp);
162 QString audioDriver = KdenliveSettings::audiodrivername();
165 // Disabled because the "auto" detected driver was sometimes wrong
166 if (audioDriver.isEmpty())
167 audioDriver = KdenliveSettings::autoaudiodrivername();
170 if (!audioDriver.isEmpty()) {
171 tmp = decodedString(audioDriver);
172 m_mltConsumer->set("audio_driver", tmp);
177 m_mltConsumer->set("progressive", 1);
178 m_mltConsumer->set("audio_buffer", 1024);
179 m_mltConsumer->set("frequency", 48000);
181 m_blackClip = new Mlt::Producer(*m_mltProfile , "colour", "black");
182 m_blackClip->set("id", "black");
186 int Render::resetProfile()
188 if (!m_mltConsumer) return 0;
189 if (m_activeProfile == KdenliveSettings::current_profile()) {
190 kDebug() << "reset to same profile, nothing to do";
193 kDebug() << "// RESETTING PROFILE FROM: " << m_activeProfile << " TO: " << KdenliveSettings::current_profile();
194 if (m_isSplitView) slotSplitView(false);
195 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
196 m_mltConsumer->purge();
197 delete m_mltConsumer;
198 m_mltConsumer = NULL;
199 QString scene = sceneList();
202 pos = m_mltProducer->position();
203 delete m_mltProducer;
205 m_mltProducer = NULL;
207 //WARNING: Trying to delete the profile will crash when trying to display a clip afterwards...
208 /*if (m_mltProfile) delete m_mltProfile;
209 m_mltProfile = NULL;*/
213 //kDebug() << "//RESET WITHSCENE: " << scene;
214 setSceneList(scene, pos);
216 /*char *tmp = decodedString(scene);
217 Mlt::Producer *producer = new Mlt::Producer(*m_mltProfile , "xml-string", tmp);
219 m_mltProducer = producer;
220 m_blackClip = new Mlt::Producer(*m_mltProfile , "colour", "black");
221 m_mltProducer->optimise();
222 m_mltProducer->set_speed(0);
225 //delete m_mltProfile;
226 // mlt_properties properties = MLT_CONSUMER_PROPERTIES(m_mltConsumer->get_consumer());
227 //mlt_profile prof = m_mltProfile->get_profile();
228 //mlt_properties_set_data(properties, "_profile", prof, 0, (mlt_destructor)mlt_profile_close, NULL);
229 //mlt_properties_set(properties, "profile", "hdv_1080_50i");
230 //m_mltConsumer->set("profile", (char *) profile.toUtf8().data());
231 //m_mltProfile = new Mlt::Profile((char*) profile.toUtf8().data());
233 //apply_profile_properties( m_mltProfile, m_mltConsumer->get_consumer(), properties );
238 /** Wraps the VEML command of the same name; Seeks the renderer clip to the given time. */
239 void Render::seek(GenTime time)
244 m_mltProducer->seek((int)(time.frames(m_fps)));
249 char *Render::decodedString(QString str)
251 /*QCString fn = QFile::encodeName(str);
252 char *t = new char[fn.length() + 1];
253 strcpy(t, (const char *)fn);*/
255 return (char *) qstrdup(str.toUtf8().data()); //toLatin1
259 /*QPixmap Render::frameThumbnail(Mlt::Frame *frame, int width, int height, bool border) {
260 QPixmap pix(width, height);
262 mlt_image_format format = mlt_image_rgb24a;
263 uint8_t *thumb = frame->get_image(format, width, height);
264 QImage image(thumb, width, height, QImage::Format_ARGB32);
266 if (!image.isNull()) {
267 pix = pix.fromImage(image);
269 QPainter painter(&pix);
270 painter.drawRect(0, 0, width - 1, height - 1);
272 } else pix.fill(Qt::black);
276 int Render::renderWidth() const
278 return (int)(m_mltProfile->height() * m_mltProfile->dar());
281 int Render::renderHeight() const
283 return m_mltProfile->height();
286 QPixmap Render::extractFrame(int frame_position, int width, int height)
289 width = renderWidth();
290 height = renderHeight();
291 } else if (width % 2 == 1) width++;
292 QPixmap pix(width, height);
293 if (!m_mltProducer) {
297 return KThumb::getFrame(m_mltProducer, frame_position, width, height);
300 QPixmap Render::getImageThumbnail(KUrl url, int /*width*/, int /*height*/)
304 if (url.fileName().startsWith(".all.")) { // check for slideshow
305 QString fileType = url.fileName().right(3);
307 QStringList::Iterator it;
309 QDir dir(url.directory());
311 filter << "*." + fileType;
312 filter << "*." + fileType.toUpper();
313 more = dir.entryList(filter, QDir::Files);
314 im.load(url.directory() + '/' + more.at(0));
315 } else im.load(url.path());
316 //pixmap = im.scaled(width, height);
321 QPixmap Render::getVideoThumbnail(char *profile, QString file, int frame_position, int width, int height) {
322 QPixmap pix(width, height);
323 char *tmp = decodedString(file);
324 Mlt::Profile *prof = new Mlt::Profile(profile);
325 Mlt::Producer m_producer(*prof, tmp);
327 if (m_producer.is_blank()) {
332 Mlt::Filter m_convert(*prof, "avcolour_space");
333 m_convert.set("forced", mlt_image_rgb24a);
334 m_producer.attach(m_convert);
335 m_producer.seek(frame_position);
336 Mlt::Frame * frame = m_producer.get_frame();
338 pix = frameThumbnail(frame, width, height, true);
341 if (prof) delete prof;
346 void Render::getImage(KUrl url, int frame_position, QPoint size)
348 char *tmp = decodedString(url.path());
349 Mlt::Producer m_producer(tmp);
351 if (m_producer.is_blank()) {
354 Mlt::Filter m_convert("avcolour_space");
355 m_convert.set("forced", mlt_image_rgb24a);
356 m_producer.attach(m_convert);
357 m_producer.seek(frame_position);
359 Mlt::Frame * frame = m_producer.get_frame();
362 QPixmap pix = frameThumbnail(frame, size.x(), size.y(), true);
364 emit replyGetImage(url, frame_position, pix, size.x(), size.y());
368 /* Create thumbnail for color */
369 /*void Render::getImage(int id, QString color, QPoint size)
371 QPixmap pixmap(size.x() - 2, size.y() - 2);
372 color = color.replace(0, 2, "#");
373 color = color.left(7);
374 pixmap.fill(QColor(color));
375 QPixmap result(size.x(), size.y());
376 result.fill(Qt::black);
377 //copyBlt(&result, 1, 1, &pixmap, 0, 0, size.x() - 2, size.y() - 2);
378 emit replyGetImage(id, result, size.x(), size.y());
382 /* Create thumbnail for image */
383 /*void Render::getImage(KUrl url, QPoint size)
387 if (url.fileName().startsWith(".all.")) { // check for slideshow
388 QString fileType = url.fileName().right(3);
390 QStringList::Iterator it;
392 QDir dir( url.directory() );
393 more = dir.entryList( QDir::Files );
394 for ( it = more.begin() ; it != more.end() ; ++it ) {
395 if ((*it).endsWith("."+fileType, Qt::CaseInsensitive)) {
396 if (!im.load(url.directory() + '/' + *it))
397 kDebug()<<"++ ERROR LOADIN IMAGE: "<<url.directory() + '/' + *it;
402 else im.load(url.path());
404 //pixmap = im.smoothScale(size.x() - 2, size.y() - 2);
405 QPixmap result(size.x(), size.y());
406 result.fill(Qt::black);
407 //copyBlt(&result, 1, 1, &pixmap, 0, 0, size.x() - 2, size.y() - 2);
408 emit replyGetImage(url, 1, result, size.x(), size.y());
412 double Render::consumerRatio() const
414 if (!m_mltConsumer) return 1.0;
415 return (m_mltConsumer->get_double("aspect_ratio_num") / m_mltConsumer->get_double("aspect_ratio_den"));
419 int Render::getLength()
423 // kDebug()<<"////// LENGTH: "<<mlt_producer_get_playtime(m_mltProducer->get_producer());
424 return mlt_producer_get_playtime(m_mltProducer->get_producer());
429 bool Render::isValid(KUrl url)
431 char *tmp = decodedString(url.path());
432 Mlt::Producer producer(*m_mltProfile, tmp);
434 if (producer.is_blank())
440 double Render::dar() const
442 return m_mltProfile->dar();
445 void Render::slotSplitView(bool doit)
447 m_isSplitView = doit;
448 Mlt::Service service(m_mltProducer->parent().get_service());
449 Mlt::Tractor tractor(service);
450 if (service.type() != tractor_type || tractor.count() < 2) return;
451 Mlt::Field *field = tractor.field();
454 for (int i = 1; i < tractor.count() && screen < 4; i++) {
455 Mlt::Producer trackProducer(tractor.track(i));
456 kDebug() << "// TRACK: " << i << ", HIDE: " << trackProducer.get("hide");
457 if (QString(trackProducer.get("hide")).toInt() != 1) {
458 kDebug() << "// ADIDNG TRACK: " << i;
459 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
460 transition->set("mlt_service", "composite");
461 transition->set("a_track", 0);
462 transition->set("b_track", i);
463 transition->set("distort", 1);
464 transition->set("internal_added", "200");
471 tmp = "50%,0:50%x50%";
474 tmp = "0,50%:50%x50%";
478 tmp = "50%,50%:50%x50%";
481 transition->set("geometry", tmp);
482 transition->set("always_active", "1");
483 field->plant_transition(*transition, 0, i);
488 m_mltConsumer->set("refresh", 1);
490 mlt_service serv = m_mltProducer->parent().get_service();
491 mlt_service nextservice = mlt_service_get_producer(serv);
492 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
493 QString mlt_type = mlt_properties_get(properties, "mlt_type");
494 QString resource = mlt_properties_get(properties, "mlt_service");
496 while (mlt_type == "transition") {
497 QString added = mlt_properties_get(MLT_SERVICE_PROPERTIES(nextservice), "internal_added");
498 if (added == "200") {
499 mlt_field_disconnect_service(field->get_field(), nextservice);
501 nextservice = mlt_service_producer(nextservice);
502 if (nextservice == NULL) break;
503 properties = MLT_SERVICE_PROPERTIES(nextservice);
504 mlt_type = mlt_properties_get(properties, "mlt_type");
505 resource = mlt_properties_get(properties, "mlt_service");
506 m_mltConsumer->set("refresh", 1);
511 void Render::getFileProperties(const QDomElement &xml, const QString &clipId, bool replaceProducer)
513 KUrl url = KUrl(xml.attribute("resource", QString()));
514 Mlt::Producer *producer = NULL;
515 if (xml.attribute("type").toInt() == TEXT && !QFile::exists(url.path())) {
516 emit replyGetFileProperties(clipId, producer, QMap < QString, QString >(), QMap < QString, QString >(), replaceProducer);
519 if (xml.attribute("type").toInt() == COLOR) {
520 char *tmp = decodedString("colour:" + xml.attribute("colour"));
521 producer = new Mlt::Producer(*m_mltProfile, 0, tmp);
523 } else if (url.isEmpty()) {
525 QDomElement mlt = doc.createElement("mlt");
526 QDomElement play = doc.createElement("playlist");
527 doc.appendChild(mlt);
528 mlt.appendChild(play);
529 play.appendChild(doc.importNode(xml, true));
530 char *tmp = decodedString(doc.toString());
531 producer = new Mlt::Producer(*m_mltProfile, "xml-string", tmp);
534 char *tmp = decodedString(url.path());
535 producer = new Mlt::Producer(*m_mltProfile, tmp);
539 if (producer == NULL || producer->is_blank() || !producer->is_valid()) {
540 kDebug() << " / / / / / / / / ERROR / / / / // CANNOT LOAD PRODUCER: ";
541 emit removeInvalidClip(clipId, replaceProducer);
546 if (xml.hasAttribute("force_aspect_ratio")) {
547 double aspect = xml.attribute("force_aspect_ratio").toDouble();
548 if (aspect > 0) producer->set("force_aspect_ratio", aspect);
550 if (xml.hasAttribute("threads")) {
551 int threads = xml.attribute("threads").toInt();
552 if (threads != 1) producer->set("threads", threads);
554 if (xml.hasAttribute("video_index")) {
555 int vindex = xml.attribute("video_index").toInt();
556 if (vindex != 0) producer->set("video_index", vindex);
558 if (xml.hasAttribute("audio_index")) {
559 int aindex = xml.attribute("audio_index").toInt();
560 if (aindex != 0) producer->set("audio_index", aindex);
563 if (xml.hasAttribute("out")) producer->set_in_and_out(xml.attribute("in").toInt(), xml.attribute("out").toInt());
565 char *tmp = decodedString(clipId);
566 producer->set("id", tmp);
569 if (!replaceProducer && xml.hasAttribute("file_hash")) {
570 // Clip already has all properties
571 emit replyGetFileProperties(clipId, producer, QMap < QString, QString >(), QMap < QString, QString >(), replaceProducer);
576 int width = (int)(height * m_mltProfile->dar());
577 QMap < QString, QString > filePropertyMap;
578 QMap < QString, QString > metadataPropertyMap;
580 int frameNumber = xml.attribute("thumbnail", "0").toInt();
581 if (frameNumber != 0) producer->seek(frameNumber);
583 filePropertyMap["duration"] = QString::number(producer->get_playtime());
584 //kDebug() << "/////// PRODUCER: " << url.path() << " IS: " << producer.get_playtime();
586 Mlt::Frame *frame = producer->get_frame();
588 if (xml.attribute("type").toInt() == SLIDESHOW) {
589 if (xml.hasAttribute("ttl")) producer->set("ttl", xml.attribute("ttl").toInt());
590 if (xml.attribute("fade") == "1") {
591 // user wants a fade effect to slideshow
592 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "luma");
593 if (filter && filter->is_valid()) {
594 if (xml.hasAttribute("ttl")) filter->set("period", xml.attribute("ttl").toInt() - 1);
595 if (xml.hasAttribute("luma_duration") && !xml.attribute("luma_duration").isEmpty()) filter->set("luma.out", xml.attribute("luma_duration").toInt());
596 if (xml.hasAttribute("luma_file") && !xml.attribute("luma_file").isEmpty()) {
597 char *tmp = decodedString(xml.attribute("luma_file"));
598 filter->set("luma.resource", tmp);
600 if (xml.hasAttribute("softness")) {
601 int soft = xml.attribute("softness").toInt();
602 filter->set("luma.softness", (double) soft / 100.0);
605 Mlt::Service clipService(producer->get_service());
606 clipService.attach(*filter);
612 filePropertyMap["fps"] = producer->get("source_fps");
614 if (frame && frame->is_valid()) {
615 filePropertyMap["frame_size"] = QString::number(frame->get_int("width")) + 'x' + QString::number(frame->get_int("height"));
616 filePropertyMap["frequency"] = QString::number(frame->get_int("frequency"));
617 filePropertyMap["channels"] = QString::number(frame->get_int("channels"));
618 filePropertyMap["aspect_ratio"] = frame->get("aspect_ratio");
620 if (frame->get_int("test_image") == 0) {
621 if (url.path().endsWith(".mlt") || url.path().endsWith(".westley") || url.path().endsWith(".kdenlive")) {
622 filePropertyMap["type"] = "playlist";
623 metadataPropertyMap["comment"] = QString::fromUtf8(producer->get("title"));
624 } else if (frame->get_int("test_audio") == 0)
625 filePropertyMap["type"] = "av";
627 filePropertyMap["type"] = "video";
629 mlt_image_format format = mlt_image_rgb24a;
630 int frame_width = width;
631 int frame_height = height;
632 QPixmap pix(width, height);
633 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
634 QImage image((uchar *)data, frame_width, frame_height, QImage::Format_ARGB32);
636 if (!image.isNull()) {
637 pix = QPixmap::fromImage(image.rgbSwapped());
641 emit replyGetImage(clipId, pix);
643 } else if (frame->get_int("test_audio") == 0) {
644 QPixmap pixmap = KIcon("audio-x-generic").pixmap(QSize(width, height));
645 emit replyGetImage(clipId, pixmap);
646 filePropertyMap["type"] = "audio";
650 // Retrieve audio / video codec name
654 if (producer->get_int("video_index") > -1) {
655 /*if (context->duration == AV_NOPTS_VALUE) {
656 kDebug() << " / / / / / / / /ERROR / / / CLIP HAS UNKNOWN DURATION";
657 emit removeInvalidClip(clipId);
661 // Get the video_index
662 int default_video = producer->get_int("video_index");
664 int default_audio = producer->get_int("audio_index");
667 // Find maximum stream index values
668 for (int ix = 0; ix < producer->get_int("meta.media.nb_streams"); ix++) {
669 snprintf(property, sizeof(property), "meta.media.%d.stream.type", ix);
670 QString type = producer->get(property);
673 else if (type == "audio")
676 filePropertyMap["default_video"] = QString::number(default_video);
677 filePropertyMap["video_max"] = QString::number(video_max);
678 filePropertyMap["default_audio"] = QString::number(default_audio);
679 filePropertyMap["audio_max"] = QString::number(audio_max);
681 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", default_video);
682 if (producer->get(property)) {
683 filePropertyMap["videocodec"] = producer->get(property);
685 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
686 if (producer->get(property))
687 filePropertyMap["videocodec"] = producer->get(property);
690 if (KdenliveSettings::dropbframes()) {
691 kDebug() << "// LOOKING FOR H264 on: " << default_video;
692 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
693 kDebug() << "PROP: " << property << " = " << producer->get(property);
694 if (producer->get(property) && strcmp(producer->get(property), "h264") == 0) {
695 kDebug() << "// GOT H264 CLIP, SETTING FAST PROPS";
696 producer->set("skip_loop_filter", "all");
697 producer->set("skip_frame", "bidir");
701 } else kDebug() << " / / / / /WARNING, VIDEO CONTEXT IS NULL!!!!!!!!!!!!!!";
702 if (producer->get_int("audio_index") > -1) {
703 // Get the audio_index
704 int index = producer->get_int("audio_index");
706 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", index);
707 if (producer->get(property)) {
708 filePropertyMap["audiocodec"] = producer->get(property);
710 snprintf(property, sizeof(property), "meta.media.%d.codec.name", index);
711 if (producer->get(property))
712 filePropertyMap["audiocodec"] = producer->get(property);
717 Mlt::Properties metadata;
718 metadata.pass_values(*producer, "meta.attr.");
719 int count = metadata.count();
720 for (int i = 0; i < count; i ++) {
721 QString name = metadata.get_name(i);
722 QString value = QString::fromUtf8(metadata.get(i));
723 if (name.endsWith("markup") && !value.isEmpty())
724 metadataPropertyMap[ name.section('.', 0, -2)] = value;
727 emit replyGetFileProperties(clipId, producer, filePropertyMap, metadataPropertyMap, replaceProducer);
728 kDebug() << "REquested fuile info for: " << url.path();
730 // FIXME: should delete this to avoid a leak...
735 /** Create the producer from the MLT XML QDomDocument */
737 void Render::initSceneList()
739 kDebug() << "-------- INIT SCENE LIST ------_";
741 QDomElement mlt = doc.createElement("mlt");
742 doc.appendChild(mlt);
743 QDomElement prod = doc.createElement("producer");
744 prod.setAttribute("resource", "colour");
745 prod.setAttribute("colour", "red");
746 prod.setAttribute("id", "black");
747 prod.setAttribute("in", "0");
748 prod.setAttribute("out", "0");
750 QDomElement tractor = doc.createElement("tractor");
751 QDomElement multitrack = doc.createElement("multitrack");
753 QDomElement playlist1 = doc.createElement("playlist");
754 playlist1.appendChild(prod);
755 multitrack.appendChild(playlist1);
756 QDomElement playlist2 = doc.createElement("playlist");
757 multitrack.appendChild(playlist2);
758 QDomElement playlist3 = doc.createElement("playlist");
759 multitrack.appendChild(playlist3);
760 QDomElement playlist4 = doc.createElement("playlist");
761 multitrack.appendChild(playlist4);
762 QDomElement playlist5 = doc.createElement("playlist");
763 multitrack.appendChild(playlist5);
764 tractor.appendChild(multitrack);
765 mlt.appendChild(tractor);
766 // kDebug()<<doc.toString();
768 QString tmp = QString("<mlt><producer resource=\"colour\" colour=\"red\" id=\"red\" /><tractor><multitrack><playlist></playlist><playlist></playlist><playlist /><playlist /><playlist></playlist></multitrack></tractor></mlt>");*/
769 setSceneList(doc, 0);
775 /** Create the producer from the MLT XML QDomDocument */
776 int Render::setProducer(Mlt::Producer *producer, int position)
778 if (m_winid == -1) return -1;
781 m_mltConsumer->stop();
784 m_mltConsumer->purge();
788 m_mltProducer->set_speed(0);
789 delete m_mltProducer;
790 m_mltProducer = NULL;
794 m_mltProducer = new Mlt::Producer(producer->get_producer());
795 } else m_mltProducer = m_blackClip->cut(0, 50);
796 /*if (KdenliveSettings::dropbframes()) {
797 m_mltProducer->set("skip_loop_filter", "all");
798 m_mltProducer->set("skip_frame", "bidir");
800 if (!m_mltProducer || !m_mltProducer->is_valid()) kDebug() << " WARNING - - - - -INVALID PLAYLIST: ";
802 m_fps = m_mltProducer->get_fps();
803 int error = connectPlaylist();
804 if (position != -1) {
805 m_mltProducer->seek(position);
806 emit rendererPosition(position);
814 /** Create the producer from the MLT XML QDomDocument */
815 int Render::setSceneList(QDomDocument list, int position)
817 return setSceneList(list.toString(), position);
820 /** Create the producer from the MLT XML QDomDocument */
821 int Render::setSceneList(QString playlist, int position)
823 if (m_winid == -1) return -1;
826 qDeleteAll(m_slowmotionProducers.values());
827 m_slowmotionProducers.clear();
829 //kWarning() << "////// RENDER, SET SCENE LIST: " << playlist;
831 if (m_mltConsumer == NULL) {
832 kWarning() << "/////// ERROR, TRYING TO USE NULL MLT CONSUMER";
837 if (!m_mltConsumer->is_stopped()) {
838 m_mltConsumer->stop();
839 //m_mltConsumer->set("refresh", 0);
843 m_mltProducer->set_speed(0);
844 //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
846 delete m_mltProducer;
847 m_mltProducer = NULL;
852 char *tmp = decodedString(playlist);
853 m_mltProducer = new Mlt::Producer(*m_mltProfile, "xml-string", tmp);
855 if (!m_mltProducer || !m_mltProducer->is_valid()) {
856 kDebug() << " WARNING - - - - -INVALID PLAYLIST: " << tmp;
857 m_mltProducer = m_blackClip->cut(0, 50);
861 m_mltProducer->optimise();
863 /*if (KdenliveSettings::osdtimecode()) {
864 // Attach filter for on screen display of timecode
866 QString attr = "attr_check";
867 mlt_filter filter = mlt_factory_filter( "data_feed", (char*) attr.ascii() );
868 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
869 mlt_producer_attach( m_mltProducer->get_producer(), filter );
870 mlt_filter_close( filter );
872 m_osdInfo = new Mlt::Filter("data_show");
873 tmp = decodedString(m_osdProfile);
874 m_osdInfo->set("resource", tmp);
876 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
877 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
878 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
879 m_osdInfo->set("dynamic", "1");
881 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
883 m_osdInfo->set("dynamic", "0");
886 m_fps = m_mltProducer->get_fps();
888 // Seek to correct place after opening project.
889 m_mltProducer->seek(position);
892 kDebug() << "// NEW SCENE LIST DURATION SET TO: " << m_mltProducer->get_playtime();
893 error = connectPlaylist();
894 fillSlowMotionProducers();
898 emit refreshDocumentProducers();
900 //kDebug()<<"// SETSCN LST, POS: "<<position;
901 //if (position != 0) emit rendererPosition(position);
904 /** Create the producer from the MLT XML QDomDocument */
905 const QString Render::sceneList()
908 Mlt::Consumer xmlConsumer(*m_mltProfile , "xml:kdenlive_playlist");
909 m_mltProducer->optimise();
910 xmlConsumer.set("terminate_on_pause", 1);
911 Mlt::Producer prod(m_mltProducer->get_producer());
912 bool split = m_isSplitView;
913 if (split) slotSplitView(false);
914 xmlConsumer.connect(prod);
916 while (!xmlConsumer.is_stopped()) {}
917 playlist = QString::fromUtf8(xmlConsumer.get("kdenlive_playlist"));
918 if (split) slotSplitView(true);
922 bool Render::saveSceneList(QString path, QDomElement kdenliveData)
926 doc.setContent(sceneList(), false);
927 if (!kdenliveData.isNull()) {
928 // add Kdenlive specific tags
929 QDomNode mlt = doc.elementsByTagName("mlt").at(0);
930 mlt.appendChild(doc.importNode(kdenliveData, true));
932 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
933 kWarning() << "////// ERROR writing to file: " << path;
936 file.write(doc.toString().toUtf8());
937 if (file.error() != QFile::NoError) {
946 void Render::saveZone(KUrl url, QString desc, QPoint zone)
948 kDebug() << "// SAVING CLIP ZONE, RENDER: " << m_name;
949 char *tmppath = decodedString("xml:" + url.path());
950 Mlt::Consumer xmlConsumer(*m_mltProfile , tmppath);
951 m_mltProducer->optimise();
953 xmlConsumer.set("terminate_on_pause", 1);
954 if (m_name == "clip") {
955 Mlt::Producer *prod = m_mltProducer->cut(zone.x(), zone.y());
956 tmppath = decodedString(desc);
958 list.insert_at(0, prod, 0);
959 list.set("title", tmppath);
961 xmlConsumer.connect(list);
964 //TODO: not working yet, save zone from timeline
965 Mlt::Producer *p1 = new Mlt::Producer(m_mltProducer->get_producer());
966 /* Mlt::Service service(p1->parent().get_service());
967 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";*/
969 //Mlt::Producer *prod = p1->cut(zone.x(), zone.y());
970 tmppath = decodedString(desc);
971 //prod->set("title", tmppath);
973 xmlConsumer.connect(*p1); //list);
979 double Render::fps() const
984 int Render::connectPlaylist()
986 if (!m_mltConsumer) return -1;
987 //m_mltConsumer->set("refresh", "0");
988 m_mltConsumer->connect(*m_mltProducer);
989 m_mltProducer->set_speed(0);
990 if (m_mltConsumer->start() == -1) {
991 // ARGH CONSUMER BROKEN!!!!
992 KMessageBox::error(qApp->activeWindow(), i18n("Could not create the video preview window.\nThere is something wrong with your Kdenlive install or your driver settings, please fix it."));
993 emit blockMonitors();
994 delete m_mltProducer;
995 m_mltProducer = NULL;
998 emit durationChanged(m_mltProducer->get_playtime());
1003 void Render::refreshDisplay()
1006 if (!m_mltProducer) return;
1007 //m_mltConsumer->set("refresh", 0);
1009 //mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1010 /*if (KdenliveSettings::osdtimecode()) {
1011 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
1012 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
1013 m_osdInfo->set("dynamic", "1");
1014 m_mltProducer->attach(*m_osdInfo);
1017 m_mltProducer->detach(*m_osdInfo);
1018 m_osdInfo->set("dynamic", "0");
1023 void Render::setVolume(double /*volume*/)
1025 if (!m_mltConsumer || !m_mltProducer) return;
1027 m_mltConsumer->set("refresh", 0);
1028 // Attach filter for on screen display of timecode
1029 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1030 mlt_properties_set_double( properties, "meta.volume", volume );
1031 mlt_properties_set_int( properties, "meta.attr.osdvolume", 1);
1032 mlt_properties_set( properties, "meta.attr.osdvolume.markup", i18n("Volume: ") + QString::number(volume * 100));
1034 if (!KdenliveSettings::osdtimecode()) {
1035 m_mltProducer->detach(*m_osdInfo);
1036 mlt_properties_set_int( properties, "meta.attr.timecode", 0);
1037 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1040 m_osdTimer->setSingleShot(2500);
1043 void Render::slotOsdTimeout()
1045 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1046 mlt_properties_set_int(properties, "meta.attr.osdvolume", 0);
1047 mlt_properties_set(properties, "meta.attr.osdvolume.markup", NULL);
1048 //if (!KdenliveSettings::osdtimecode()) m_mltProducer->detach(*m_osdInfo);
1052 void Render::start()
1054 kDebug() << "----- STARTING MONITOR: " << m_name;
1055 if (m_winid == -1) {
1056 kDebug() << "----- BROKEN MONITOR: " << m_name << ", RESTART";
1059 if (m_mltConsumer && m_mltConsumer->is_stopped()) {
1060 kDebug() << "----- MONITOR: " << m_name << " WAS STOPPED";
1061 if (m_mltConsumer->start() == -1) {
1062 KMessageBox::error(qApp->activeWindow(), i18n("Could not create the video preview window.\nThere is something wrong with your Kdenlive install or your driver settings, please fix it."));
1063 emit blockMonitors();
1064 delete m_mltProducer;
1065 m_mltProducer = NULL;
1067 kDebug() << "----- MONITOR: " << m_name << " REFRESH";
1068 m_isBlocked = false;
1072 m_isBlocked = false;
1075 void Render::clear()
1077 kDebug() << " ********* RENDER CLEAR";
1078 if (m_mltConsumer) {
1079 //m_mltConsumer->set("refresh", 0);
1080 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
1083 if (m_mltProducer) {
1084 //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
1085 m_mltProducer->set_speed(0.0);
1086 delete m_mltProducer;
1087 m_mltProducer = NULL;
1094 if (m_mltProducer == NULL) return;
1095 if (m_mltConsumer && !m_mltConsumer->is_stopped()) {
1096 kDebug() << "///////////// RENDER STOPPED: " << m_name;
1098 //m_mltConsumer->set("refresh", 0);
1099 m_mltConsumer->stop();
1100 // delete m_mltConsumer;
1101 // m_mltConsumer = NULL;
1103 kDebug() << "///////////// RENDER STOP2-------";
1106 if (m_mltProducer) {
1107 if (m_isZoneMode) resetZoneMode();
1108 m_mltProducer->set_speed(0.0);
1109 //m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1110 //kDebug() << m_mltProducer->get_length();
1112 kDebug() << "///////////// RENDER STOP3-------";
1115 void Render::stop(const GenTime & startTime)
1118 kDebug() << "///////////// RENDER STOP-------2";
1119 if (m_mltProducer) {
1120 if (m_isZoneMode) resetZoneMode();
1121 m_mltProducer->set_speed(0.0);
1122 m_mltProducer->seek((int) startTime.frames(m_fps));
1124 m_mltConsumer->purge();
1127 void Render::pause()
1129 if (!m_mltProducer || !m_mltConsumer)
1131 if (m_mltProducer->get_speed() == 0.0) return;
1132 if (m_isZoneMode) resetZoneMode();
1134 m_mltConsumer->set("refresh", 0);
1135 m_mltProducer->set_speed(0.0);
1136 emit rendererPosition(m_framePosition);
1137 m_mltProducer->seek(m_framePosition);
1138 m_mltConsumer->purge();
1141 void Render::switchPlay()
1143 if (!m_mltProducer || !m_mltConsumer)
1145 if (m_isZoneMode) resetZoneMode();
1146 if (m_mltProducer->get_speed() == 0.0) {
1147 m_isBlocked = false;
1148 m_mltProducer->set_speed(1.0);
1149 m_mltConsumer->set("refresh", 1);
1152 m_mltConsumer->set("refresh", 0);
1153 m_mltProducer->set_speed(0.0);
1154 emit rendererPosition(m_framePosition);
1155 m_mltProducer->seek(m_framePosition);
1156 m_mltConsumer->purge();
1157 //kDebug()<<" ********* RENDER PAUSE: "<<m_mltProducer->get_speed();
1158 //m_mltConsumer->set("refresh", 0);
1159 /*mlt_position position = mlt_producer_position( m_mltProducer->get_producer() );
1160 m_mltProducer->set_speed(0);
1161 m_mltProducer->seek( position );
1162 //m_mltProducer->seek((int) m_framePosition);
1163 m_isBlocked = false;*/
1165 /*if (speed == 0.0) {
1166 m_mltProducer->seek((int) m_framePosition + 1);
1167 m_mltConsumer->purge();
1172 void Render::play(double speed)
1176 // if (speed == 0.0) m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1177 m_isBlocked = false;
1178 m_mltProducer->set_speed(speed);
1179 /*if (speed == 0.0) {
1180 m_mltProducer->seek((int) m_framePosition + 1);
1181 m_mltConsumer->purge();
1186 void Render::play(const GenTime & startTime)
1188 if (!m_mltProducer || !m_mltConsumer)
1190 m_isBlocked = false;
1191 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1192 m_mltProducer->set_speed(1.0);
1193 m_mltConsumer->set("refresh", 1);
1196 void Render::loopZone(const GenTime & startTime, const GenTime & stopTime)
1198 if (!m_mltProducer || !m_mltConsumer)
1200 //m_mltProducer->set("eof", "loop");
1201 m_isLoopMode = true;
1202 m_loopStart = startTime;
1203 playZone(startTime, stopTime);
1206 void Render::playZone(const GenTime & startTime, const GenTime & stopTime)
1208 if (!m_mltProducer || !m_mltConsumer)
1210 m_isBlocked = false;
1211 if (!m_isZoneMode) m_originalOut = m_mltProducer->get_playtime() - 1;
1212 m_mltProducer->set("out", stopTime.frames(m_fps));
1213 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1214 m_mltProducer->set_speed(1.0);
1215 m_mltConsumer->set("refresh", 1);
1216 m_isZoneMode = true;
1219 void Render::resetZoneMode()
1221 if (!m_isZoneMode && !m_isLoopMode) return;
1222 m_mltProducer->set("out", m_originalOut);
1223 //m_mltProducer->set("eof", "pause");
1224 m_isZoneMode = false;
1225 m_isLoopMode = false;
1228 void Render::seekToFrame(int pos)
1230 //kDebug()<<" ********* RENDER SEEK TO POS";
1233 m_isBlocked = false;
1235 m_mltProducer->seek(pos);
1239 void Render::askForRefresh()
1241 // Use a Timer so that we don't refresh too much
1242 m_refreshTimer->start(300);
1245 void Render::doRefresh()
1247 // Use a Timer so that we don't refresh too much
1248 if (!m_isBlocked && m_mltConsumer) m_mltConsumer->set("refresh", 1);
1251 void Render::refresh()
1253 if (!m_mltProducer || m_isBlocked)
1255 m_refreshTimer->stop();
1256 if (m_mltConsumer) {
1257 m_mltConsumer->set("refresh", 1);
1261 void Render::setDropFrames(bool show)
1263 if (m_mltConsumer) {
1265 if (show == false) dropFrames = 0;
1266 m_mltConsumer->stop();
1267 m_mltConsumer->set("play.real_time", dropFrames);
1268 if (m_mltConsumer->start() == -1) {
1269 emit blockMonitors();
1270 delete m_mltProducer;
1271 m_mltProducer = NULL;
1277 double Render::playSpeed()
1279 if (m_mltProducer) return m_mltProducer->get_speed();
1283 GenTime Render::seekPosition() const
1285 if (m_mltProducer) return GenTime((int) m_mltProducer->position(), m_fps);
1286 else return GenTime();
1290 const QString & Render::rendererName() const
1296 void Render::emitFrameNumber(double position)
1298 m_framePosition = position;
1299 emit rendererPosition((int) position);
1300 //if (qApp->activeWindow()) QApplication::postEvent(qApp->activeWindow(), new PositionChangeEvent( GenTime((int) position, m_fps), m_monitorId));
1303 void Render::emitConsumerStopped()
1305 // This is used to know when the playing stopped
1306 if (m_mltProducer) {
1307 double pos = m_mltProducer->position();
1308 if (m_isLoopMode) play(m_loopStart);
1309 else if (m_isZoneMode) resetZoneMode();
1310 emit rendererStopped((int) pos);
1311 //if (qApp->activeWindow()) QApplication::postEvent(qApp->activeWindow(), new PositionChangeEvent(GenTime((int) pos, m_fps), m_monitorId + 100));
1312 //new QCustomEvent(10002));
1318 void Render::exportFileToFirewire(QString /*srcFileName*/, int /*port*/, GenTime /*startTime*/, GenTime /*endTime*/)
1320 KMessageBox::sorry(0, i18n("Firewire is not enabled on your system.\n Please install Libiec61883 and recompile Kdenlive"));
1324 void Render::exportCurrentFrame(KUrl url, bool /*notify*/)
1326 if (!m_mltProducer) {
1327 KMessageBox::sorry(qApp->activeWindow(), i18n("There is no clip, cannot extract frame."));
1331 //int height = 1080;//KdenliveSettings::defaultheight();
1332 //int width = 1940; //KdenliveSettings::displaywidth();
1334 QPixmap pix; // = KThumb::getFrame(m_mltProducer, -1, width, height);
1336 QPixmap pix(width, height);
1337 Mlt::Filter m_convert(*m_mltProfile, "avcolour_space");
1338 m_convert.set("forced", mlt_image_rgb24a);
1339 m_mltProducer->attach(m_convert);
1340 Mlt::Frame * frame = m_mltProducer->get_frame();
1341 m_mltProducer->detach(m_convert);
1343 pix = frameThumbnail(frame, width, height);
1346 pix.save(url.path(), "PNG");
1347 //if (notify) QApplication::postEvent(qApp->activeWindow(), new UrlEvent(url, 10003));
1350 /** MLT PLAYLIST DIRECT MANIPULATON **/
1353 void Render::mltCheckLength()
1355 //kDebug()<<"checking track length: "<<track<<"..........";
1357 Mlt::Service service(m_mltProducer->get_service());
1358 Mlt::Tractor tractor(service);
1360 int trackNb = tractor.count();
1364 Mlt::Producer trackProducer(tractor.track(0));
1365 duration = trackProducer.get_playtime() - 1;
1366 m_mltProducer->set("out", duration);
1367 emit durationChanged(duration);
1370 while (trackNb > 1) {
1371 Mlt::Producer trackProducer(tractor.track(trackNb - 1));
1372 trackDuration = trackProducer.get_playtime() - 1;
1374 //kDebug() << " / / /DURATON FOR TRACK " << trackNb - 1 << " = " << trackDuration;
1375 if (trackDuration > duration) duration = trackDuration;
1379 Mlt::Producer blackTrackProducer(tractor.track(0));
1381 if (blackTrackProducer.get_playtime() - 1 != duration) {
1382 Mlt::Playlist blackTrackPlaylist((mlt_playlist) blackTrackProducer.get_service());
1383 Mlt::Producer *blackclip = blackTrackPlaylist.get_clip(0);
1384 if (duration > m_blackClip->get_length()) {
1385 m_blackClip->set("length", duration);
1386 if (blackclip) blackclip->set("length", duration);
1388 if (blackclip == NULL || blackclip->is_blank() || blackTrackPlaylist.count() != 1) {
1389 blackTrackPlaylist.clear();
1390 blackTrackPlaylist.append(*m_blackClip, 0, duration - 1);
1391 } else blackTrackPlaylist.resize_clip(0, 0, duration - 1);
1393 m_mltProducer->set("out", duration);
1394 emit durationChanged(duration);
1398 void Render::mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod)
1400 if (m_mltProducer == NULL) {
1401 kDebug() << "PLAYLIST NOT INITIALISED //////";
1404 Mlt::Producer parentProd(m_mltProducer->parent());
1405 if (parentProd.get_producer() == NULL) {
1406 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1410 Mlt::Service service(parentProd.get_service());
1411 Mlt::Tractor tractor(service);
1412 if (info.track > tractor.count() - 1) {
1413 kDebug() << "ERROR TRYING TO INSERT CLIP ON TRACK " << info.track << ", at POS: " << info.startPos.frames(25);
1416 mlt_service_lock(service.get_service());
1417 Mlt::Producer trackProducer(tractor.track(info.track));
1418 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1419 //kDebug()<<"/// INSERT cLIP: "<<info.cropStart.frames(m_fps)<<", "<<info.startPos.frames(m_fps)<<"-"<<info.endPos.frames(m_fps);
1421 if (element.attribute("speed", "1.0").toDouble() != 1.0) {
1422 // We want a slowmotion producer
1423 double speed = element.attribute("speed", "1.0").toDouble();
1424 QString url = QString::fromUtf8(prod->get("resource"));
1425 url.append('?' + QString::number(speed));
1426 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1427 if (!slowprod || slowprod->get_producer() == NULL) {
1428 char *tmp = decodedString(url);
1429 slowprod = new Mlt::Producer(*m_mltProfile, "framebuffer", tmp);
1431 QString id = prod->get("id");
1432 if (id.contains('_')) id = id.section('_', 0, 0);
1433 QString producerid = "slowmotion:" + id + ':' + QString::number(speed);
1434 tmp = decodedString(producerid);
1435 slowprod->set("id", tmp);
1437 m_slowmotionProducers.insert(url, slowprod);
1442 Mlt::Producer *clip = prod->cut((int) info.cropStart.frames(m_fps), (int)(info.endPos - info.startPos + info.cropStart).frames(m_fps) - 1);
1443 int newIndex = trackPlaylist.insert_at((int) info.startPos.frames(m_fps), *clip, 1);
1445 /*if (QString(prod->get("transparency")).toInt() == 1)
1446 mltAddClipTransparency(info, info.track - 1, QString(prod->get("id")).toInt());*/
1448 mlt_service_unlock(service.get_service());
1450 if (info.track != 0 && (newIndex + 1 == trackPlaylist.count())) mltCheckLength();
1451 //tractor.multitrack()->refresh();
1452 //tractor.refresh();
1456 void Render::mltCutClip(int track, GenTime position)
1461 Mlt::Service service(m_mltProducer->parent().get_service());
1462 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
1464 Mlt::Tractor tractor(service);
1465 Mlt::Producer trackProducer(tractor.track(track));
1466 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1469 /* // Display playlist info
1470 kDebug()<<"//////////// BEFORE";
1471 for (int i = 0; i < trackPlaylist.count(); i++) {
1472 int blankStart = trackPlaylist.clip_start(i);
1473 int blankDuration = trackPlaylist.clip_length(i) - 1;
1475 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1476 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1479 int cutPos = (int) position.frames(m_fps);
1481 int clipIndex = trackPlaylist.get_clip_index_at(cutPos);
1482 if (trackPlaylist.is_blank(clipIndex)) {
1483 kDebug() << "// WARNING, TRYING TO CUT A BLANK";
1484 m_isBlocked = false;
1487 mlt_service_lock(service.get_service());
1488 int clipStart = trackPlaylist.clip_start(clipIndex);
1489 trackPlaylist.split(clipIndex, cutPos - clipStart - 1);
1490 mlt_service_unlock(service.get_service());
1492 // duplicate effects
1493 Mlt::Producer *original = trackPlaylist.get_clip_at(clipStart);
1494 Mlt::Producer *clip = trackPlaylist.get_clip_at(cutPos);
1496 if (original == NULL || clip == NULL) {
1497 kDebug() << "// ERROR GRABBING CLIP AFTER SPLIT";
1499 Mlt::Service clipService(original->get_service());
1500 Mlt::Service dupService(clip->get_service());
1502 Mlt::Filter *filter = clipService.filter(ct);
1504 // Only duplicate Kdenlive filters, and skip the fade in effects
1505 if (filter->is_valid() && strcmp(filter->get("kdenlive_id"), "") && strcmp(filter->get("kdenlive_id"), "fadein") && strcmp(filter->get("kdenlive_id"), "fade_from_black")) {
1506 // looks like there is no easy way to duplicate a filter,
1507 // so we will create a new one and duplicate its properties
1508 Mlt::Filter *dup = new Mlt::Filter(*m_mltProfile, filter->get("mlt_service"));
1509 if (dup && dup->is_valid()) {
1510 Mlt::Properties entries(filter->get_properties());
1511 for (int i = 0; i < entries.count(); i++) {
1512 dup->set(entries.get_name(i), entries.get(i));
1514 dupService.attach(*dup);
1518 filter = clipService.filter(ct);
1521 /* // Display playlist info
1522 kDebug()<<"//////////// AFTER";
1523 for (int i = 0; i < trackPlaylist.count(); i++) {
1524 int blankStart = trackPlaylist.clip_start(i);
1525 int blankDuration = trackPlaylist.clip_length(i) - 1;
1527 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1528 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1531 m_isBlocked = false;
1534 void Render::mltUpdateClip(ItemInfo info, QDomElement element, Mlt::Producer *prod)
1537 mltRemoveClip(info.track, info.startPos);
1538 mltInsertClip(info, element, prod);
1542 bool Render::mltRemoveClip(int track, GenTime position)
1544 Mlt::Service service(m_mltProducer->parent().get_service());
1545 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
1547 Mlt::Tractor tractor(service);
1548 mlt_service_lock(service.get_service());
1549 Mlt::Producer trackProducer(tractor.track(track));
1550 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1551 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
1553 // Display playlist info
1554 //kDebug() << "//// BEFORE -( " << position.frames(m_fps) << " )-------------------------------";
1555 /*for (int i = 0; i < trackPlaylist.count(); i++) {
1556 int blankStart = trackPlaylist.clip_start(i);
1557 int blankDuration = trackPlaylist.clip_length(i) - 1;
1559 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1560 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1562 if (trackPlaylist.is_blank(clipIndex)) {
1563 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << position.frames(25);
1564 mlt_service_unlock(service.get_service());
1567 //kDebug()<<"//// Deleting at: "<< (int) position.frames(m_fps) <<" --------------------------------------";
1569 trackPlaylist.replace_with_blank(clipIndex);
1570 trackPlaylist.consolidate_blanks(0);
1571 /*if (QString(clip.parent().get("transparency")).toInt() == 1)
1572 mltDeleteTransparency((int) position.frames(m_fps), track, QString(clip.parent().get("id")).toInt());*/
1574 /* // Display playlist info
1575 kDebug()<<"//// AFTER";
1576 for (int i = 0; i < trackPlaylist.count(); i++) {
1577 int blankStart = trackPlaylist.clip_start(i);
1578 int blankDuration = trackPlaylist.clip_length(i) - 1;
1580 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1581 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1583 mlt_service_unlock(service.get_service());
1584 if (track != 0 && trackPlaylist.count() <= clipIndex) mltCheckLength();
1585 m_isBlocked = false;
1589 int Render::mltGetSpaceLength(const GenTime pos, int track, bool fromBlankStart)
1591 if (!m_mltProducer) {
1592 kDebug() << "PLAYLIST NOT INITIALISED //////";
1595 Mlt::Producer parentProd(m_mltProducer->parent());
1596 if (parentProd.get_producer() == NULL) {
1597 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1601 Mlt::Service service(parentProd.get_service());
1602 Mlt::Tractor tractor(service);
1603 int insertPos = pos.frames(m_fps);
1605 Mlt::Producer trackProducer(tractor.track(track));
1606 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1607 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1608 if (!trackPlaylist.is_blank(clipIndex)) return -1;
1609 if (fromBlankStart) return trackPlaylist.clip_length(clipIndex);
1610 return trackPlaylist.clip_length(clipIndex) + trackPlaylist.clip_start(clipIndex) - insertPos;
1613 int Render::mltTrackDuration(int track)
1615 if (!m_mltProducer) {
1616 kDebug() << "PLAYLIST NOT INITIALISED //////";
1619 Mlt::Producer parentProd(m_mltProducer->parent());
1620 if (parentProd.get_producer() == NULL) {
1621 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1625 Mlt::Service service(parentProd.get_service());
1626 Mlt::Tractor tractor(service);
1628 Mlt::Producer trackProducer(tractor.track(track));
1629 return trackProducer.get_playtime() - 1;
1632 void Render::mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime duration, const GenTime timeOffset)
1634 if (!m_mltProducer) {
1635 kDebug() << "PLAYLIST NOT INITIALISED //////";
1638 Mlt::Producer parentProd(m_mltProducer->parent());
1639 if (parentProd.get_producer() == NULL) {
1640 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1643 //kDebug()<<"// CLP STRT LST: "<<trackClipStartList;
1644 //kDebug()<<"// TRA STRT LST: "<<trackTransitionStartList;
1646 Mlt::Service service(parentProd.get_service());
1647 Mlt::Tractor tractor(service);
1648 mlt_service_lock(service.get_service());
1649 int diff = duration.frames(m_fps);
1650 int offset = timeOffset.frames(m_fps);
1654 // insert space in one track only
1655 Mlt::Producer trackProducer(tractor.track(track));
1656 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1657 insertPos = trackClipStartList.value(track);
1658 if (insertPos != -1) {
1659 insertPos += offset;
1660 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1661 if (diff > 0) trackPlaylist.insert_blank(clipIndex, diff - 1);
1663 if (!trackPlaylist.is_blank(clipIndex)) clipIndex --;
1664 if (!trackPlaylist.is_blank(clipIndex)) kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
1665 int position = trackPlaylist.clip_start(clipIndex);
1666 trackPlaylist.remove_region(position, - diff - 1);
1668 trackPlaylist.consolidate_blanks(0);
1670 // now move transitions
1671 mlt_service serv = m_mltProducer->parent().get_service();
1672 mlt_service nextservice = mlt_service_get_producer(serv);
1673 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
1674 QString mlt_type = mlt_properties_get(properties, "mlt_type");
1675 QString resource = mlt_properties_get(properties, "mlt_service");
1677 while (mlt_type == "transition") {
1678 mlt_transition tr = (mlt_transition) nextservice;
1679 int currentTrack = mlt_transition_get_b_track(tr);
1680 int currentIn = (int) mlt_transition_get_in(tr);
1681 int currentOut = (int) mlt_transition_get_out(tr);
1682 insertPos = trackTransitionStartList.value(track);
1683 if (insertPos != -1) {
1684 insertPos += offset;
1685 if (track == currentTrack && currentOut > insertPos && resource != "mix") {
1686 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
1689 nextservice = mlt_service_producer(nextservice);
1690 if (nextservice == NULL) break;
1691 properties = MLT_SERVICE_PROPERTIES(nextservice);
1692 mlt_type = mlt_properties_get(properties, "mlt_type");
1693 resource = mlt_properties_get(properties, "mlt_service");
1696 int trackNb = tractor.count();
1697 while (trackNb > 1) {
1698 Mlt::Producer trackProducer(tractor.track(trackNb - 1));
1699 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1702 //int clipNb = trackPlaylist.count();
1703 insertPos = trackClipStartList.value(trackNb - 1);
1704 if (insertPos != -1) {
1705 insertPos += offset;
1707 /* kDebug()<<"-------------\nTRACK "<<trackNb<<" HAS "<<clipNb<<" CLPIS";
1708 kDebug() << "INSERT SPACE AT: "<<insertPos<<", DIFF: "<<diff<<", TK: "<<trackNb;
1709 for (int i = 0; i < clipNb; i++) {
1710 kDebug()<<"CLIP "<<i<<", START: "<<trackPlaylist.clip_start(i)<<", END: "<<trackPlaylist.clip_start(i) + trackPlaylist.clip_length(i);
1711 if (trackPlaylist.is_blank(i)) kDebug()<<"++ BLANK ++ ";
1712 kDebug()<<"-------------";
1714 kDebug()<<"END-------------";*/
1717 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1718 if (diff > 0) trackPlaylist.insert_blank(clipIndex, diff - 1);
1720 if (!trackPlaylist.is_blank(clipIndex)) clipIndex --;
1721 if (!trackPlaylist.is_blank(clipIndex)) kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
1722 int position = trackPlaylist.clip_start(clipIndex);
1723 trackPlaylist.remove_region(position, - diff - 1);
1725 trackPlaylist.consolidate_blanks(0);
1729 // now move transitions
1730 mlt_service serv = m_mltProducer->parent().get_service();
1731 mlt_service nextservice = mlt_service_get_producer(serv);
1732 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
1733 QString mlt_type = mlt_properties_get(properties, "mlt_type");
1734 QString resource = mlt_properties_get(properties, "mlt_service");
1736 while (mlt_type == "transition") {
1737 mlt_transition tr = (mlt_transition) nextservice;
1738 int currentIn = (int) mlt_transition_get_in(tr);
1739 int currentOut = (int) mlt_transition_get_out(tr);
1740 int currentTrack = mlt_transition_get_b_track(tr);
1741 insertPos = trackTransitionStartList.value(currentTrack);
1742 if (insertPos != -1) {
1743 insertPos += offset;
1744 if (currentOut > insertPos && resource != "mix") {
1745 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
1748 nextservice = mlt_service_producer(nextservice);
1749 if (nextservice == NULL) break;
1750 properties = MLT_SERVICE_PROPERTIES(nextservice);
1751 mlt_type = mlt_properties_get(properties, "mlt_type");
1752 resource = mlt_properties_get(properties, "mlt_service");
1755 mlt_service_unlock(service.get_service());
1757 m_mltConsumer->set("refresh", 1);
1761 void Render::mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest)
1763 Mlt::Service sourceService(source->get_service());
1764 Mlt::Service destService(dest->get_service());
1766 // move all effects to the correct producer
1768 Mlt::Filter *filter = sourceService.filter(ct);
1770 if (filter->get("kdenlive_ix") != 0) {
1771 sourceService.detach(*filter);
1772 destService.attach(*filter);
1774 filter = sourceService.filter(ct);
1778 int Render::mltChangeClipSpeed(ItemInfo info, double speed, double oldspeed, Mlt::Producer *prod)
1782 Mlt::Service service(m_mltProducer->parent().get_service());
1783 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
1784 //kDebug() << "Changing clip speed, set in and out: " << info.cropStart.frames(m_fps) << " to " << (info.endPos - info.startPos).frames(m_fps) - 1;
1785 Mlt::Tractor tractor(service);
1786 Mlt::Producer trackProducer(tractor.track(info.track));
1787 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1788 int startPos = info.startPos.frames(m_fps);
1789 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
1790 int clipLength = trackPlaylist.clip_length(clipIndex);
1792 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
1796 if (!clip->is_valid() || clip->is_blank()) {
1801 Mlt::Producer clipparent = clip->parent();
1802 if (!clipparent.is_valid() || clipparent.is_blank()) {
1808 QString serv = clipparent.get("mlt_service");
1809 QString id = clipparent.get("id");
1810 //kDebug() << "CLIP SERVICE: " << serv;
1811 if (serv == "avformat" && speed != 1.0) {
1812 mlt_service_lock(service.get_service());
1813 QString url = QString::fromUtf8(clipparent.get("resource"));
1814 url.append('?' + QString::number(speed));
1815 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1816 if (!slowprod || slowprod->get_producer() == NULL) {
1817 char *tmp = decodedString(url);
1818 slowprod = new Mlt::Producer(*m_mltProfile, "framebuffer", tmp);
1820 QString producerid = "slowmotion:" + id + ':' + QString::number(speed);
1821 tmp = decodedString(producerid);
1822 slowprod->set("id", tmp);
1824 m_slowmotionProducers.insert(url, slowprod);
1826 trackPlaylist.replace_with_blank(clipIndex);
1827 trackPlaylist.consolidate_blanks(0);
1828 // Check that the blank space is long enough for our new duration
1829 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1830 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
1832 if (clipIndex + 1 < trackPlaylist.count() && (startPos + clipLength / speed > blankEnd)) {
1833 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
1834 cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)(info.cropStart.frames(m_fps) / speed + maxLength.frames(m_fps) - 1));
1835 } else cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)((info.cropStart.frames(m_fps) + clipLength) / speed - 1));
1837 // move all effects to the correct producer
1838 mltPasteEffects(clip, cut);
1840 trackPlaylist.insert_at(startPos, *cut, 1);
1841 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1842 newLength = trackPlaylist.clip_length(clipIndex);
1843 mlt_service_unlock(service.get_service());
1844 } else if (speed == 1.0) {
1845 mlt_service_lock(service.get_service());
1847 trackPlaylist.replace_with_blank(clipIndex);
1848 trackPlaylist.consolidate_blanks(0);
1850 // Check that the blank space is long enough for our new duration
1851 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1852 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
1855 GenTime oldDuration = GenTime(clipLength, m_fps);
1856 GenTime newDuration = oldDuration * oldspeed;
1857 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + newDuration).frames(m_fps) > blankEnd) {
1858 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
1859 cut = prod->cut((int)(info.cropStart.frames(m_fps)), (int)(info.cropStart.frames(m_fps) + maxLength.frames(m_fps) - 1));
1860 } else cut = prod->cut((int)(info.cropStart.frames(m_fps)), (int)((info.cropStart + newDuration).frames(m_fps)) - 1);
1862 // move all effects to the correct producer
1863 mltPasteEffects(clip, cut);
1865 trackPlaylist.insert_at(startPos, *cut, 1);
1866 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1867 newLength = trackPlaylist.clip_length(clipIndex);
1868 mlt_service_unlock(service.get_service());
1870 } else if (serv == "framebuffer") {
1871 mlt_service_lock(service.get_service());
1872 QString url = QString::fromUtf8(clipparent.get("resource"));
1873 url = url.section('?', 0, 0);
1874 url.append('?' + QString::number(speed));
1875 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1876 if (!slowprod || slowprod->get_producer() == NULL) {
1877 char *tmp = decodedString(url);
1878 slowprod = new Mlt::Producer(*m_mltProfile, "framebuffer", tmp);
1880 QString producerid = "slowmotion:" + id.section(':', 1, 1) + ':' + QString::number(speed);
1881 tmp = decodedString(producerid);
1882 slowprod->set("id", tmp);
1884 m_slowmotionProducers.insert(url, slowprod);
1886 trackPlaylist.replace_with_blank(clipIndex);
1887 trackPlaylist.consolidate_blanks(0);
1889 GenTime oldDuration = GenTime(clipLength, m_fps);
1890 GenTime newDuration = oldDuration * oldspeed / speed;
1892 // Check that the blank space is long enough for our new duration
1893 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1894 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
1897 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + newDuration).frames(m_fps) > blankEnd) {
1898 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
1899 cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)(info.cropStart.frames(m_fps) / speed + maxLength.frames(m_fps) - 1));
1900 } else cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)((info.cropStart / speed + newDuration).frames(m_fps) - 1));
1902 // move all effects to the correct producer
1903 mltPasteEffects(clip, cut);
1905 trackPlaylist.insert_at(startPos, *cut, 1);
1906 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1907 newLength = trackPlaylist.clip_length(clipIndex);
1909 mlt_service_unlock(service.get_service());
1913 if (clipIndex + 1 == trackPlaylist.count()) mltCheckLength();
1914 m_isBlocked = false;
1918 bool Render::mltRemoveEffect(int track, GenTime position, QString index, bool updateIndex, bool doRefresh)
1920 kDebug() << "// TRYing to remove effect at: " << index;
1921 Mlt::Service service(m_mltProducer->parent().get_service());
1922 bool success = false;
1923 Mlt::Tractor tractor(service);
1924 Mlt::Producer trackProducer(tractor.track(track));
1925 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1926 //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
1927 Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
1929 kDebug() << " / / / CANNOT FIND CLIP TO REMOVE EFFECT";
1932 Mlt::Service clipService(clip->get_service());
1933 // if (tag.startsWith("ladspa")) tag = "ladspa";
1936 Mlt::Filter *filter = clipService.filter(ct);
1938 if ((index == "-1" && strcmp(filter->get("kdenlive_id"), "")) || filter->get("kdenlive_ix") == index) {// && filter->get("kdenlive_id") == id) {
1939 if (clipService.detach(*filter) == 0) success = true;
1940 kDebug() << " / / / DLEETED EFFECT: " << ct;
1941 } else if (updateIndex) {
1942 // Adjust the other effects index
1943 if (QString(filter->get("kdenlive_ix")).toInt() > index.toInt()) filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() - 1);
1946 filter = clipService.filter(ct);
1948 m_isBlocked = false;
1949 if (doRefresh) refresh();
1954 bool Render::mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh)
1957 Mlt::Service service(m_mltProducer->parent().get_service());
1959 Mlt::Tractor tractor(service);
1960 Mlt::Producer trackProducer(tractor.track(track));
1961 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1963 Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
1967 Mlt::Service clipService(clip->get_service());
1970 // temporarily remove all effects after insert point
1971 QList <Mlt::Filter *> filtersList;
1972 const int filter_ix = params.paramValue("kdenlive_ix").toInt();
1974 Mlt::Filter *filter = clipService.filter(ct);
1976 if (QString(filter->get("kdenlive_ix")).toInt() > filter_ix) {
1977 filtersList.append(filter);
1978 clipService.detach(*filter);
1980 filter = clipService.filter(ct);
1984 QString tag = params.paramValue("tag");
1985 kDebug() << " / / INSERTING EFFECT: " << tag;
1986 if (tag.startsWith("ladspa")) tag = "ladspa";
1987 char *filterTag = decodedString(tag);
1988 char *filterId = decodedString(params.paramValue("id"));
1989 QHash<QString, QString>::Iterator it;
1990 QString kfr = params.paramValue("keyframes");
1992 if (!kfr.isEmpty()) {
1993 QStringList keyFrames = kfr.split(';', QString::SkipEmptyParts);
1994 kDebug() << "// ADDING KEYFRAME EFFECT: " << params.paramValue("keyframes");
1995 char *starttag = decodedString(params.paramValue("starttag", "start"));
1996 char *endtag = decodedString(params.paramValue("endtag", "end"));
1997 kDebug() << "// ADDING KEYFRAME TAGS: " << starttag << ", " << endtag;
1998 int duration = clip->get_playtime();
1999 //double max = params.paramValue("max").toDouble();
2000 double min = params.paramValue("min").toDouble();
2001 double factor = params.paramValue("factor", "1").toDouble();
2002 params.removeParam("starttag");
2003 params.removeParam("endtag");
2004 params.removeParam("keyframes");
2005 params.removeParam("min");
2006 params.removeParam("max");
2007 params.removeParam("factor");
2009 for (int i = 0; i < keyFrames.size() - 1; ++i) {
2010 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2011 if (filter && filter->is_valid()) {
2012 filter->set("kdenlive_id", filterId);
2013 int x1 = keyFrames.at(i).section(':', 0, 0).toInt() + offset;
2014 double y1 = keyFrames.at(i).section(':', 1, 1).toDouble();
2015 int x2 = keyFrames.at(i + 1).section(':', 0, 0).toInt();
2016 double y2 = keyFrames.at(i + 1).section(':', 1, 1).toDouble();
2017 if (x2 == -1) x2 = duration;
2019 for (int j = 0; j < params.count(); j++) {
2020 char *name = decodedString(params.at(j).name());
2021 char *value = decodedString(params.at(j).value());
2022 filter->set(name, value);
2027 filter->set("in", x1);
2028 filter->set("out", x2);
2029 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2030 filter->set(starttag, QString::number((min + y1) / factor).toUtf8().data());
2031 filter->set(endtag, QString::number((min + y2) / factor).toUtf8().data());
2032 clipService.attach(*filter);
2039 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2040 if (filter && filter->is_valid())
2041 filter->set("kdenlive_id", filterId);
2043 kDebug() << "filter is NULL";
2044 m_isBlocked = false;
2048 params.removeParam("kdenlive_id");
2050 for (int j = 0; j < params.count(); j++) {
2051 char *name = decodedString(params.at(j).name());
2052 char *value = decodedString(params.at(j).value());
2053 filter->set(name, value);
2059 QString effectArgs = params.paramValue("id").section('_', 1);
2061 params.removeParam("id");
2062 params.removeParam("kdenlive_ix");
2063 params.removeParam("tag");
2064 params.removeParam("disabled");
2066 for (int j = 0; j < params.count(); j++) {
2067 effectArgs.append(' ' + params.at(j).value());
2069 //kDebug() << "SOX EFFECTS: " << effectArgs.simplified();
2070 char *value = decodedString(effectArgs.simplified());
2071 filter->set("effect", value);
2076 // attach filter to the clip
2077 clipService.attach(*filter);
2082 // re-add following filters
2083 for (int i = 0; i < filtersList.count(); i++) {
2084 clipService.attach(*(filtersList.at(i)));
2087 m_isBlocked = false;
2088 if (doRefresh) refresh();
2092 bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList params)
2094 QString index = params.paramValue("kdenlive_ix");
2095 QString tag = params.paramValue("tag");
2097 if (!params.paramValue("keyframes").isEmpty() || /*it.key().startsWith("#") || */tag.startsWith("ladspa") || tag == "sox" || tag == "autotrack_rectangle") {
2098 // This is a keyframe effect, to edit it, we remove it and re-add it.
2099 mltRemoveEffect(track, position, index, true);
2100 bool success = mltAddEffect(track, position, params);
2105 Mlt::Service service(m_mltProducer->parent().get_service());
2107 Mlt::Tractor tractor(service);
2108 Mlt::Producer trackProducer(tractor.track(track));
2109 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2110 //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
2111 Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
2113 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2116 Mlt::Service clipService(clip->get_service());
2119 Mlt::Filter *filter = clipService.filter(ct);
2121 if (filter->get("kdenlive_ix") == index) {
2125 filter = clipService.filter(ct);
2129 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT!!!!!";
2130 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2132 filter = clipService.filter(ct);
2133 QList <Mlt::Filter *> filtersList;
2135 if (QString(filter->get("kdenlive_ix")).toInt() > index.toInt()) {
2136 filtersList.append(filter);
2137 clipService.detach(*filter);
2139 filter = clipService.filter(ct);
2141 bool success = mltAddEffect(track, position, params);
2143 for (int i = 0; i < filtersList.count(); i++) {
2144 clipService.attach(*(filtersList.at(i)));
2147 m_isBlocked = false;
2151 for (int j = 0; j < params.count(); j++) {
2152 char *name = decodedString(params.at(j).name());
2153 char *value = decodedString(params.at(j).value());
2154 filter->set(name, value);
2159 m_isBlocked = false;
2164 void Render::mltMoveEffect(int track, GenTime position, int oldPos, int newPos)
2167 kDebug() << "MOVING EFFECT FROM " << oldPos << ", TO: " << newPos;
2168 Mlt::Service service(m_mltProducer->parent().get_service());
2170 Mlt::Tractor tractor(service);
2171 Mlt::Producer trackProducer(tractor.track(track));
2172 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2173 //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
2174 Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
2176 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2179 Mlt::Service clipService(clip->get_service());
2182 QList <Mlt::Filter *> filtersList;
2183 Mlt::Filter *filter = clipService.filter(ct);
2185 if (newPos > oldPos) {
2187 if (!found && QString(filter->get("kdenlive_ix")).toInt() == oldPos) {
2188 filter->set("kdenlive_ix", newPos);
2189 filtersList.append(filter);
2190 clipService.detach(*filter);
2191 filter = clipService.filter(ct);
2192 while (filter && QString(filter->get("kdenlive_ix")).toInt() <= newPos) {
2193 filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() - 1);
2195 filter = clipService.filter(ct);
2199 if (filter && QString(filter->get("kdenlive_ix")).toInt() > newPos) {
2200 filtersList.append(filter);
2201 clipService.detach(*filter);
2203 filter = clipService.filter(ct);
2207 if (QString(filter->get("kdenlive_ix")).toInt() == oldPos) {
2208 filter->set("kdenlive_ix", newPos);
2209 filtersList.append(filter);
2210 clipService.detach(*filter);
2212 filter = clipService.filter(ct);
2216 filter = clipService.filter(ct);
2218 int pos = QString(filter->get("kdenlive_ix")).toInt();
2219 if (pos >= newPos) {
2220 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
2221 filtersList.append(filter);
2222 clipService.detach(*filter);
2224 filter = clipService.filter(ct);
2228 for (int i = 0; i < filtersList.count(); i++) {
2229 clipService.attach(*(filtersList.at(i)));
2232 m_isBlocked = false;
2236 bool Render::mltResizeClipEnd(ItemInfo info, GenTime clipDuration)
2239 Mlt::Service service(m_mltProducer->parent().get_service());
2240 Mlt::Tractor tractor(service);
2241 Mlt::Producer trackProducer(tractor.track(info.track));
2242 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2244 /* // Display playlist info
2245 kDebug()<<"//////////// BEFORE RESIZE";
2246 for (int i = 0; i < trackPlaylist.count(); i++) {
2247 int blankStart = trackPlaylist.clip_start(i);
2248 int blankDuration = trackPlaylist.clip_length(i) - 1;
2250 if (trackPlaylist.is_blank(i)) blk = "(blank)";
2251 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
2254 if (trackPlaylist.is_blank_at((int) info.startPos.frames(m_fps))) {
2255 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
2256 m_isBlocked = false;
2259 mlt_service_lock(service.get_service());
2260 int clipIndex = trackPlaylist.get_clip_index_at((int) info.startPos.frames(m_fps));
2261 //kDebug() << "// SELECTED CLIP START: " << trackPlaylist.clip_start(clipIndex);
2262 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2263 int previousStart = clip->get_in();
2264 int newDuration = (int) clipDuration.frames(m_fps) - 1;
2265 int diff = newDuration - (trackPlaylist.clip_length(clipIndex) - 1);
2266 if (newDuration > clip->get_length()) {
2267 clip->parent().set("length", newDuration + 1);
2268 clip->set("length", newDuration + 1);
2270 if (newDuration > clip->get_out()) {
2271 clip->parent().set_in_and_out(0, newDuration + 1);
2272 clip->set_in_and_out(0, newDuration + 1);
2275 trackPlaylist.resize_clip(clipIndex, previousStart, newDuration + previousStart);
2276 trackPlaylist.consolidate_blanks(0);
2277 // skip to next clip
2279 //kDebug() << "//////// RESIZE CLIP: " << clipIndex << "( pos: " << info.startPos.frames(25) << "), DIFF: " << diff << ", CURRENT DUR: " << previousDuration << ", NEW DUR: " << newDuration << ", IX: " << clipIndex << ", MAX: " << trackPlaylist.count();
2281 // clip was made longer, trim next blank if there is one.
2282 if (clipIndex < trackPlaylist.count()) {
2283 // If this is not the last clip in playlist
2284 if (trackPlaylist.is_blank(clipIndex)) {
2285 int blankStart = trackPlaylist.clip_start(clipIndex);
2286 int blankDuration = trackPlaylist.clip_length(clipIndex) - 1;
2287 if (diff > blankDuration) kDebug() << "// ERROR blank clip is not large enough to get back required space!!!";
2288 if (diff - blankDuration == 1) {
2289 trackPlaylist.remove(clipIndex);
2290 } else trackPlaylist.remove_region(blankStart, diff - 1);
2292 kDebug() << "/// RESIZE ERROR, NXT CLIP IS NOT BLK: " << clipIndex;
2295 } else trackPlaylist.insert_blank(clipIndex, 0 - diff - 1);
2296 trackPlaylist.consolidate_blanks(0);
2297 mlt_service_unlock(service.get_service());
2299 if (info.track != 0 && clipIndex == trackPlaylist.count()) mltCheckLength();
2300 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
2301 //mltResizeTransparency(previousStart, previousStart, previousStart + newDuration, track, QString(clip->parent().get("id")).toInt());
2302 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
2303 ItemInfo transpinfo;
2304 transpinfo.startPos = info.startPos;
2305 transpinfo.endPos = info.startPos + clipDuration;
2306 transpinfo.track = info.track;
2307 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
2309 m_isBlocked = false;
2310 m_mltConsumer->set("refresh", 1);
2314 void Render::mltChangeTrackState(int track, bool mute, bool blind)
2316 Mlt::Service service(m_mltProducer->parent().get_service());
2317 Mlt::Tractor tractor(service);
2318 Mlt::Producer trackProducer(tractor.track(track));
2321 if (blind) trackProducer.set("hide", 3);
2322 else trackProducer.set("hide", 2);
2324 trackProducer.set("hide", 1);
2326 trackProducer.set("hide", 0);
2328 tractor.multitrack()->refresh();
2334 bool Render::mltResizeClipCrop(ItemInfo info, GenTime diff)
2336 Mlt::Service service(m_mltProducer->parent().get_service());
2337 int frameOffset = (int) diff.frames(m_fps);
2338 Mlt::Tractor tractor(service);
2339 Mlt::Producer trackProducer(tractor.track(info.track));
2340 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2341 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
2342 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
2345 mlt_service_lock(service.get_service());
2346 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
2347 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2349 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
2350 mlt_service_unlock(service.get_service());
2353 int previousStart = clip->get_in();
2355 int previousDuration = trackPlaylist.clip_length(clipIndex) - 1;
2357 trackPlaylist.resize_clip(clipIndex, previousStart + frameOffset, previousStart + previousDuration + frameOffset);
2358 m_isBlocked = false;
2359 mlt_service_unlock(service.get_service());
2360 m_mltConsumer->set("refresh", 1);
2364 bool Render::mltResizeClipStart(ItemInfo info, GenTime diff)
2366 //kDebug() << "//////// RSIZING CLIP from: "<<info.startPos.frames(25)<<" to "<<diff.frames(25);
2367 Mlt::Service service(m_mltProducer->parent().get_service());
2368 int moveFrame = (int) diff.frames(m_fps);
2369 Mlt::Tractor tractor(service);
2370 Mlt::Producer trackProducer(tractor.track(info.track));
2371 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2372 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
2373 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
2376 mlt_service_lock(service.get_service());
2377 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
2378 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2380 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
2381 mlt_service_unlock(service.get_service());
2384 int previousStart = clip->get_in();
2386 int previousDuration = trackPlaylist.clip_length(clipIndex) - 1;
2388 kDebug() << "RESIZE, old start: " << previousStart << ", PREV DUR: " << previousDuration << ", DIFF: " << moveFrame;
2389 trackPlaylist.resize_clip(clipIndex, previousStart + moveFrame, previousStart + previousDuration);
2390 if (moveFrame > 0) trackPlaylist.insert_blank(clipIndex, moveFrame - 1);
2392 //int midpos = info.startPos.frames(m_fps) + moveFrame - 1;
2393 int blankIndex = clipIndex - 1;
2394 int blankLength = trackPlaylist.clip_length(blankIndex);
2395 kDebug() << " + resizing blank length " << blankLength << ", SIZE DIFF: " << moveFrame;
2396 if (! trackPlaylist.is_blank(blankIndex)) {
2397 kDebug() << "WARNING, CLIP TO RESIZE IS NOT BLANK";
2399 if (blankLength + moveFrame == 0) trackPlaylist.remove(blankIndex);
2400 else trackPlaylist.resize_clip(blankIndex, 0, blankLength + moveFrame - 1);
2402 trackPlaylist.consolidate_blanks(0);
2403 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
2404 //mltResizeTransparency(previousStart, (int) moveEnd.frames(m_fps), (int) (moveEnd + out - in).frames(m_fps), track, QString(clip->parent().get("id")).toInt());
2405 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
2406 ItemInfo transpinfo;
2407 transpinfo.startPos = info.startPos + diff;
2408 transpinfo.endPos = info.startPos + diff + (info.endPos - info.startPos);
2409 transpinfo.track = info.track;
2410 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
2412 m_isBlocked = false;
2413 //m_mltConsumer->set("refresh", 1);
2414 mlt_service_unlock(service.get_service());
2415 m_mltConsumer->set("refresh", 1);
2419 bool Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd, Mlt::Producer *prod)
2421 return mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps), prod);
2425 void Render::mltUpdateClipProducer(int track, int pos, Mlt::Producer *prod)
2427 if (prod == NULL || !prod->is_valid()) {
2428 kDebug() << "// Warning, CLIP on track " << track << ", at: " << pos << " is invalid, cannot update it!!!";
2431 kDebug() << "NEW PROD ID: " << prod->get("id");
2433 kDebug() << "// TRYING TO UPDATE CLIP at: " << pos << ", TK: " << track;
2434 mlt_service_lock(m_mltConsumer->get_service());
2435 Mlt::Service service(m_mltProducer->parent().get_service());
2436 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
2438 Mlt::Tractor tractor(service);
2439 Mlt::Producer trackProducer(tractor.track(track));
2440 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2441 int clipIndex = trackPlaylist.get_clip_index_at(pos + 1);
2442 Mlt::Producer clipProducer(trackPlaylist.replace_with_blank(clipIndex));
2443 if (clipProducer.is_blank()) {
2444 kDebug() << "// ERROR UPDATING CLIP PROD";
2445 mlt_service_unlock(m_mltConsumer->get_service());
2449 Mlt::Producer *clip = prod->cut(clipProducer.get_in(), clipProducer.get_out());
2451 // move all effects to the correct producer
2452 mltPasteEffects(&clipProducer, clip);
2454 trackPlaylist.insert_at(pos, clip, 1);
2455 mlt_service_unlock(m_mltConsumer->get_service());
2459 bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd, Mlt::Producer *prod)
2463 Mlt::Service service(m_mltProducer->parent().get_service());
2464 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
2466 Mlt::Tractor tractor(service);
2467 mlt_service_lock(service.get_service());
2468 Mlt::Producer trackProducer(tractor.track(startTrack));
2469 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2470 int clipIndex = trackPlaylist.get_clip_index_at(moveStart);
2471 kDebug() << "////// LOOKING FOR CLIP TO MOVE, INDEX: " << clipIndex;
2472 bool checkLength = false;
2473 if (endTrack == startTrack) {
2474 Mlt::Producer clipProducer(trackPlaylist.replace_with_blank(clipIndex));
2475 if (!trackPlaylist.is_blank_at(moveEnd) || clipProducer.is_blank()) {
2476 // error, destination is not empty
2477 if (!trackPlaylist.is_blank_at(moveEnd)) trackPlaylist.insert_at(moveStart, clipProducer, 1);
2478 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
2479 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
2480 mlt_service_unlock(service.get_service());
2484 trackPlaylist.consolidate_blanks(0);
2485 int newIndex = trackPlaylist.insert_at(moveEnd, clipProducer, 1);
2486 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
2487 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
2489 if (newIndex + 1 == trackPlaylist.count()) checkLength = true;
2491 //mlt_service_unlock(service.get_service());
2493 Mlt::Producer destTrackProducer(tractor.track(endTrack));
2494 Mlt::Playlist destTrackPlaylist((mlt_playlist) destTrackProducer.get_service());
2495 if (!destTrackPlaylist.is_blank_at(moveEnd)) {
2496 // error, destination is not empty
2497 mlt_service_unlock(service.get_service());
2501 Mlt::Producer clipProducer(trackPlaylist.replace_with_blank(clipIndex));
2502 if (clipProducer.is_blank()) {
2503 // error, destination is not empty
2504 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
2505 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
2506 mlt_service_unlock(service.get_service());
2510 trackPlaylist.consolidate_blanks(0);
2511 destTrackPlaylist.consolidate_blanks(1);
2512 Mlt::Producer *clip;
2513 // check if we are moving a slowmotion producer
2514 QString serv = clipProducer.parent().get("mlt_service");
2515 QString currentid = clipProducer.parent().get("id");
2516 if (serv == "framebuffer" || currentid.endsWith("_video")) {
2517 clip = &clipProducer;
2520 // Special case: prod is null when using placeholder clips.
2521 // in that case, use the producer existing in playlist. Note that
2522 // it will bypass the one producer per track logic and might cause
2523 // Sound cracks if clip is moved so that it overlaps another copy of itself
2524 clip = clipProducer.cut(clipProducer.get_in(), clipProducer.get_out());
2525 } else clip = prod->cut(clipProducer.get_in(), clipProducer.get_out());
2528 // move all effects to the correct producer
2529 mltPasteEffects(&clipProducer, clip);
2531 int newIndex = destTrackPlaylist.insert_at(moveEnd, clip, 1);
2532 destTrackPlaylist.consolidate_blanks(0);
2533 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
2534 kDebug() << "//////// moving clip transparency";
2535 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
2537 if (clipIndex > trackPlaylist.count()) checkLength = true;
2538 else if (newIndex + 1 == destTrackPlaylist.count()) checkLength = true;
2541 mlt_service_unlock(service.get_service());
2542 if (checkLength) mltCheckLength();
2545 //m_mltConsumer->set("refresh", 1);
2549 bool Render::mltMoveTransition(QString type, int startTrack, int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut)
2551 int new_in = (int)newIn.frames(m_fps);
2552 int new_out = (int)newOut.frames(m_fps) - 1;
2553 if (new_in >= new_out) return false;
2555 mlt_service serv = m_mltProducer->parent().get_service();
2557 mlt_service_lock(serv);
2558 //m_mltConsumer->set("refresh", 0);
2561 mlt_service nextservice = mlt_service_get_producer(serv);
2562 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2563 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2564 QString resource = mlt_properties_get(properties, "mlt_service");
2565 int old_pos = (int)(oldIn.frames(m_fps) + oldOut.frames(m_fps)) / 2;
2567 while (mlt_type == "transition") {
2568 mlt_transition tr = (mlt_transition) nextservice;
2569 int currentTrack = mlt_transition_get_b_track(tr);
2570 int currentIn = (int) mlt_transition_get_in(tr);
2571 int currentOut = (int) mlt_transition_get_out(tr);
2573 if (resource == type && startTrack == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
2574 mlt_transition_set_in_and_out(tr, new_in, new_out);
2575 if (newTrack - startTrack != 0) {
2576 //kDebug() << "///// TRANSITION CHANGE TRACK. CUrrent (b): " << currentTrack << 'x' << mlt_transition_get_a_track(tr) << ", NEw: " << newTrack << 'x' << newTransitionTrack;
2578 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
2579 mlt_properties_set_int(properties, "a_track", newTransitionTrack);
2580 mlt_properties_set_int(properties, "b_track", newTrack);
2581 //kDebug() << "set new start & end :" << new_in << new_out<< "TR OFFSET: "<<trackOffset<<", TRACKS: "<<mlt_transition_get_a_track(tr)<<'x'<<mlt_transition_get_b_track(tr);
2585 nextservice = mlt_service_producer(nextservice);
2586 if (nextservice == NULL) break;
2587 properties = MLT_SERVICE_PROPERTIES(nextservice);
2588 mlt_type = mlt_properties_get(properties, "mlt_type");
2589 resource = mlt_properties_get(properties, "mlt_service");
2591 mlt_service_unlock(serv);
2594 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
2598 void Render::mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml)
2600 if (oldTag == tag) mltUpdateTransitionParams(tag, a_track, b_track, in, out, xml);
2602 mltDeleteTransition(oldTag, a_track, b_track, in, out, xml, false);
2603 mltAddTransition(tag, a_track, b_track, in, out, xml, false);
2606 //mltSavePlaylist();
2609 void Render::mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml)
2611 mlt_service serv = m_mltProducer->parent().get_service();
2612 mlt_service_lock(serv);
2615 mlt_service nextservice = mlt_service_get_producer(serv);
2616 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2617 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2618 QString resource = mlt_properties_get(properties, "mlt_service");
2619 int in_pos = (int) in.frames(m_fps);
2620 int out_pos = (int) out.frames(m_fps) - 1;
2622 while (mlt_type == "transition") {
2623 mlt_transition tr = (mlt_transition) nextservice;
2624 int currentTrack = mlt_transition_get_b_track(tr);
2625 int currentBTrack = mlt_transition_get_a_track(tr);
2626 int currentIn = (int) mlt_transition_get_in(tr);
2627 int currentOut = (int) mlt_transition_get_out(tr);
2629 // kDebug()<<"Looking for transition : " << currentIn <<'x'<<currentOut<< ", OLD oNE: "<<in_pos<<'x'<<out_pos;
2631 if (resource == type && b_track == currentTrack && currentIn == in_pos && currentOut == out_pos) {
2632 QMap<QString, QString> map = mltGetTransitionParamsFromXml(xml);
2633 QMap<QString, QString>::Iterator it;
2635 mlt_properties transproperties = MLT_TRANSITION_PROPERTIES(tr);
2636 mlt_properties_set_int(transproperties, "force_track", xml.attribute("force_track").toInt());
2637 // update the transition id in case it uses the same MLT service but different Kdenlive id
2638 char *tmp = decodedString(xml.attribute("id"));
2639 mlt_properties_set(transproperties, "kdenlive_id", tmp);
2641 if (currentBTrack != a_track) {
2642 mlt_properties_set_int(properties, "a_track", a_track);
2644 for (it = map.begin(); it != map.end(); ++it) {
2646 char *name = decodedString(key);
2647 char *value = decodedString(it.value());
2648 mlt_properties_set(transproperties, name, value);
2649 //kDebug() << " ------ UPDATING TRANS PARAM: " << name << ": " << value;
2650 //filter->set("kdenlive_id", id);
2656 nextservice = mlt_service_producer(nextservice);
2657 if (nextservice == NULL) break;
2658 properties = MLT_SERVICE_PROPERTIES(nextservice);
2659 mlt_type = mlt_properties_get(properties, "mlt_type");
2660 resource = mlt_properties_get(properties, "mlt_service");
2662 mlt_service_unlock(serv);
2665 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
2668 void Render::mltDeleteTransition(QString tag, int /*a_track*/, int b_track, GenTime in, GenTime out, QDomElement /*xml*/, bool /*do_refresh*/)
2670 mlt_service serv = m_mltProducer->parent().get_service();
2672 mlt_service_lock(serv);
2674 Mlt::Service service(serv);
2675 Mlt::Tractor tractor(service);
2676 Mlt::Field *field = tractor.field();
2678 //if (do_refresh) m_mltConsumer->set("refresh", 0);
2680 mlt_service nextservice = mlt_service_get_producer(serv);
2681 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2682 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2683 QString resource = mlt_properties_get(properties, "mlt_service");
2685 const int old_pos = (int)((in + out).frames(m_fps) / 2);
2686 kDebug() << " del trans pos: " << in.frames(25) << "-" << out.frames(25);
2688 while (mlt_type == "transition") {
2689 mlt_transition tr = (mlt_transition) nextservice;
2690 int currentTrack = mlt_transition_get_b_track(tr);
2691 int currentIn = (int) mlt_transition_get_in(tr);
2692 int currentOut = (int) mlt_transition_get_out(tr);
2693 //kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
2695 if (resource == tag && b_track == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
2696 mlt_field_disconnect_service(field->get_field(), nextservice);
2699 nextservice = mlt_service_producer(nextservice);
2700 if (nextservice == NULL) break;
2701 properties = MLT_SERVICE_PROPERTIES(nextservice);
2702 mlt_type = mlt_properties_get(properties, "mlt_type");
2703 resource = mlt_properties_get(properties, "mlt_service");
2705 mlt_service_unlock(serv);
2708 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
2711 QMap<QString, QString> Render::mltGetTransitionParamsFromXml(QDomElement xml)
2713 QDomNodeList attribs = xml.elementsByTagName("parameter");
2714 QMap<QString, QString> map;
2715 for (int i = 0; i < attribs.count(); i++) {
2716 QDomElement e = attribs.item(i).toElement();
2717 QString name = e.attribute("name");
2718 //kDebug()<<"-- TRANSITION PARAM: "<<name<<" = "<< e.attribute("name")<<" / " << e.attribute("value");
2719 map[name] = e.attribute("default");
2720 if (!e.attribute("value").isEmpty()) {
2721 map[name] = e.attribute("value");
2723 if (!e.attribute("factor").isEmpty() && e.attribute("factor").toDouble() > 0) {
2724 map[name] = QString::number(map[name].toDouble() / e.attribute("factor").toDouble());
2725 //map[name]=map[name].replace(".",","); //FIXME how to solve locale conversion of . ,
2728 if (e.attribute("namedesc").contains(';')) {
2729 QString format = e.attribute("format");
2730 QStringList separators = format.split("%d", QString::SkipEmptyParts);
2731 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
2733 QTextStream txtNeu(&neu);
2734 if (values.size() > 0)
2735 txtNeu << (int)values[0].toDouble();
2737 for (i = 0; i < separators.size() && i + 1 < values.size(); i++) {
2738 txtNeu << separators[i];
2739 txtNeu << (int)(values[i+1].toDouble());
2741 if (i < separators.size())
2742 txtNeu << separators[i];
2743 map[e.attribute("name")] = neu;
2750 void Render::mltAddClipTransparency(ItemInfo info, int transitiontrack, int id)
2752 kDebug() << "///////// ADDING CLIP TRANSPARENCY AT: " << info.startPos.frames(25);
2753 Mlt::Service service(m_mltProducer->parent().get_service());
2754 Mlt::Tractor tractor(service);
2755 Mlt::Field *field = tractor.field();
2757 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
2758 transition->set_in_and_out((int) info.startPos.frames(m_fps), (int) info.endPos.frames(m_fps) - 1);
2759 transition->set("transparency", id);
2760 transition->set("fill", 1);
2761 transition->set("internal_added", 237);
2762 field->plant_transition(*transition, transitiontrack, info.track);
2766 void Render::mltDeleteTransparency(int pos, int track, int id)
2768 Mlt::Service service(m_mltProducer->parent().get_service());
2769 Mlt::Tractor tractor(service);
2770 Mlt::Field *field = tractor.field();
2772 //if (do_refresh) m_mltConsumer->set("refresh", 0);
2773 mlt_service serv = m_mltProducer->parent().get_service();
2775 mlt_service nextservice = mlt_service_get_producer(serv);
2776 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2777 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2778 QString resource = mlt_properties_get(properties, "mlt_service");
2780 while (mlt_type == "transition") {
2781 mlt_transition tr = (mlt_transition) nextservice;
2782 int currentTrack = mlt_transition_get_b_track(tr);
2783 int currentIn = (int) mlt_transition_get_in(tr);
2784 int currentOut = (int) mlt_transition_get_out(tr);
2785 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
2786 kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
2788 if (resource == "composite" && track == currentTrack && currentIn == pos && transitionId == id) {
2789 //kDebug() << " / / / / /DELETE TRANS DOOOMNE";
2790 mlt_field_disconnect_service(field->get_field(), nextservice);
2793 nextservice = mlt_service_producer(nextservice);
2794 if (nextservice == NULL) break;
2795 properties = MLT_SERVICE_PROPERTIES(nextservice);
2796 mlt_type = mlt_properties_get(properties, "mlt_type");
2797 resource = mlt_properties_get(properties, "mlt_service");
2799 //if (do_refresh) m_mltConsumer->set("refresh", 1);
2802 void Render::mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id)
2804 Mlt::Service service(m_mltProducer->parent().get_service());
2805 Mlt::Tractor tractor(service);
2807 mlt_service_lock(service.get_service());
2808 m_mltConsumer->set("refresh", 0);
2811 mlt_service serv = m_mltProducer->parent().get_service();
2812 mlt_service nextservice = mlt_service_get_producer(serv);
2813 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2814 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2815 QString resource = mlt_properties_get(properties, "mlt_service");
2816 kDebug() << "// resize transpar from: " << oldStart << ", TO: " << newStart << 'x' << newEnd << ", " << track << ", " << id;
2817 while (mlt_type == "transition") {
2818 mlt_transition tr = (mlt_transition) nextservice;
2819 int currentTrack = mlt_transition_get_b_track(tr);
2820 int currentIn = (int) mlt_transition_get_in(tr);
2821 //mlt_properties props = MLT_TRANSITION_PROPERTIES(tr);
2822 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
2823 kDebug() << "// resize transpar current in: " << currentIn << ", Track: " << currentTrack << ", id: " << id << 'x' << transitionId ;
2824 if (resource == "composite" && track == currentTrack && currentIn == oldStart && transitionId == id) {
2825 kDebug() << " / / / / /RESIZE TRANS TO: " << newStart << 'x' << newEnd;
2826 mlt_transition_set_in_and_out(tr, newStart, newEnd);
2829 nextservice = mlt_service_producer(nextservice);
2830 if (nextservice == NULL) break;
2831 properties = MLT_SERVICE_PROPERTIES(nextservice);
2832 mlt_type = mlt_properties_get(properties, "mlt_type");
2833 resource = mlt_properties_get(properties, "mlt_service");
2835 mlt_service_unlock(service.get_service());
2837 if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
2841 void Render::mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id)
2843 Mlt::Service service(m_mltProducer->parent().get_service());
2844 Mlt::Tractor tractor(service);
2846 mlt_service_lock(service.get_service());
2847 m_mltConsumer->set("refresh", 0);
2850 mlt_service serv = m_mltProducer->parent().get_service();
2851 mlt_service nextservice = mlt_service_get_producer(serv);
2852 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2853 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2854 QString resource = mlt_properties_get(properties, "mlt_service");
2856 while (mlt_type == "transition") {
2857 mlt_transition tr = (mlt_transition) nextservice;
2858 int currentTrack = mlt_transition_get_b_track(tr);
2859 int currentaTrack = mlt_transition_get_a_track(tr);
2860 int currentIn = (int) mlt_transition_get_in(tr);
2861 int currentOut = (int) mlt_transition_get_out(tr);
2862 //mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
2863 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
2864 //kDebug()<<" + TRANSITION "<<id<<" == "<<transitionId<<", START TMIE: "<<currentIn<<", LOOK FR: "<<startTime<<", TRACK: "<<currentTrack<<'x'<<startTrack;
2865 if (resource == "composite" && transitionId == id && startTime == currentIn && startTrack == currentTrack) {
2866 kDebug() << "//////MOVING";
2867 mlt_transition_set_in_and_out(tr, endTime, endTime + currentOut - currentIn);
2868 if (endTrack != startTrack) {
2869 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
2870 mlt_properties_set_int(properties, "a_track", currentaTrack + endTrack - currentTrack);
2871 mlt_properties_set_int(properties, "b_track", endTrack);
2875 nextservice = mlt_service_producer(nextservice);
2876 if (nextservice == NULL) break;
2877 properties = MLT_SERVICE_PROPERTIES(nextservice);
2878 mlt_type = mlt_properties_get(properties, "mlt_type");
2879 resource = mlt_properties_get(properties, "mlt_service");
2882 mlt_service_unlock(service.get_service());
2883 m_mltConsumer->set("refresh", 1);
2887 bool Render::mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool do_refresh)
2889 if (in >= out) return false;
2890 QMap<QString, QString> args = mltGetTransitionParamsFromXml(xml);
2891 Mlt::Service service(m_mltProducer->parent().get_service());
2893 Mlt::Tractor tractor(service);
2894 Mlt::Field *field = tractor.field();
2896 char *transId = decodedString(tag);
2897 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, transId);
2898 if (out != GenTime())
2899 transition->set_in_and_out((int) in.frames(m_fps), (int) out.frames(m_fps) - 1);
2900 QMap<QString, QString>::Iterator it;
2902 if (xml.attribute("automatic") == "1") transition->set("automatic", 1);
2903 //kDebug() << " ------ ADDING TRANSITION PARAMs: " << args.count();
2904 if (xml.hasAttribute("id"))
2905 transition->set("kdenlive_id", xml.attribute("id").toUtf8().constData());
2907 for (it = args.begin(); it != args.end(); ++it) {
2909 char *name = decodedString(key);
2910 char *value = decodedString(it.value());
2911 if (it.value().isEmpty() == false) transition->set(name, value);
2912 //kDebug() << " ------ ADDING TRANS PARAM: " << name << ": " << value;
2916 // attach filter to the clip
2917 field->plant_transition(*transition, a_track, b_track);
2919 if (do_refresh) refresh();
2923 void Render::mltSavePlaylist()
2925 kWarning() << "// UPDATING PLAYLIST TO DISK++++++++++++++++";
2926 Mlt::Consumer fileConsumer(*m_mltProfile, "xml");
2927 fileConsumer.set("resource", "/tmp/playlist.mlt");
2929 Mlt::Service service(m_mltProducer->get_service());
2931 fileConsumer.connect(service);
2932 fileConsumer.start();
2935 QList <Mlt::Producer *> Render::producersList()
2937 QList <Mlt::Producer *> prods;
2938 if (m_mltProducer == NULL) return prods;
2939 Mlt::Service service(m_mltProducer->parent().get_service());
2940 if (service.type() != tractor_type) return prods;
2941 Mlt::Tractor tractor(service);
2944 int trackNb = tractor.count();
2945 for (int t = 1; t < trackNb; t++) {
2946 Mlt::Producer *tt = tractor.track(t);
2947 Mlt::Producer trackProducer(tt);
2949 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2950 int clipNb = trackPlaylist.count();
2951 //kDebug() << "// PARSING SCENE TRACK: " << t << ", CLIPS: " << clipNb;
2952 for (int i = 0; i < clipNb; i++) {
2953 Mlt::Producer *c = trackPlaylist.get_clip(i);
2954 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
2956 if (!nprod->is_blank() && !ids.contains(nprod->get("id"))) {
2957 ids.append(nprod->get("id"));
2958 prods.append(nprod);
2959 } else delete nprod;
2967 void Render::fillSlowMotionProducers()
2969 if (m_mltProducer == NULL) return;
2970 Mlt::Service service(m_mltProducer->parent().get_service());
2971 if (service.type() != tractor_type) return;
2973 Mlt::Tractor tractor(service);
2975 int trackNb = tractor.count();
2976 for (int t = 1; t < trackNb; t++) {
2977 Mlt::Producer *tt = tractor.track(t);
2978 Mlt::Producer trackProducer(tt);
2980 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2981 int clipNb = trackPlaylist.count();
2982 for (int i = 0; i < clipNb; i++) {
2983 Mlt::Producer *c = trackPlaylist.get_clip(i);
2984 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
2986 QString id = nprod->get("id");
2987 if (id.startsWith("slowmotion:") && !nprod->is_blank()) {
2988 // this is a slowmotion producer, add it to the list
2989 QString url = QString::fromUtf8(nprod->get("resource"));
2990 if (!m_slowmotionProducers.contains(url)) {
2991 m_slowmotionProducers.insert(url, nprod);
2993 } else delete nprod;
3000 void Render::mltInsertTrack(int ix, bool videoTrack)
3005 Mlt::Service service(m_mltProducer->parent().get_service());
3006 mlt_service_lock(service.get_service());
3007 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
3009 Mlt::Tractor tractor(service);
3011 Mlt::Playlist playlist;
3012 int ct = tractor.count();
3014 kDebug() << "// ERROR, TRYING TO insert TRACK " << ix << ", max: " << ct;
3020 Mlt::Producer *prodToMove = new Mlt::Producer(tractor.track(pos));
3021 tractor.set_track(playlist, pos);
3022 Mlt::Producer newProd(tractor.track(pos));
3023 if (!videoTrack) newProd.set("hide", 1);
3025 for (; pos <= ct; pos++) {
3026 Mlt::Producer *prodToMove2 = new Mlt::Producer(tractor.track(pos));
3027 tractor.set_track(*prodToMove, pos);
3028 prodToMove = prodToMove2;
3031 tractor.set_track(playlist, ix);
3032 Mlt::Producer newProd(tractor.track(ix));
3033 if (!videoTrack) newProd.set("hide", 1);
3037 mlt_service serv = m_mltProducer->parent().get_service();
3038 mlt_service nextservice = mlt_service_get_producer(serv);
3039 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3040 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3041 QString resource = mlt_properties_get(properties, "mlt_service");
3043 while (mlt_type == "transition") {
3044 if (resource != "mix") {
3045 mlt_transition tr = (mlt_transition) nextservice;
3046 int currentTrack = mlt_transition_get_b_track(tr);
3047 int currentaTrack = mlt_transition_get_a_track(tr);
3048 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3050 if (currentTrack >= ix) {
3051 mlt_properties_set_int(properties, "b_track", currentTrack + 1);
3052 mlt_properties_set_int(properties, "a_track", currentaTrack + 1);
3055 nextservice = mlt_service_producer(nextservice);
3056 if (nextservice == NULL) break;
3057 properties = MLT_SERVICE_PROPERTIES(nextservice);
3058 mlt_type = mlt_properties_get(properties, "mlt_type");
3059 resource = mlt_properties_get(properties, "mlt_service");
3062 // Add audio mix transition to last track
3063 Mlt::Field *field = tractor.field();
3064 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "mix");
3065 transition->set("a_track", 1);
3066 transition->set("b_track", ct);
3067 transition->set("always_active", 1);
3068 transition->set("internal_added", 237);
3069 transition->set("combine", 1);
3070 field->plant_transition(*transition, 1, ct);
3071 //mlt_service_unlock(m_mltConsumer->get_service());
3072 mlt_service_unlock(service.get_service());
3073 //tractor.multitrack()->refresh();
3074 //tractor.refresh();
3076 blockSignals(false);
3080 void Render::mltDeleteTrack(int ix)
3083 doc.setContent(sceneList(), false);
3084 int tracksCount = doc.elementsByTagName("track").count() - 1;
3085 QDomNode track = doc.elementsByTagName("track").at(ix);
3086 QDomNode tractor = doc.elementsByTagName("tractor").at(0);
3087 QDomNodeList transitions = doc.elementsByTagName("transition");
3088 for (int i = 0; i < transitions.count(); i++) {
3089 QDomElement e = transitions.at(i).toElement();
3090 QDomNodeList props = e.elementsByTagName("property");
3091 QMap <QString, QString> mappedProps;
3092 for (int j = 0; j < props.count(); j++) {
3093 QDomElement f = props.at(j).toElement();
3094 mappedProps.insert(f.attribute("name"), f.firstChild().nodeValue());
3096 if (mappedProps.value("mlt_service") == "mix" && mappedProps.value("b_track").toInt() == tracksCount) {
3097 tractor.removeChild(transitions.at(i));
3099 } else if (mappedProps.value("mlt_service") != "mix" && (mappedProps.value("b_track").toInt() >= ix || mappedProps.value("a_track").toInt() >= ix)) {
3100 // Transition needs to be moved
3101 int a_track = mappedProps.value("a_track").toInt();
3102 int b_track = mappedProps.value("b_track").toInt();
3103 if (a_track > 0 && a_track >= ix) a_track --;
3104 if (b_track > 0 && b_track >= ix) b_track --;
3105 for (int j = 0; j < props.count(); j++) {
3106 QDomElement f = props.at(j).toElement();
3107 if (f.attribute("name") == "a_track") f.firstChild().setNodeValue(QString::number(a_track));
3108 else if (f.attribute("name") == "b_track") f.firstChild().setNodeValue(QString::number(b_track));
3113 tractor.removeChild(track);
3114 //kDebug() << "/////////// RESULT SCENE: \n" << doc.toString();
3115 setSceneList(doc.toString(), m_framePosition);
3120 void Render::updatePreviewSettings()
3122 kDebug() << "////// RESTARTING CONSUMER";
3123 if (!m_mltConsumer || !m_mltProducer) return;
3124 if (m_mltProducer->get_playtime() == 0) return;
3125 Mlt::Service service(m_mltProducer->parent().get_service());
3126 if (service.type() != tractor_type) return;
3128 //m_mltConsumer->set("refresh", 0);
3129 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
3130 m_mltConsumer->purge();
3131 QString scene = sceneList();
3133 if (m_mltProducer) {
3134 pos = m_mltProducer->position();
3135 delete m_mltProducer;
3137 m_mltProducer = NULL;
3138 setSceneList(scene, pos);
3141 #include "renderer.moc"