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>
45 static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr)
47 // detect if the producer has finished playing. Is there a better way to do it?
48 if (self->m_isBlocked) return;
49 if (mlt_properties_get_double(MLT_FRAME_PROPERTIES(frame_ptr), "_speed") == 0.0) {
50 self->emitConsumerStopped();
52 self->emitFrameNumber(mlt_frame_get_position(frame_ptr));
56 Render::Render(const QString & rendererName, int winid, int /* extid */, QWidget *parent) :
69 kDebug() << "////////// USING PROFILE: " << (char*)KdenliveSettings::current_profile().toUtf8().data();
70 m_refreshTimer = new QTimer(this);
71 connect(m_refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
73 /*if (rendererName == "project") m_monitorId = 10000;
74 else m_monitorId = 10001;*/
75 m_osdTimer = new QTimer(this);
76 connect(m_osdTimer, SIGNAL(timeout()), this, SLOT(slotOsdTimeout()));
80 m_mltProducer = m_blackClip->cut(0, 50);
81 m_mltConsumer->connect(*m_mltProducer);
82 m_mltProducer->set_speed(0.0);
92 void Render::closeMlt()
95 delete m_refreshTimer;
103 void Render::buildConsumer()
106 m_activeProfile = KdenliveSettings::current_profile();
107 tmp = decodedString(m_activeProfile);
108 setenv("MLT_PROFILE", tmp, 1);
112 m_mltProfile = new Mlt::Profile(tmp);
115 QString videoDriver = KdenliveSettings::videodrivername();
116 if (!videoDriver.isEmpty()) {
117 if (videoDriver == "x11_noaccel") {
118 setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
121 unsetenv("SDL_VIDEO_YUV_HWACCEL");
124 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
126 m_mltConsumer = new Mlt::Consumer(*m_mltProfile , "sdl_preview");
127 m_mltConsumer->set("resize", 1);
128 m_mltConsumer->set("window_id", m_winid);
129 m_mltConsumer->set("terminate_on_pause", 1);
130 tmp = decodedString(KdenliveSettings::window_background().name());
131 m_mltConsumer->set("window_background", tmp);
134 // FIXME: the event object returned by the listen gets leaked...
135 m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_show);
136 m_mltConsumer->set("rescale", "nearest");
138 QString audioDevice = KdenliveSettings::audiodevicename();
139 if (!audioDevice.isEmpty()) {
140 tmp = decodedString(audioDevice);
141 m_mltConsumer->set("audio_device", tmp);
145 if (!videoDriver.isEmpty()) {
146 tmp = decodedString(videoDriver);
147 m_mltConsumer->set("video_driver", tmp);
151 QString audioDriver = KdenliveSettings::audiodrivername();
152 if (audioDriver.isEmpty())
153 audioDriver = KdenliveSettings::autoaudiodrivername();
154 if (!audioDriver.isEmpty()) {
155 tmp = decodedString(audioDriver);
156 m_mltConsumer->set("audio_driver", tmp);
161 m_mltConsumer->set("progressive", 1);
162 m_mltConsumer->set("audio_buffer", 1024);
163 m_mltConsumer->set("frequency", 48000);
165 m_blackClip = new Mlt::Producer(*m_mltProfile , "colour", "black");
166 m_blackClip->set("id", "black");
170 int Render::resetProfile()
172 if (!m_mltConsumer) return 0;
173 if (m_activeProfile == KdenliveSettings::current_profile()) {
174 kDebug() << "reset to same profile, nothing to do";
177 kDebug() << "// RESETTING PROFILE FROM: " << m_activeProfile << " TO: " << KdenliveSettings::current_profile();
178 if (m_isSplitView) slotSplitView(false);
179 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
180 m_mltConsumer->purge();
181 delete m_mltConsumer;
182 m_mltConsumer = NULL;
183 QString scene = sceneList();
186 pos = m_mltProducer->position();
187 delete m_mltProducer;
189 m_mltProducer = NULL;
191 //WARNING: Trying to delete the profile will crash when trying to display a clip afterwards...
192 /*if (m_mltProfile) delete m_mltProfile;
193 m_mltProfile = NULL;*/
197 //kDebug() << "//RESET WITHSCENE: " << scene;
198 setSceneList(scene, pos);
200 /*char *tmp = decodedString(scene);
201 Mlt::Producer *producer = new Mlt::Producer(*m_mltProfile , "xml-string", tmp);
203 m_mltProducer = producer;
204 m_blackClip = new Mlt::Producer(*m_mltProfile , "colour", "black");
205 m_mltProducer->optimise();
206 m_mltProducer->set_speed(0);
209 //delete m_mltProfile;
210 // mlt_properties properties = MLT_CONSUMER_PROPERTIES(m_mltConsumer->get_consumer());
211 //mlt_profile prof = m_mltProfile->get_profile();
212 //mlt_properties_set_data(properties, "_profile", prof, 0, (mlt_destructor)mlt_profile_close, NULL);
213 //mlt_properties_set(properties, "profile", "hdv_1080_50i");
214 //m_mltConsumer->set("profile", (char *) profile.toUtf8().data());
215 //m_mltProfile = new Mlt::Profile((char*) profile.toUtf8().data());
217 //apply_profile_properties( m_mltProfile, m_mltConsumer->get_consumer(), properties );
222 /** Wraps the VEML command of the same name; Seeks the renderer clip to the given time. */
223 void Render::seek(GenTime time)
228 m_mltProducer->seek((int)(time.frames(m_fps)));
233 char *Render::decodedString(QString str)
235 /*QCString fn = QFile::encodeName(str);
236 char *t = new char[fn.length() + 1];
237 strcpy(t, (const char *)fn);*/
239 return (char *) qstrdup(str.toUtf8().data()); //toLatin1
243 /*QPixmap Render::frameThumbnail(Mlt::Frame *frame, int width, int height, bool border) {
244 QPixmap pix(width, height);
246 mlt_image_format format = mlt_image_rgb24a;
247 uint8_t *thumb = frame->get_image(format, width, height);
248 QImage image(thumb, width, height, QImage::Format_ARGB32);
250 if (!image.isNull()) {
251 pix = pix.fromImage(image);
253 QPainter painter(&pix);
254 painter.drawRect(0, 0, width - 1, height - 1);
256 } else pix.fill(Qt::black);
260 int Render::renderWidth() const
262 return (int)(m_mltProfile->height() * m_mltProfile->dar());
265 int Render::renderHeight() const
267 return m_mltProfile->height();
270 QPixmap Render::extractFrame(int frame_position, int width, int height)
273 width = renderWidth();
274 height = renderHeight();
275 } else if (width % 2 == 1) width++;
276 QPixmap pix(width, height);
277 if (!m_mltProducer) {
281 return KThumb::getFrame(m_mltProducer, frame_position, width, height);
284 QPixmap Render::getImageThumbnail(KUrl url, int /*width*/, int /*height*/)
288 if (url.fileName().startsWith(".all.")) { // check for slideshow
289 QString fileType = url.fileName().right(3);
291 QStringList::Iterator it;
293 QDir dir(url.directory());
295 filter << "*." + fileType;
296 filter << "*." + fileType.toUpper();
297 more = dir.entryList(filter, QDir::Files);
298 im.load(url.directory() + '/' + more.at(0));
299 } else im.load(url.path());
300 //pixmap = im.scaled(width, height);
305 QPixmap Render::getVideoThumbnail(char *profile, QString file, int frame_position, int width, int height) {
306 QPixmap pix(width, height);
307 char *tmp = decodedString(file);
308 Mlt::Profile *prof = new Mlt::Profile(profile);
309 Mlt::Producer m_producer(*prof, tmp);
311 if (m_producer.is_blank()) {
316 Mlt::Filter m_convert(*prof, "avcolour_space");
317 m_convert.set("forced", mlt_image_rgb24a);
318 m_producer.attach(m_convert);
319 m_producer.seek(frame_position);
320 Mlt::Frame * frame = m_producer.get_frame();
322 pix = frameThumbnail(frame, width, height, true);
325 if (prof) delete prof;
330 void Render::getImage(KUrl url, int frame_position, QPoint size)
332 char *tmp = decodedString(url.path());
333 Mlt::Producer m_producer(tmp);
335 if (m_producer.is_blank()) {
338 Mlt::Filter m_convert("avcolour_space");
339 m_convert.set("forced", mlt_image_rgb24a);
340 m_producer.attach(m_convert);
341 m_producer.seek(frame_position);
343 Mlt::Frame * frame = m_producer.get_frame();
346 QPixmap pix = frameThumbnail(frame, size.x(), size.y(), true);
348 emit replyGetImage(url, frame_position, pix, size.x(), size.y());
352 /* Create thumbnail for color */
353 /*void Render::getImage(int id, QString color, QPoint size)
355 QPixmap pixmap(size.x() - 2, size.y() - 2);
356 color = color.replace(0, 2, "#");
357 color = color.left(7);
358 pixmap.fill(QColor(color));
359 QPixmap result(size.x(), size.y());
360 result.fill(Qt::black);
361 //copyBlt(&result, 1, 1, &pixmap, 0, 0, size.x() - 2, size.y() - 2);
362 emit replyGetImage(id, result, size.x(), size.y());
366 /* Create thumbnail for image */
367 /*void Render::getImage(KUrl url, QPoint size)
371 if (url.fileName().startsWith(".all.")) { // check for slideshow
372 QString fileType = url.fileName().right(3);
374 QStringList::Iterator it;
376 QDir dir( url.directory() );
377 more = dir.entryList( QDir::Files );
378 for ( it = more.begin() ; it != more.end() ; ++it ) {
379 if ((*it).endsWith("."+fileType, Qt::CaseInsensitive)) {
380 if (!im.load(url.directory() + '/' + *it))
381 kDebug()<<"++ ERROR LOADIN IMAGE: "<<url.directory() + '/' + *it;
386 else im.load(url.path());
388 //pixmap = im.smoothScale(size.x() - 2, size.y() - 2);
389 QPixmap result(size.x(), size.y());
390 result.fill(Qt::black);
391 //copyBlt(&result, 1, 1, &pixmap, 0, 0, size.x() - 2, size.y() - 2);
392 emit replyGetImage(url, 1, result, size.x(), size.y());
396 double Render::consumerRatio() const
398 if (!m_mltConsumer) return 1.0;
399 return (m_mltConsumer->get_double("aspect_ratio_num") / m_mltConsumer->get_double("aspect_ratio_den"));
403 int Render::getLength()
407 // kDebug()<<"////// LENGTH: "<<mlt_producer_get_playtime(m_mltProducer->get_producer());
408 return mlt_producer_get_playtime(m_mltProducer->get_producer());
413 bool Render::isValid(KUrl url)
415 char *tmp = decodedString(url.path());
416 Mlt::Producer producer(*m_mltProfile, tmp);
418 if (producer.is_blank())
424 double Render::dar() const
426 return m_mltProfile->dar();
429 void Render::slotSplitView(bool doit)
431 m_isSplitView = doit;
432 Mlt::Service service(m_mltProducer->parent().get_service());
433 Mlt::Tractor tractor(service);
434 if (service.type() != tractor_type || tractor.count() < 2) return;
435 Mlt::Field *field = tractor.field();
438 for (int i = 1; i < tractor.count() && screen < 4; i++) {
439 Mlt::Producer trackProducer(tractor.track(i));
440 kDebug() << "// TRACK: " << i << ", HIDE: " << trackProducer.get("hide");
441 if (QString(trackProducer.get("hide")).toInt() != 1) {
442 kDebug() << "// ADIDNG TRACK: " << i;
443 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
444 transition->set("mlt_service", "composite");
445 transition->set("a_track", 0);
446 transition->set("b_track", i);
447 transition->set("distort", 1);
448 transition->set("internal_added", "200");
455 tmp = "50%,0:50%x50%";
458 tmp = "0,50%:50%x50%";
462 tmp = "50%,50%:50%x50%";
465 transition->set("geometry", tmp);
466 transition->set("always_active", "1");
467 field->plant_transition(*transition, 0, i);
472 m_mltConsumer->set("refresh", 1);
474 mlt_service serv = m_mltProducer->parent().get_service();
475 mlt_service nextservice = mlt_service_get_producer(serv);
476 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
477 QString mlt_type = mlt_properties_get(properties, "mlt_type");
478 QString resource = mlt_properties_get(properties, "mlt_service");
480 while (mlt_type == "transition") {
481 QString added = mlt_properties_get(MLT_SERVICE_PROPERTIES(nextservice), "internal_added");
482 if (added == "200") {
483 mlt_field_disconnect_service(field->get_field(), nextservice);
485 nextservice = mlt_service_producer(nextservice);
486 if (nextservice == NULL) break;
487 properties = MLT_SERVICE_PROPERTIES(nextservice);
488 mlt_type = mlt_properties_get(properties, "mlt_type");
489 resource = mlt_properties_get(properties, "mlt_service");
490 m_mltConsumer->set("refresh", 1);
495 void Render::getFileProperties(const QDomElement &xml, const QString &clipId, bool replaceProducer)
497 KUrl url = KUrl(xml.attribute("resource", QString()));
498 Mlt::Producer *producer = NULL;
499 if (xml.attribute("type").toInt() == TEXT && !QFile::exists(url.path())) {
500 emit replyGetFileProperties(clipId, producer, QMap < QString, QString >(), QMap < QString, QString >(), replaceProducer);
503 if (xml.attribute("type").toInt() == COLOR) {
504 char *tmp = decodedString("colour:" + xml.attribute("colour"));
505 producer = new Mlt::Producer(*m_mltProfile, 0, tmp);
507 } else if (url.isEmpty()) {
509 QDomElement mlt = doc.createElement("mlt");
510 QDomElement play = doc.createElement("playlist");
511 doc.appendChild(mlt);
512 mlt.appendChild(play);
513 play.appendChild(doc.importNode(xml, true));
514 char *tmp = decodedString(doc.toString());
515 producer = new Mlt::Producer(*m_mltProfile, "xml-string", tmp);
518 char *tmp = decodedString(url.path());
519 producer = new Mlt::Producer(*m_mltProfile, tmp);
523 if (producer == NULL || producer->is_blank() || !producer->is_valid()) {
524 kDebug() << " / / / / / / / / ERROR / / / / // CANNOT LOAD PRODUCER: ";
525 emit removeInvalidClip(clipId, replaceProducer);
530 if (xml.hasAttribute("force_aspect_ratio")) {
531 double aspect = xml.attribute("force_aspect_ratio").toDouble();
532 if (aspect > 0) producer->set("force_aspect_ratio", aspect);
534 if (xml.hasAttribute("threads")) {
535 int threads = xml.attribute("threads").toInt();
536 if (threads != 1) producer->set("threads", threads);
538 if (xml.hasAttribute("video_index")) {
539 int vindex = xml.attribute("video_index").toInt();
540 if (vindex != 0) producer->set("video_index", vindex);
542 if (xml.hasAttribute("audio_index")) {
543 int aindex = xml.attribute("audio_index").toInt();
544 if (aindex != 0) producer->set("audio_index", aindex);
547 if (xml.hasAttribute("out")) producer->set_in_and_out(xml.attribute("in").toInt(), xml.attribute("out").toInt());
549 char *tmp = decodedString(clipId);
550 producer->set("id", tmp);
553 if (!replaceProducer && xml.hasAttribute("file_hash")) {
554 // Clip already has all properties
555 emit replyGetFileProperties(clipId, producer, QMap < QString, QString >(), QMap < QString, QString >(), replaceProducer);
560 int width = (int)(height * m_mltProfile->dar());
561 QMap < QString, QString > filePropertyMap;
562 QMap < QString, QString > metadataPropertyMap;
564 int frameNumber = xml.attribute("thumbnail", "0").toInt();
565 if (frameNumber != 0) producer->seek(frameNumber);
567 filePropertyMap["duration"] = QString::number(producer->get_playtime());
568 //kDebug() << "/////// PRODUCER: " << url.path() << " IS: " << producer.get_playtime();
570 Mlt::Frame *frame = producer->get_frame();
572 if (xml.attribute("type").toInt() == SLIDESHOW) {
573 if (xml.hasAttribute("ttl")) producer->set("ttl", xml.attribute("ttl").toInt());
574 if (xml.attribute("fade") == "1") {
575 // user wants a fade effect to slideshow
576 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "luma");
577 if (filter && filter->is_valid()) {
578 if (xml.hasAttribute("ttl")) filter->set("period", xml.attribute("ttl").toInt() - 1);
579 if (xml.hasAttribute("luma_duration") && !xml.attribute("luma_duration").isEmpty()) filter->set("luma.out", xml.attribute("luma_duration").toInt());
580 if (xml.hasAttribute("luma_file") && !xml.attribute("luma_file").isEmpty()) {
581 char *tmp = decodedString(xml.attribute("luma_file"));
582 filter->set("luma.resource", tmp);
584 if (xml.hasAttribute("softness")) {
585 int soft = xml.attribute("softness").toInt();
586 filter->set("luma.softness", (double) soft / 100.0);
589 Mlt::Service clipService(producer->get_service());
590 clipService.attach(*filter);
596 filePropertyMap["fps"] = producer->get("source_fps");
598 if (frame && frame->is_valid()) {
599 filePropertyMap["frame_size"] = QString::number(frame->get_int("width")) + 'x' + QString::number(frame->get_int("height"));
600 filePropertyMap["frequency"] = QString::number(frame->get_int("frequency"));
601 filePropertyMap["channels"] = QString::number(frame->get_int("channels"));
602 filePropertyMap["aspect_ratio"] = frame->get("aspect_ratio");
604 if (frame->get_int("test_image") == 0) {
605 if (url.path().endsWith(".mlt") || url.path().endsWith(".westley") || url.path().endsWith(".kdenlive")) {
606 filePropertyMap["type"] = "playlist";
607 metadataPropertyMap["comment"] = QString::fromUtf8(producer->get("title"));
608 } else if (frame->get_int("test_audio") == 0)
609 filePropertyMap["type"] = "av";
611 filePropertyMap["type"] = "video";
613 mlt_image_format format = mlt_image_yuv422;
615 int frame_height = 0;
616 //frame->set("rescale.interp", "hyper");
617 frame->set("normalised_height", height);
618 frame->set("normalised_width", width);
619 QPixmap pix(width, height);
621 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
622 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
623 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
624 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
626 if (!image.isNull()) {
627 pix = QPixmap::fromImage(image.rgbSwapped());
631 mlt_pool_release(new_image);
632 emit replyGetImage(clipId, pix);
634 } else if (frame->get_int("test_audio") == 0) {
635 QPixmap pixmap = KIcon("audio-x-generic").pixmap(QSize(width, height));
636 emit replyGetImage(clipId, pixmap);
637 filePropertyMap["type"] = "audio";
641 // Retrieve audio / video codec name
645 if (producer->get_int("video_index") > -1) {
646 /*if (context->duration == AV_NOPTS_VALUE) {
647 kDebug() << " / / / / / / / /ERROR / / / CLIP HAS UNKNOWN DURATION";
648 emit removeInvalidClip(clipId);
652 // Get the video_index
653 int default_video = producer->get_int("video_index");
655 int default_audio = producer->get_int("audio_index");
658 // Find maximum stream index values
659 for (int ix = 0; ix < producer->get_int("meta.media.nb_streams"); ix++) {
660 snprintf(property, sizeof(property), "meta.media.%d.stream.type", ix);
661 QString type = producer->get(property);
664 else if (type == "audio")
667 filePropertyMap["default_video"] = QString::number(default_video);
668 filePropertyMap["video_max"] = QString::number(video_max);
669 filePropertyMap["default_audio"] = QString::number(default_audio);
670 filePropertyMap["audio_max"] = QString::number(audio_max);
672 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", default_video);
673 if (producer->get(property)) {
674 filePropertyMap["videocodec"] = producer->get(property);
676 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
677 if (producer->get(property))
678 filePropertyMap["videocodec"] = producer->get(property);
681 if (KdenliveSettings::dropbframes()) {
682 kDebug() << "// LOOKING FOR H264 on: " << default_video;
683 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
684 kDebug() << "PROP: " << property << " = " << producer->get(property);
685 if (producer->get(property) && strcmp(producer->get(property), "h264") == 0) {
686 kDebug() << "// GOT H264 CLIP, SETTING FAST PROPS";
687 producer->set("skip_loop_filter", "all");
688 producer->set("skip_frame", "bidir");
692 } else kDebug() << " / / / / /WARNING, VIDEO CONTEXT IS NULL!!!!!!!!!!!!!!";
693 if (producer->get_int("audio_index") > -1) {
694 // Get the audio_index
695 int index = producer->get_int("audio_index");
697 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", index);
698 if (producer->get(property)) {
699 filePropertyMap["audiocodec"] = producer->get(property);
701 snprintf(property, sizeof(property), "meta.media.%d.codec.name", index);
702 if (producer->get(property))
703 filePropertyMap["audiocodec"] = producer->get(property);
708 Mlt::Properties metadata;
709 metadata.pass_values(*producer, "meta.attr.");
710 int count = metadata.count();
711 for (int i = 0; i < count; i ++) {
712 QString name = metadata.get_name(i);
713 QString value = QString::fromUtf8(metadata.get(i));
714 if (name.endsWith("markup") && !value.isEmpty())
715 metadataPropertyMap[ name.section('.', 0, -2)] = value;
718 emit replyGetFileProperties(clipId, producer, filePropertyMap, metadataPropertyMap, replaceProducer);
719 kDebug() << "REquested fuile info for: " << url.path();
721 // FIXME: should delete this to avoid a leak...
726 /** Create the producer from the MLT XML QDomDocument */
728 void Render::initSceneList()
730 kDebug() << "-------- INIT SCENE LIST ------_";
732 QDomElement mlt = doc.createElement("mlt");
733 doc.appendChild(mlt);
734 QDomElement prod = doc.createElement("producer");
735 prod.setAttribute("resource", "colour");
736 prod.setAttribute("colour", "red");
737 prod.setAttribute("id", "black");
738 prod.setAttribute("in", "0");
739 prod.setAttribute("out", "0");
741 QDomElement tractor = doc.createElement("tractor");
742 QDomElement multitrack = doc.createElement("multitrack");
744 QDomElement playlist1 = doc.createElement("playlist");
745 playlist1.appendChild(prod);
746 multitrack.appendChild(playlist1);
747 QDomElement playlist2 = doc.createElement("playlist");
748 multitrack.appendChild(playlist2);
749 QDomElement playlist3 = doc.createElement("playlist");
750 multitrack.appendChild(playlist3);
751 QDomElement playlist4 = doc.createElement("playlist");
752 multitrack.appendChild(playlist4);
753 QDomElement playlist5 = doc.createElement("playlist");
754 multitrack.appendChild(playlist5);
755 tractor.appendChild(multitrack);
756 mlt.appendChild(tractor);
757 // kDebug()<<doc.toString();
759 QString tmp = QString("<mlt><producer resource=\"colour\" colour=\"red\" id=\"red\" /><tractor><multitrack><playlist></playlist><playlist></playlist><playlist /><playlist /><playlist></playlist></multitrack></tractor></mlt>");*/
760 setSceneList(doc, 0);
766 /** Create the producer from the MLT XML QDomDocument */
767 void Render::setProducer(Mlt::Producer *producer, int position)
769 if (m_winid == -1) return;
772 m_mltConsumer->stop();
775 m_mltConsumer->purge();
779 m_mltProducer->set_speed(0);
780 delete m_mltProducer;
781 m_mltProducer = NULL;
785 m_mltProducer = new Mlt::Producer(producer->get_producer());
786 } else m_mltProducer = m_blackClip->cut(0, 50);
787 /*if (KdenliveSettings::dropbframes()) {
788 m_mltProducer->set("skip_loop_filter", "all");
789 m_mltProducer->set("skip_frame", "bidir");
791 if (!m_mltProducer || !m_mltProducer->is_valid()) kDebug() << " WARNING - - - - -INVALID PLAYLIST: ";
793 m_fps = m_mltProducer->get_fps();
795 if (position != -1) {
796 m_mltProducer->seek(position);
797 emit rendererPosition(position);
804 /** Create the producer from the MLT XML QDomDocument */
805 void Render::setSceneList(QDomDocument list, int position)
807 setSceneList(list.toString(), position);
810 /** Create the producer from the MLT XML QDomDocument */
811 void Render::setSceneList(QString playlist, int position)
813 if (m_winid == -1) return;
815 qDeleteAll(m_slowmotionProducers.values());
816 m_slowmotionProducers.clear();
818 //kWarning() << "////// RENDER, SET SCENE LIST: " << playlist;
820 if (m_mltConsumer == NULL) {
821 kWarning() << "/////// ERROR, TRYING TO USE NULL MLT CONSUMER";
826 if (!m_mltConsumer->is_stopped()) {
827 m_mltConsumer->stop();
828 //m_mltConsumer->set("refresh", 0);
832 m_mltProducer->set_speed(0);
833 //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
835 delete m_mltProducer;
836 m_mltProducer = NULL;
841 char *tmp = decodedString(playlist);
842 m_mltProducer = new Mlt::Producer(*m_mltProfile, "xml-string", tmp);
844 if (!m_mltProducer || !m_mltProducer->is_valid()) {
845 kDebug() << " WARNING - - - - -INVALID PLAYLIST: " << tmp;
846 m_mltProducer = m_blackClip->cut(0, 50);
850 m_mltProducer->optimise();
852 /*if (KdenliveSettings::osdtimecode()) {
853 // Attach filter for on screen display of timecode
855 QString attr = "attr_check";
856 mlt_filter filter = mlt_factory_filter( "data_feed", (char*) attr.ascii() );
857 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
858 mlt_producer_attach( m_mltProducer->get_producer(), filter );
859 mlt_filter_close( filter );
861 m_osdInfo = new Mlt::Filter("data_show");
862 tmp = decodedString(m_osdProfile);
863 m_osdInfo->set("resource", tmp);
865 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
866 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
867 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
868 m_osdInfo->set("dynamic", "1");
870 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
872 m_osdInfo->set("dynamic", "0");
875 m_fps = m_mltProducer->get_fps();
877 // Seek to correct place after opening project.
878 m_mltProducer->seek(position);
881 kDebug() << "// NEW SCENE LIST DURATION SET TO: " << m_mltProducer->get_playtime();
883 fillSlowMotionProducers();
887 emit refreshDocumentProducers();
888 //kDebug()<<"// SETSCN LST, POS: "<<position;
889 //if (position != 0) emit rendererPosition(position);
892 /** Create the producer from the MLT XML QDomDocument */
893 const QString Render::sceneList()
896 Mlt::Consumer xmlConsumer(*m_mltProfile , "xml:kdenlive_playlist");
897 m_mltProducer->optimise();
898 xmlConsumer.set("terminate_on_pause", 1);
899 Mlt::Producer prod(m_mltProducer->get_producer());
900 bool split = m_isSplitView;
901 if (split) slotSplitView(false);
902 xmlConsumer.connect(prod);
904 while (!xmlConsumer.is_stopped()) {}
905 playlist = QString::fromUtf8(xmlConsumer.get("kdenlive_playlist"));
906 if (split) slotSplitView(true);
910 bool Render::saveSceneList(QString path, QDomElement kdenliveData)
914 doc.setContent(sceneList(), false);
915 if (!kdenliveData.isNull()) {
916 // add Kdenlive specific tags
917 QDomNode mlt = doc.elementsByTagName("mlt").at(0);
918 mlt.appendChild(doc.importNode(kdenliveData, true));
920 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
921 kWarning() << "////// ERROR writing to file: " << path;
924 file.write(doc.toString().toUtf8());
925 if (file.error() != QFile::NoError) {
934 void Render::saveZone(KUrl url, QString desc, QPoint zone)
936 kDebug() << "// SAVING CLIP ZONE, RENDER: " << m_name;
937 char *tmppath = decodedString("xml:" + url.path());
938 Mlt::Consumer xmlConsumer(*m_mltProfile , tmppath);
939 m_mltProducer->optimise();
941 xmlConsumer.set("terminate_on_pause", 1);
942 if (m_name == "clip") {
943 Mlt::Producer *prod = m_mltProducer->cut(zone.x(), zone.y());
944 tmppath = decodedString(desc);
946 list.insert_at(0, prod, 0);
947 list.set("title", tmppath);
949 xmlConsumer.connect(list);
952 //TODO: not working yet, save zone from timeline
953 Mlt::Producer *p1 = new Mlt::Producer(m_mltProducer->get_producer());
954 /* Mlt::Service service(p1->parent().get_service());
955 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";*/
957 //Mlt::Producer *prod = p1->cut(zone.x(), zone.y());
958 tmppath = decodedString(desc);
959 //prod->set("title", tmppath);
961 xmlConsumer.connect(*p1); //list);
967 double Render::fps() const
972 void Render::connectPlaylist()
974 if (!m_mltConsumer) return;
975 //m_mltConsumer->set("refresh", "0");
976 m_mltConsumer->connect(*m_mltProducer);
977 m_mltProducer->set_speed(0);
978 m_mltConsumer->start();
979 emit durationChanged(m_mltProducer->get_playtime());
982 if (m_mltConsumer->start() == -1) {
983 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."));
984 delete m_mltConsumer;
985 m_mltConsumer = NULL;
992 void Render::refreshDisplay()
995 if (!m_mltProducer) return;
996 //m_mltConsumer->set("refresh", 0);
998 //mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
999 /*if (KdenliveSettings::osdtimecode()) {
1000 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
1001 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
1002 m_osdInfo->set("dynamic", "1");
1003 m_mltProducer->attach(*m_osdInfo);
1006 m_mltProducer->detach(*m_osdInfo);
1007 m_osdInfo->set("dynamic", "0");
1012 void Render::setVolume(double /*volume*/)
1014 if (!m_mltConsumer || !m_mltProducer) return;
1016 m_mltConsumer->set("refresh", 0);
1017 // Attach filter for on screen display of timecode
1018 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1019 mlt_properties_set_double( properties, "meta.volume", volume );
1020 mlt_properties_set_int( properties, "meta.attr.osdvolume", 1);
1021 mlt_properties_set( properties, "meta.attr.osdvolume.markup", i18n("Volume: ") + QString::number(volume * 100));
1023 if (!KdenliveSettings::osdtimecode()) {
1024 m_mltProducer->detach(*m_osdInfo);
1025 mlt_properties_set_int( properties, "meta.attr.timecode", 0);
1026 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1029 m_osdTimer->setSingleShot(2500);
1032 void Render::slotOsdTimeout()
1034 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1035 mlt_properties_set_int(properties, "meta.attr.osdvolume", 0);
1036 mlt_properties_set(properties, "meta.attr.osdvolume.markup", NULL);
1037 //if (!KdenliveSettings::osdtimecode()) m_mltProducer->detach(*m_osdInfo);
1041 void Render::start()
1043 kDebug() << "----- STARTING MONITOR: " << m_name;
1044 if (m_winid == -1) {
1045 kDebug() << "----- BROKEN MONITOR: " << m_name << ", RESTART";
1049 if (m_mltConsumer && m_mltConsumer->is_stopped()) {
1050 kDebug() << "----- MONITOR: " << m_name << " WAS STOPPED";
1051 if (m_mltConsumer->start() == -1) {
1052 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."));
1053 delete m_mltConsumer;
1054 m_mltConsumer = NULL;
1057 kDebug() << "----- MONITOR: " << m_name << " REFRESH";
1058 m_isBlocked = false;
1062 m_isBlocked = false;
1065 void Render::clear()
1067 kDebug() << " ********* RENDER CLEAR";
1068 if (m_mltConsumer) {
1069 //m_mltConsumer->set("refresh", 0);
1070 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
1073 if (m_mltProducer) {
1074 //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
1075 m_mltProducer->set_speed(0.0);
1076 delete m_mltProducer;
1077 m_mltProducer = NULL;
1084 if (m_mltConsumer && !m_mltConsumer->is_stopped()) {
1085 kDebug() << "///////////// RENDER STOPPED: " << m_name;
1087 //m_mltConsumer->set("refresh", 0);
1088 m_mltConsumer->stop();
1089 // delete m_mltConsumer;
1090 // m_mltConsumer = NULL;
1092 kDebug() << "///////////// RENDER STOP2-------";
1095 if (m_mltProducer) {
1096 if (m_isZoneMode) resetZoneMode();
1097 m_mltProducer->set_speed(0.0);
1098 //m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1099 //kDebug() << m_mltProducer->get_length();
1101 kDebug() << "///////////// RENDER STOP3-------";
1104 void Render::stop(const GenTime & startTime)
1107 kDebug() << "///////////// RENDER STOP-------2";
1108 if (m_mltProducer) {
1109 if (m_isZoneMode) resetZoneMode();
1110 m_mltProducer->set_speed(0.0);
1111 m_mltProducer->seek((int) startTime.frames(m_fps));
1113 m_mltConsumer->purge();
1116 void Render::pause()
1118 if (!m_mltProducer || !m_mltConsumer)
1120 if (m_mltProducer->get_speed() == 0.0) return;
1121 if (m_isZoneMode) resetZoneMode();
1123 m_mltConsumer->set("refresh", 0);
1124 m_mltProducer->set_speed(0.0);
1125 emit rendererPosition(m_framePosition);
1126 m_mltProducer->seek(m_framePosition);
1127 m_mltConsumer->purge();
1130 void Render::switchPlay()
1132 if (!m_mltProducer || !m_mltConsumer)
1134 if (m_isZoneMode) resetZoneMode();
1135 if (m_mltProducer->get_speed() == 0.0) {
1136 m_isBlocked = false;
1137 m_mltProducer->set_speed(1.0);
1138 m_mltConsumer->set("refresh", 1);
1141 m_mltConsumer->set("refresh", 0);
1142 m_mltProducer->set_speed(0.0);
1143 emit rendererPosition(m_framePosition);
1144 m_mltProducer->seek(m_framePosition);
1145 m_mltConsumer->purge();
1146 //kDebug()<<" ********* RENDER PAUSE: "<<m_mltProducer->get_speed();
1147 //m_mltConsumer->set("refresh", 0);
1148 /*mlt_position position = mlt_producer_position( m_mltProducer->get_producer() );
1149 m_mltProducer->set_speed(0);
1150 m_mltProducer->seek( position );
1151 //m_mltProducer->seek((int) m_framePosition);
1152 m_isBlocked = false;*/
1154 /*if (speed == 0.0) {
1155 m_mltProducer->seek((int) m_framePosition + 1);
1156 m_mltConsumer->purge();
1161 void Render::play(double speed)
1165 // if (speed == 0.0) m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1166 m_isBlocked = false;
1167 m_mltProducer->set_speed(speed);
1168 /*if (speed == 0.0) {
1169 m_mltProducer->seek((int) m_framePosition + 1);
1170 m_mltConsumer->purge();
1175 void Render::play(const GenTime & startTime)
1177 if (!m_mltProducer || !m_mltConsumer)
1179 m_isBlocked = false;
1180 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1181 m_mltProducer->set_speed(1.0);
1182 m_mltConsumer->set("refresh", 1);
1185 void Render::loopZone(const GenTime & startTime, const GenTime & stopTime)
1187 if (!m_mltProducer || !m_mltConsumer)
1189 //m_mltProducer->set("eof", "loop");
1190 m_isLoopMode = true;
1191 m_loopStart = startTime;
1192 playZone(startTime, stopTime);
1195 void Render::playZone(const GenTime & startTime, const GenTime & stopTime)
1197 if (!m_mltProducer || !m_mltConsumer)
1199 m_isBlocked = false;
1200 if (!m_isZoneMode) m_originalOut = m_mltProducer->get_playtime() - 1;
1201 m_mltProducer->set("out", stopTime.frames(m_fps));
1202 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1203 m_mltProducer->set_speed(1.0);
1204 m_mltConsumer->set("refresh", 1);
1205 m_isZoneMode = true;
1208 void Render::resetZoneMode()
1210 if (!m_isZoneMode && !m_isLoopMode) return;
1211 m_mltProducer->set("out", m_originalOut);
1212 //m_mltProducer->set("eof", "pause");
1213 m_isZoneMode = false;
1214 m_isLoopMode = false;
1217 void Render::seekToFrame(int pos)
1219 //kDebug()<<" ********* RENDER SEEK TO POS";
1222 m_isBlocked = false;
1224 m_mltProducer->seek(pos);
1228 void Render::askForRefresh()
1230 // Use a Timer so that we don't refresh too much
1231 m_refreshTimer->start(300);
1234 void Render::doRefresh()
1236 // Use a Timer so that we don't refresh too much
1237 if (!m_isBlocked && m_mltConsumer) m_mltConsumer->set("refresh", 1);
1240 void Render::refresh()
1242 if (!m_mltProducer || m_isBlocked)
1244 m_refreshTimer->stop();
1245 if (m_mltConsumer) {
1246 m_mltConsumer->set("refresh", 1);
1250 void Render::setDropFrames(bool show)
1252 if (m_mltConsumer) {
1254 if (show == false) dropFrames = 0;
1255 m_mltConsumer->stop();
1256 m_mltConsumer->set("play.real_time", dropFrames);
1257 m_mltConsumer->start();
1261 double Render::playSpeed()
1263 if (m_mltProducer) return m_mltProducer->get_speed();
1267 GenTime Render::seekPosition() const
1269 if (m_mltProducer) return GenTime((int) m_mltProducer->position(), m_fps);
1270 else return GenTime();
1274 const QString & Render::rendererName() const
1280 void Render::emitFrameNumber(double position)
1282 m_framePosition = position;
1283 emit rendererPosition((int) position);
1284 //if (qApp->activeWindow()) QApplication::postEvent(qApp->activeWindow(), new PositionChangeEvent( GenTime((int) position, m_fps), m_monitorId));
1287 void Render::emitConsumerStopped()
1289 // This is used to know when the playing stopped
1290 if (m_mltProducer) {
1291 double pos = m_mltProducer->position();
1292 if (m_isLoopMode) play(m_loopStart);
1293 else if (m_isZoneMode) resetZoneMode();
1294 emit rendererStopped((int) pos);
1295 //if (qApp->activeWindow()) QApplication::postEvent(qApp->activeWindow(), new PositionChangeEvent(GenTime((int) pos, m_fps), m_monitorId + 100));
1296 //new QCustomEvent(10002));
1302 void Render::exportFileToFirewire(QString /*srcFileName*/, int /*port*/, GenTime /*startTime*/, GenTime /*endTime*/)
1304 KMessageBox::sorry(0, i18n("Firewire is not enabled on your system.\n Please install Libiec61883 and recompile Kdenlive"));
1308 void Render::exportCurrentFrame(KUrl url, bool /*notify*/)
1310 if (!m_mltProducer) {
1311 KMessageBox::sorry(qApp->activeWindow(), i18n("There is no clip, cannot extract frame."));
1315 //int height = 1080;//KdenliveSettings::defaultheight();
1316 //int width = 1940; //KdenliveSettings::displaywidth();
1318 QPixmap pix; // = KThumb::getFrame(m_mltProducer, -1, width, height);
1320 QPixmap pix(width, height);
1321 Mlt::Filter m_convert(*m_mltProfile, "avcolour_space");
1322 m_convert.set("forced", mlt_image_rgb24a);
1323 m_mltProducer->attach(m_convert);
1324 Mlt::Frame * frame = m_mltProducer->get_frame();
1325 m_mltProducer->detach(m_convert);
1327 pix = frameThumbnail(frame, width, height);
1330 pix.save(url.path(), "PNG");
1331 //if (notify) QApplication::postEvent(qApp->activeWindow(), new UrlEvent(url, 10003));
1334 /** MLT PLAYLIST DIRECT MANIPULATON **/
1337 void Render::mltCheckLength()
1339 //kDebug()<<"checking track length: "<<track<<"..........";
1341 Mlt::Service service(m_mltProducer->get_service());
1342 Mlt::Tractor tractor(service);
1344 int trackNb = tractor.count();
1348 Mlt::Producer trackProducer(tractor.track(0));
1349 duration = trackProducer.get_playtime() - 1;
1350 m_mltProducer->set("out", duration);
1351 emit durationChanged(duration);
1354 while (trackNb > 1) {
1355 Mlt::Producer trackProducer(tractor.track(trackNb - 1));
1356 trackDuration = trackProducer.get_playtime() - 1;
1358 //kDebug() << " / / /DURATON FOR TRACK " << trackNb - 1 << " = " << trackDuration;
1359 if (trackDuration > duration) duration = trackDuration;
1363 Mlt::Producer blackTrackProducer(tractor.track(0));
1365 if (blackTrackProducer.get_playtime() - 1 != duration) {
1366 Mlt::Playlist blackTrackPlaylist((mlt_playlist) blackTrackProducer.get_service());
1367 Mlt::Producer *blackclip = blackTrackPlaylist.get_clip(0);
1368 if (duration > m_blackClip->get_length()) {
1369 m_blackClip->set("length", duration);
1370 if (blackclip) blackclip->set("length", duration);
1372 if (blackclip == NULL || blackclip->is_blank() || blackTrackPlaylist.count() != 1) {
1373 blackTrackPlaylist.clear();
1374 blackTrackPlaylist.append(*m_blackClip, 0, duration - 1);
1375 } else blackTrackPlaylist.resize_clip(0, 0, duration - 1);
1377 m_mltProducer->set("out", duration);
1378 emit durationChanged(duration);
1382 void Render::mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod)
1384 if (m_mltProducer == NULL) {
1385 kDebug() << "PLAYLIST NOT INITIALISED //////";
1388 Mlt::Producer parentProd(m_mltProducer->parent());
1389 if (parentProd.get_producer() == NULL) {
1390 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1394 Mlt::Service service(parentProd.get_service());
1395 Mlt::Tractor tractor(service);
1396 if (info.track > tractor.count() - 1) {
1397 kDebug() << "ERROR TRYING TO INSERT CLIP ON TRACK " << info.track << ", at POS: " << info.startPos.frames(25);
1400 mlt_service_lock(service.get_service());
1401 Mlt::Producer trackProducer(tractor.track(info.track));
1402 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1403 //kDebug()<<"/// INSERT cLIP: "<<info.cropStart.frames(m_fps)<<", "<<info.startPos.frames(m_fps)<<"-"<<info.endPos.frames(m_fps);
1405 if (element.attribute("speed", "1.0").toDouble() != 1.0) {
1406 // We want a slowmotion producer
1407 double speed = element.attribute("speed", "1.0").toDouble();
1408 QString url = QString::fromUtf8(prod->get("resource"));
1409 url.append('?' + QString::number(speed));
1410 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1411 if (!slowprod || slowprod->get_producer() == NULL) {
1412 char *tmp = decodedString(url);
1413 slowprod = new Mlt::Producer(*m_mltProfile, "framebuffer", tmp);
1415 QString id = prod->get("id");
1416 if (id.contains('_')) id = id.section('_', 0, 0);
1417 QString producerid = "slowmotion:" + id + ':' + QString::number(speed);
1418 tmp = decodedString(producerid);
1419 slowprod->set("id", tmp);
1421 m_slowmotionProducers.insert(url, slowprod);
1426 Mlt::Producer *clip = prod->cut((int) info.cropStart.frames(m_fps), (int)(info.endPos - info.startPos + info.cropStart).frames(m_fps) - 1);
1427 int newIndex = trackPlaylist.insert_at((int) info.startPos.frames(m_fps), *clip, 1);
1429 /*if (QString(prod->get("transparency")).toInt() == 1)
1430 mltAddClipTransparency(info, info.track - 1, QString(prod->get("id")).toInt());*/
1432 mlt_service_unlock(service.get_service());
1434 if (info.track != 0 && (newIndex + 1 == trackPlaylist.count())) mltCheckLength();
1435 //tractor.multitrack()->refresh();
1436 //tractor.refresh();
1440 void Render::mltCutClip(int track, GenTime position)
1445 Mlt::Service service(m_mltProducer->parent().get_service());
1446 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
1448 Mlt::Tractor tractor(service);
1449 Mlt::Producer trackProducer(tractor.track(track));
1450 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1453 /* // Display playlist info
1454 kDebug()<<"//////////// BEFORE";
1455 for (int i = 0; i < trackPlaylist.count(); i++) {
1456 int blankStart = trackPlaylist.clip_start(i);
1457 int blankDuration = trackPlaylist.clip_length(i) - 1;
1459 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1460 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1463 int cutPos = (int) position.frames(m_fps);
1465 int clipIndex = trackPlaylist.get_clip_index_at(cutPos);
1466 if (trackPlaylist.is_blank(clipIndex)) {
1467 kDebug() << "// WARNING, TRYING TO CUT A BLANK";
1468 m_isBlocked = false;
1471 mlt_service_lock(service.get_service());
1472 int clipStart = trackPlaylist.clip_start(clipIndex);
1473 trackPlaylist.split(clipIndex, cutPos - clipStart - 1);
1474 mlt_service_unlock(service.get_service());
1476 // duplicate effects
1477 Mlt::Producer *original = trackPlaylist.get_clip_at(clipStart);
1478 Mlt::Producer *clip = trackPlaylist.get_clip_at(cutPos);
1480 if (original == NULL || clip == NULL) {
1481 kDebug() << "// ERROR GRABBING CLIP AFTER SPLIT";
1483 Mlt::Service clipService(original->get_service());
1484 Mlt::Service dupService(clip->get_service());
1486 Mlt::Filter *filter = clipService.filter(ct);
1488 // Only duplicate Kdenlive filters, and skip the fade in effects
1489 if (filter->is_valid() && strcmp(filter->get("kdenlive_id"), "") && strcmp(filter->get("kdenlive_id"), "fadein") && strcmp(filter->get("kdenlive_id"), "fade_from_black")) {
1490 // looks like there is no easy way to duplicate a filter,
1491 // so we will create a new one and duplicate its properties
1492 Mlt::Filter *dup = new Mlt::Filter(*m_mltProfile, filter->get("mlt_service"));
1493 if (dup && dup->is_valid()) {
1494 Mlt::Properties entries(filter->get_properties());
1495 for (int i = 0; i < entries.count(); i++) {
1496 dup->set(entries.get_name(i), entries.get(i));
1498 dupService.attach(*dup);
1502 filter = clipService.filter(ct);
1505 /* // Display playlist info
1506 kDebug()<<"//////////// AFTER";
1507 for (int i = 0; i < trackPlaylist.count(); i++) {
1508 int blankStart = trackPlaylist.clip_start(i);
1509 int blankDuration = trackPlaylist.clip_length(i) - 1;
1511 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1512 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1515 m_isBlocked = false;
1518 void Render::mltUpdateClip(ItemInfo info, QDomElement element, Mlt::Producer *prod)
1521 mltRemoveClip(info.track, info.startPos);
1522 mltInsertClip(info, element, prod);
1526 bool Render::mltRemoveClip(int track, GenTime position)
1528 Mlt::Service service(m_mltProducer->parent().get_service());
1529 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
1531 Mlt::Tractor tractor(service);
1532 mlt_service_lock(service.get_service());
1533 Mlt::Producer trackProducer(tractor.track(track));
1534 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1535 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
1537 /* // Display playlist info
1538 kDebug()<<"//// BEFORE";
1539 for (int i = 0; i < trackPlaylist.count(); i++) {
1540 int blankStart = trackPlaylist.clip_start(i);
1541 int blankDuration = trackPlaylist.clip_length(i) - 1;
1543 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1544 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1546 if (trackPlaylist.is_blank(clipIndex)) {
1547 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << position.frames(25);
1551 trackPlaylist.replace_with_blank(clipIndex);
1552 trackPlaylist.consolidate_blanks(0);
1553 /*if (QString(clip.parent().get("transparency")).toInt() == 1)
1554 mltDeleteTransparency((int) position.frames(m_fps), track, QString(clip.parent().get("id")).toInt());*/
1556 /* // Display playlist info
1557 kDebug()<<"//// AFTER";
1558 for (int i = 0; i < trackPlaylist.count(); i++) {
1559 int blankStart = trackPlaylist.clip_start(i);
1560 int blankDuration = trackPlaylist.clip_length(i) - 1;
1562 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1563 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1565 mlt_service_unlock(service.get_service());
1566 if (track != 0 && trackPlaylist.count() <= clipIndex) mltCheckLength();
1567 m_isBlocked = false;
1571 int Render::mltGetSpaceLength(const GenTime pos, int track, bool fromBlankStart)
1573 if (!m_mltProducer) {
1574 kDebug() << "PLAYLIST NOT INITIALISED //////";
1577 Mlt::Producer parentProd(m_mltProducer->parent());
1578 if (parentProd.get_producer() == NULL) {
1579 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1583 Mlt::Service service(parentProd.get_service());
1584 Mlt::Tractor tractor(service);
1585 int insertPos = pos.frames(m_fps);
1587 Mlt::Producer trackProducer(tractor.track(track));
1588 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1589 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1590 if (!trackPlaylist.is_blank(clipIndex)) return -1;
1591 if (fromBlankStart) return trackPlaylist.clip_length(clipIndex);
1592 return trackPlaylist.clip_length(clipIndex) + trackPlaylist.clip_start(clipIndex) - insertPos;
1595 int Render::mltTrackDuration(int track)
1597 if (!m_mltProducer) {
1598 kDebug() << "PLAYLIST NOT INITIALISED //////";
1601 Mlt::Producer parentProd(m_mltProducer->parent());
1602 if (parentProd.get_producer() == NULL) {
1603 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1607 Mlt::Service service(parentProd.get_service());
1608 Mlt::Tractor tractor(service);
1610 Mlt::Producer trackProducer(tractor.track(track));
1611 return trackProducer.get_playtime() - 1;
1614 void Render::mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime duration, const GenTime timeOffset)
1616 if (!m_mltProducer) {
1617 kDebug() << "PLAYLIST NOT INITIALISED //////";
1620 Mlt::Producer parentProd(m_mltProducer->parent());
1621 if (parentProd.get_producer() == NULL) {
1622 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1625 //kDebug()<<"// CLP STRT LST: "<<trackClipStartList;
1626 //kDebug()<<"// TRA STRT LST: "<<trackTransitionStartList;
1628 Mlt::Service service(parentProd.get_service());
1629 Mlt::Tractor tractor(service);
1630 mlt_service_lock(service.get_service());
1631 int diff = duration.frames(m_fps);
1632 int offset = timeOffset.frames(m_fps);
1636 // insert space in one track only
1637 Mlt::Producer trackProducer(tractor.track(track));
1638 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1639 insertPos = trackClipStartList.value(track);
1640 if (insertPos != -1) {
1641 insertPos += offset;
1642 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1643 if (diff > 0) trackPlaylist.insert_blank(clipIndex, diff - 1);
1645 if (!trackPlaylist.is_blank(clipIndex)) clipIndex --;
1646 if (!trackPlaylist.is_blank(clipIndex)) kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
1647 int position = trackPlaylist.clip_start(clipIndex);
1648 trackPlaylist.remove_region(position, - diff - 1);
1650 trackPlaylist.consolidate_blanks(0);
1652 // now move transitions
1653 mlt_service serv = m_mltProducer->parent().get_service();
1654 mlt_service nextservice = mlt_service_get_producer(serv);
1655 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
1656 QString mlt_type = mlt_properties_get(properties, "mlt_type");
1657 QString resource = mlt_properties_get(properties, "mlt_service");
1659 while (mlt_type == "transition") {
1660 mlt_transition tr = (mlt_transition) nextservice;
1661 int currentTrack = mlt_transition_get_b_track(tr);
1662 int currentIn = (int) mlt_transition_get_in(tr);
1663 int currentOut = (int) mlt_transition_get_out(tr);
1664 insertPos = trackTransitionStartList.value(track);
1665 if (insertPos != -1) {
1666 insertPos += offset;
1667 if (track == currentTrack && currentOut > insertPos && resource != "mix") {
1668 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
1671 nextservice = mlt_service_producer(nextservice);
1672 if (nextservice == NULL) break;
1673 properties = MLT_SERVICE_PROPERTIES(nextservice);
1674 mlt_type = mlt_properties_get(properties, "mlt_type");
1675 resource = mlt_properties_get(properties, "mlt_service");
1678 int trackNb = tractor.count();
1679 while (trackNb > 1) {
1680 Mlt::Producer trackProducer(tractor.track(trackNb - 1));
1681 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1684 //int clipNb = trackPlaylist.count();
1685 insertPos = trackClipStartList.value(trackNb - 1);
1686 if (insertPos != -1) {
1687 insertPos += offset;
1689 /* kDebug()<<"-------------\nTRACK "<<trackNb<<" HAS "<<clipNb<<" CLPIS";
1690 kDebug() << "INSERT SPACE AT: "<<insertPos<<", DIFF: "<<diff<<", TK: "<<trackNb;
1691 for (int i = 0; i < clipNb; i++) {
1692 kDebug()<<"CLIP "<<i<<", START: "<<trackPlaylist.clip_start(i)<<", END: "<<trackPlaylist.clip_start(i) + trackPlaylist.clip_length(i);
1693 if (trackPlaylist.is_blank(i)) kDebug()<<"++ BLANK ++ ";
1694 kDebug()<<"-------------";
1696 kDebug()<<"END-------------";*/
1699 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1700 if (diff > 0) trackPlaylist.insert_blank(clipIndex, diff - 1);
1702 if (!trackPlaylist.is_blank(clipIndex)) clipIndex --;
1703 if (!trackPlaylist.is_blank(clipIndex)) kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
1704 int position = trackPlaylist.clip_start(clipIndex);
1705 trackPlaylist.remove_region(position, - diff - 1);
1707 trackPlaylist.consolidate_blanks(0);
1711 // now move transitions
1712 mlt_service serv = m_mltProducer->parent().get_service();
1713 mlt_service nextservice = mlt_service_get_producer(serv);
1714 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
1715 QString mlt_type = mlt_properties_get(properties, "mlt_type");
1716 QString resource = mlt_properties_get(properties, "mlt_service");
1718 while (mlt_type == "transition") {
1719 mlt_transition tr = (mlt_transition) nextservice;
1720 int currentIn = (int) mlt_transition_get_in(tr);
1721 int currentOut = (int) mlt_transition_get_out(tr);
1722 int currentTrack = mlt_transition_get_b_track(tr);
1723 insertPos = trackTransitionStartList.value(currentTrack);
1724 if (insertPos != -1) {
1725 insertPos += offset;
1726 if (currentOut > insertPos && resource != "mix") {
1727 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
1730 nextservice = mlt_service_producer(nextservice);
1731 if (nextservice == NULL) break;
1732 properties = MLT_SERVICE_PROPERTIES(nextservice);
1733 mlt_type = mlt_properties_get(properties, "mlt_type");
1734 resource = mlt_properties_get(properties, "mlt_service");
1737 mlt_service_unlock(service.get_service());
1739 m_mltConsumer->set("refresh", 1);
1743 void Render::mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest)
1745 Mlt::Service sourceService(source->get_service());
1746 Mlt::Service destService(dest->get_service());
1748 // move all effects to the correct producer
1750 Mlt::Filter *filter = sourceService.filter(ct);
1752 if (filter->get("kdenlive_ix") != 0) {
1753 sourceService.detach(*filter);
1754 destService.attach(*filter);
1756 filter = sourceService.filter(ct);
1760 int Render::mltChangeClipSpeed(ItemInfo info, double speed, double oldspeed, Mlt::Producer *prod)
1764 Mlt::Service service(m_mltProducer->parent().get_service());
1765 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
1766 //kDebug() << "Changing clip speed, set in and out: " << info.cropStart.frames(m_fps) << " to " << (info.endPos - info.startPos).frames(m_fps) - 1;
1767 Mlt::Tractor tractor(service);
1768 Mlt::Producer trackProducer(tractor.track(info.track));
1769 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1770 int startPos = info.startPos.frames(m_fps);
1771 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
1772 int clipLength = trackPlaylist.clip_length(clipIndex);
1774 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
1778 if (!clip->is_valid() || clip->is_blank()) {
1783 Mlt::Producer clipparent = clip->parent();
1784 if (!clipparent.is_valid() || clipparent.is_blank()) {
1790 QString serv = clipparent.get("mlt_service");
1791 QString id = clipparent.get("id");
1792 //kDebug() << "CLIP SERVICE: " << serv;
1793 if (serv == "avformat" && speed != 1.0) {
1794 mlt_service_lock(service.get_service());
1795 QString url = QString::fromUtf8(clipparent.get("resource"));
1796 url.append('?' + QString::number(speed));
1797 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1798 if (!slowprod || slowprod->get_producer() == NULL) {
1799 char *tmp = decodedString(url);
1800 slowprod = new Mlt::Producer(*m_mltProfile, "framebuffer", tmp);
1802 QString producerid = "slowmotion:" + id + ':' + QString::number(speed);
1803 tmp = decodedString(producerid);
1804 slowprod->set("id", tmp);
1806 m_slowmotionProducers.insert(url, slowprod);
1808 trackPlaylist.replace_with_blank(clipIndex);
1809 trackPlaylist.consolidate_blanks(0);
1810 // Check that the blank space is long enough for our new duration
1811 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1812 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
1814 if (clipIndex + 1 < trackPlaylist.count() && (startPos + clipLength / speed > blankEnd)) {
1815 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
1816 cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)(info.cropStart.frames(m_fps) / speed + maxLength.frames(m_fps) - 1));
1817 } else cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)((info.cropStart.frames(m_fps) + clipLength) / speed - 1));
1819 // move all effects to the correct producer
1820 mltPasteEffects(clip, cut);
1822 trackPlaylist.insert_at(startPos, *cut, 1);
1823 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1824 newLength = trackPlaylist.clip_length(clipIndex);
1825 mlt_service_unlock(service.get_service());
1826 } else if (speed == 1.0) {
1827 mlt_service_lock(service.get_service());
1829 trackPlaylist.replace_with_blank(clipIndex);
1830 trackPlaylist.consolidate_blanks(0);
1832 // Check that the blank space is long enough for our new duration
1833 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1834 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
1837 GenTime oldDuration = GenTime(clipLength, m_fps);
1838 GenTime newDuration = oldDuration * oldspeed;
1839 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + newDuration).frames(m_fps) > blankEnd) {
1840 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
1841 cut = prod->cut((int)(info.cropStart.frames(m_fps)), (int)(info.cropStart.frames(m_fps) + maxLength.frames(m_fps) - 1));
1842 } else cut = prod->cut((int)(info.cropStart.frames(m_fps)), (int)((info.cropStart + newDuration).frames(m_fps)) - 1);
1844 // move all effects to the correct producer
1845 mltPasteEffects(clip, cut);
1847 trackPlaylist.insert_at(startPos, *cut, 1);
1848 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1849 newLength = trackPlaylist.clip_length(clipIndex);
1850 mlt_service_unlock(service.get_service());
1852 } else if (serv == "framebuffer") {
1853 mlt_service_lock(service.get_service());
1854 QString url = QString::fromUtf8(clipparent.get("resource"));
1855 url = url.section('?', 0, 0);
1856 url.append('?' + QString::number(speed));
1857 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1858 if (!slowprod || slowprod->get_producer() == NULL) {
1859 char *tmp = decodedString(url);
1860 slowprod = new Mlt::Producer(*m_mltProfile, "framebuffer", tmp);
1862 QString producerid = "slowmotion:" + id.section(':', 1, 1) + ':' + QString::number(speed);
1863 tmp = decodedString(producerid);
1864 slowprod->set("id", tmp);
1866 m_slowmotionProducers.insert(url, slowprod);
1868 trackPlaylist.replace_with_blank(clipIndex);
1869 trackPlaylist.consolidate_blanks(0);
1871 GenTime oldDuration = GenTime(clipLength, m_fps);
1872 GenTime newDuration = oldDuration * oldspeed / speed;
1874 // Check that the blank space is long enough for our new duration
1875 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1876 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
1879 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + newDuration).frames(m_fps) > blankEnd) {
1880 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
1881 cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)(info.cropStart.frames(m_fps) / speed + maxLength.frames(m_fps) - 1));
1882 } else cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)((info.cropStart / speed + newDuration).frames(m_fps) - 1));
1884 // move all effects to the correct producer
1885 mltPasteEffects(clip, cut);
1887 trackPlaylist.insert_at(startPos, *cut, 1);
1888 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1889 newLength = trackPlaylist.clip_length(clipIndex);
1891 mlt_service_unlock(service.get_service());
1895 if (clipIndex + 1 == trackPlaylist.count()) mltCheckLength();
1896 m_isBlocked = false;
1900 bool Render::mltRemoveEffect(int track, GenTime position, QString index, bool updateIndex, bool doRefresh)
1902 kDebug() << "// TRYing to remove effect at: " << index;
1903 Mlt::Service service(m_mltProducer->parent().get_service());
1904 bool success = false;
1905 Mlt::Tractor tractor(service);
1906 Mlt::Producer trackProducer(tractor.track(track));
1907 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1908 //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
1909 Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
1911 kDebug() << " / / / CANNOT FIND CLIP TO REMOVE EFFECT";
1914 Mlt::Service clipService(clip->get_service());
1915 // if (tag.startsWith("ladspa")) tag = "ladspa";
1918 Mlt::Filter *filter = clipService.filter(ct);
1920 if ((index == "-1" && strcmp(filter->get("kdenlive_id"), "")) || filter->get("kdenlive_ix") == index) {// && filter->get("kdenlive_id") == id) {
1921 if (clipService.detach(*filter) == 0) success = true;
1922 kDebug() << " / / / DLEETED EFFECT: " << ct;
1923 } else if (updateIndex) {
1924 // Adjust the other effects index
1925 if (QString(filter->get("kdenlive_ix")).toInt() > index.toInt()) filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() - 1);
1928 filter = clipService.filter(ct);
1930 m_isBlocked = false;
1931 if (doRefresh) refresh();
1936 bool Render::mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh)
1939 Mlt::Service service(m_mltProducer->parent().get_service());
1941 Mlt::Tractor tractor(service);
1942 Mlt::Producer trackProducer(tractor.track(track));
1943 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1945 Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
1949 Mlt::Service clipService(clip->get_service());
1952 // temporarily remove all effects after insert point
1953 QList <Mlt::Filter *> filtersList;
1954 const int filter_ix = params.paramValue("kdenlive_ix").toInt();
1956 Mlt::Filter *filter = clipService.filter(ct);
1958 if (QString(filter->get("kdenlive_ix")).toInt() > filter_ix) {
1959 filtersList.append(filter);
1960 clipService.detach(*filter);
1962 filter = clipService.filter(ct);
1966 QString tag = params.paramValue("tag");
1967 kDebug() << " / / INSERTING EFFECT: " << tag;
1968 if (tag.startsWith("ladspa")) tag = "ladspa";
1969 char *filterTag = decodedString(tag);
1970 char *filterId = decodedString(params.paramValue("id"));
1971 QHash<QString, QString>::Iterator it;
1972 QString kfr = params.paramValue("keyframes");
1974 if (!kfr.isEmpty()) {
1975 QStringList keyFrames = kfr.split(';', QString::SkipEmptyParts);
1976 kDebug() << "// ADDING KEYFRAME EFFECT: " << params.paramValue("keyframes");
1977 char *starttag = decodedString(params.paramValue("starttag", "start"));
1978 char *endtag = decodedString(params.paramValue("endtag", "end"));
1979 kDebug() << "// ADDING KEYFRAME TAGS: " << starttag << ", " << endtag;
1980 int duration = clip->get_playtime();
1981 //double max = params.paramValue("max").toDouble();
1982 double min = params.paramValue("min").toDouble();
1983 double factor = params.paramValue("factor", "1").toDouble();
1984 params.removeParam("starttag");
1985 params.removeParam("endtag");
1986 params.removeParam("keyframes");
1987 params.removeParam("min");
1988 params.removeParam("max");
1989 params.removeParam("factor");
1991 for (int i = 0; i < keyFrames.size() - 1; ++i) {
1992 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
1993 if (filter && filter->is_valid()) {
1994 filter->set("kdenlive_id", filterId);
1995 int x1 = keyFrames.at(i).section(':', 0, 0).toInt() + offset;
1996 double y1 = keyFrames.at(i).section(':', 1, 1).toDouble();
1997 int x2 = keyFrames.at(i + 1).section(':', 0, 0).toInt();
1998 double y2 = keyFrames.at(i + 1).section(':', 1, 1).toDouble();
1999 if (x2 == -1) x2 = duration;
2001 for (int j = 0; j < params.count(); j++) {
2002 char *name = decodedString(params.at(j).name());
2003 char *value = decodedString(params.at(j).value());
2004 filter->set(name, value);
2009 filter->set("in", x1);
2010 filter->set("out", x2);
2011 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2012 filter->set(starttag, QString::number((min + y1) / factor).toUtf8().data());
2013 filter->set(endtag, QString::number((min + y2) / factor).toUtf8().data());
2014 clipService.attach(*filter);
2021 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2022 if (filter && filter->is_valid())
2023 filter->set("kdenlive_id", filterId);
2025 kDebug() << "filter is NULL";
2026 m_isBlocked = false;
2030 params.removeParam("kdenlive_id");
2032 for (int j = 0; j < params.count(); j++) {
2033 char *name = decodedString(params.at(j).name());
2034 char *value = decodedString(params.at(j).value());
2035 filter->set(name, value);
2041 QString effectArgs = params.paramValue("id").section('_', 1);
2043 params.removeParam("id");
2044 params.removeParam("kdenlive_ix");
2045 params.removeParam("tag");
2046 params.removeParam("disabled");
2048 for (int j = 0; j < params.count(); j++) {
2049 effectArgs.append(' ' + params.at(j).value());
2051 //kDebug() << "SOX EFFECTS: " << effectArgs.simplified();
2052 char *value = decodedString(effectArgs.simplified());
2053 filter->set("effect", value);
2058 // attach filter to the clip
2059 clipService.attach(*filter);
2064 // re-add following filters
2065 for (int i = 0; i < filtersList.count(); i++) {
2066 clipService.attach(*(filtersList.at(i)));
2069 m_isBlocked = false;
2070 if (doRefresh) refresh();
2074 bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList params)
2076 QString index = params.paramValue("kdenlive_ix");
2077 QString tag = params.paramValue("tag");
2079 if (!params.paramValue("keyframes").isEmpty() || /*it.key().startsWith("#") || */tag.startsWith("ladspa") || tag == "sox" || tag == "autotrack_rectangle") {
2080 // This is a keyframe effect, to edit it, we remove it and re-add it.
2081 mltRemoveEffect(track, position, index, true);
2082 bool success = mltAddEffect(track, position, params);
2087 Mlt::Service service(m_mltProducer->parent().get_service());
2089 Mlt::Tractor tractor(service);
2090 Mlt::Producer trackProducer(tractor.track(track));
2091 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2092 //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
2093 Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
2095 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2098 Mlt::Service clipService(clip->get_service());
2101 Mlt::Filter *filter = clipService.filter(ct);
2103 if (filter->get("kdenlive_ix") == index) {
2107 filter = clipService.filter(ct);
2111 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT!!!!!";
2112 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2114 filter = clipService.filter(ct);
2115 QList <Mlt::Filter *> filtersList;
2117 if (QString(filter->get("kdenlive_ix")).toInt() > index.toInt()) {
2118 filtersList.append(filter);
2119 clipService.detach(*filter);
2121 filter = clipService.filter(ct);
2123 bool success = mltAddEffect(track, position, params);
2125 for (int i = 0; i < filtersList.count(); i++) {
2126 clipService.attach(*(filtersList.at(i)));
2129 m_isBlocked = false;
2133 for (int j = 0; j < params.count(); j++) {
2134 char *name = decodedString(params.at(j).name());
2135 char *value = decodedString(params.at(j).value());
2136 filter->set(name, value);
2141 m_isBlocked = false;
2146 void Render::mltMoveEffect(int track, GenTime position, int oldPos, int newPos)
2149 kDebug() << "MOVING EFFECT FROM " << oldPos << ", TO: " << newPos;
2150 Mlt::Service service(m_mltProducer->parent().get_service());
2152 Mlt::Tractor tractor(service);
2153 Mlt::Producer trackProducer(tractor.track(track));
2154 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2155 //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
2156 Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
2158 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2161 Mlt::Service clipService(clip->get_service());
2164 QList <Mlt::Filter *> filtersList;
2165 Mlt::Filter *filter = clipService.filter(ct);
2167 if (newPos > oldPos) {
2169 if (!found && QString(filter->get("kdenlive_ix")).toInt() == oldPos) {
2170 filter->set("kdenlive_ix", newPos);
2171 filtersList.append(filter);
2172 clipService.detach(*filter);
2173 filter = clipService.filter(ct);
2174 while (filter && QString(filter->get("kdenlive_ix")).toInt() <= newPos) {
2175 filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() - 1);
2177 filter = clipService.filter(ct);
2181 if (filter && QString(filter->get("kdenlive_ix")).toInt() > newPos) {
2182 filtersList.append(filter);
2183 clipService.detach(*filter);
2185 filter = clipService.filter(ct);
2189 if (QString(filter->get("kdenlive_ix")).toInt() == oldPos) {
2190 filter->set("kdenlive_ix", newPos);
2191 filtersList.append(filter);
2192 clipService.detach(*filter);
2194 filter = clipService.filter(ct);
2198 filter = clipService.filter(ct);
2200 int pos = QString(filter->get("kdenlive_ix")).toInt();
2201 if (pos >= newPos) {
2202 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
2203 filtersList.append(filter);
2204 clipService.detach(*filter);
2206 filter = clipService.filter(ct);
2210 for (int i = 0; i < filtersList.count(); i++) {
2211 clipService.attach(*(filtersList.at(i)));
2214 m_isBlocked = false;
2218 bool Render::mltResizeClipEnd(ItemInfo info, GenTime clipDuration)
2221 Mlt::Service service(m_mltProducer->parent().get_service());
2222 Mlt::Tractor tractor(service);
2223 Mlt::Producer trackProducer(tractor.track(info.track));
2224 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2226 /* // Display playlist info
2227 kDebug()<<"//////////// BEFORE RESIZE";
2228 for (int i = 0; i < trackPlaylist.count(); i++) {
2229 int blankStart = trackPlaylist.clip_start(i);
2230 int blankDuration = trackPlaylist.clip_length(i) - 1;
2232 if (trackPlaylist.is_blank(i)) blk = "(blank)";
2233 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
2236 if (trackPlaylist.is_blank_at((int) info.startPos.frames(m_fps))) {
2237 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
2238 m_isBlocked = false;
2241 mlt_service_lock(service.get_service());
2242 int clipIndex = trackPlaylist.get_clip_index_at((int) info.startPos.frames(m_fps));
2243 //kDebug() << "// SELECTED CLIP START: " << trackPlaylist.clip_start(clipIndex);
2244 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2245 int previousStart = clip->get_in();
2246 int newDuration = (int) clipDuration.frames(m_fps) - 1;
2247 int diff = newDuration - (trackPlaylist.clip_length(clipIndex) - 1);
2248 if (newDuration > clip->get_length()) {
2249 clip->parent().set("length", newDuration + 1);
2250 clip->set("length", newDuration + 1);
2252 if (newDuration > clip->get_out()) {
2253 clip->parent().set_in_and_out(0, newDuration + 1);
2254 clip->set_in_and_out(0, newDuration + 1);
2257 trackPlaylist.resize_clip(clipIndex, previousStart, newDuration + previousStart);
2258 trackPlaylist.consolidate_blanks(0);
2259 // skip to next clip
2261 //kDebug() << "//////// RESIZE CLIP: " << clipIndex << "( pos: " << info.startPos.frames(25) << "), DIFF: " << diff << ", CURRENT DUR: " << previousDuration << ", NEW DUR: " << newDuration << ", IX: " << clipIndex << ", MAX: " << trackPlaylist.count();
2263 // clip was made longer, trim next blank if there is one.
2264 if (clipIndex < trackPlaylist.count()) {
2265 // If this is not the last clip in playlist
2266 if (trackPlaylist.is_blank(clipIndex)) {
2267 int blankStart = trackPlaylist.clip_start(clipIndex);
2268 int blankDuration = trackPlaylist.clip_length(clipIndex) - 1;
2269 if (diff > blankDuration) kDebug() << "// ERROR blank clip is not large enough to get back required space!!!";
2270 if (diff - blankDuration == 1) {
2271 trackPlaylist.remove(clipIndex);
2272 } else trackPlaylist.remove_region(blankStart, diff - 1);
2274 kDebug() << "/// RESIZE ERROR, NXT CLIP IS NOT BLK: " << clipIndex;
2277 } else trackPlaylist.insert_blank(clipIndex, 0 - diff - 1);
2278 trackPlaylist.consolidate_blanks(0);
2279 mlt_service_unlock(service.get_service());
2281 if (info.track != 0 && clipIndex == trackPlaylist.count()) mltCheckLength();
2282 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
2283 //mltResizeTransparency(previousStart, previousStart, previousStart + newDuration, track, QString(clip->parent().get("id")).toInt());
2284 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
2285 ItemInfo transpinfo;
2286 transpinfo.startPos = info.startPos;
2287 transpinfo.endPos = info.startPos + clipDuration;
2288 transpinfo.track = info.track;
2289 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
2291 m_isBlocked = false;
2292 m_mltConsumer->set("refresh", 1);
2296 void Render::mltChangeTrackState(int track, bool mute, bool blind)
2298 Mlt::Service service(m_mltProducer->parent().get_service());
2299 Mlt::Tractor tractor(service);
2300 Mlt::Producer trackProducer(tractor.track(track));
2303 if (blind) trackProducer.set("hide", 3);
2304 else trackProducer.set("hide", 2);
2306 trackProducer.set("hide", 1);
2308 trackProducer.set("hide", 0);
2310 tractor.multitrack()->refresh();
2316 bool Render::mltResizeClipCrop(ItemInfo info, GenTime diff)
2318 Mlt::Service service(m_mltProducer->parent().get_service());
2319 int frameOffset = (int) diff.frames(m_fps);
2320 Mlt::Tractor tractor(service);
2321 Mlt::Producer trackProducer(tractor.track(info.track));
2322 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2323 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
2324 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
2327 mlt_service_lock(service.get_service());
2328 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
2329 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2331 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
2332 mlt_service_unlock(service.get_service());
2335 int previousStart = clip->get_in();
2337 int previousDuration = trackPlaylist.clip_length(clipIndex) - 1;
2339 trackPlaylist.resize_clip(clipIndex, previousStart + frameOffset, previousStart + previousDuration + frameOffset);
2340 m_isBlocked = false;
2341 mlt_service_unlock(service.get_service());
2342 m_mltConsumer->set("refresh", 1);
2346 bool Render::mltResizeClipStart(ItemInfo info, GenTime diff)
2348 //kDebug() << "//////// RSIZING CLIP from: "<<info.startPos.frames(25)<<" to "<<diff.frames(25);
2349 Mlt::Service service(m_mltProducer->parent().get_service());
2350 int moveFrame = (int) diff.frames(m_fps);
2351 Mlt::Tractor tractor(service);
2352 Mlt::Producer trackProducer(tractor.track(info.track));
2353 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2354 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
2355 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
2358 mlt_service_lock(service.get_service());
2359 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
2360 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2362 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
2363 mlt_service_unlock(service.get_service());
2366 int previousStart = clip->get_in();
2368 int previousDuration = trackPlaylist.clip_length(clipIndex) - 1;
2370 kDebug() << "RESIZE, old start: " << previousStart << ", PREV DUR: " << previousDuration << ", DIFF: " << moveFrame;
2371 trackPlaylist.resize_clip(clipIndex, previousStart + moveFrame, previousStart + previousDuration);
2372 if (moveFrame > 0) trackPlaylist.insert_blank(clipIndex, moveFrame - 1);
2374 //int midpos = info.startPos.frames(m_fps) + moveFrame - 1;
2375 int blankIndex = clipIndex - 1;
2376 int blankLength = trackPlaylist.clip_length(blankIndex);
2377 kDebug() << " + resizing blank length " << blankLength << ", SIZE DIFF: " << moveFrame;
2378 if (! trackPlaylist.is_blank(blankIndex)) {
2379 kDebug() << "WARNING, CLIP TO RESIZE IS NOT BLANK";
2381 if (blankLength + moveFrame == 0) trackPlaylist.remove(blankIndex);
2382 else trackPlaylist.resize_clip(blankIndex, 0, blankLength + moveFrame - 1);
2384 trackPlaylist.consolidate_blanks(0);
2385 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
2386 //mltResizeTransparency(previousStart, (int) moveEnd.frames(m_fps), (int) (moveEnd + out - in).frames(m_fps), track, QString(clip->parent().get("id")).toInt());
2387 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
2388 ItemInfo transpinfo;
2389 transpinfo.startPos = info.startPos + diff;
2390 transpinfo.endPos = info.startPos + diff + (info.endPos - info.startPos);
2391 transpinfo.track = info.track;
2392 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
2394 m_isBlocked = false;
2395 //m_mltConsumer->set("refresh", 1);
2396 mlt_service_unlock(service.get_service());
2397 m_mltConsumer->set("refresh", 1);
2401 bool Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd, Mlt::Producer *prod)
2403 return mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps), prod);
2407 void Render::mltUpdateClipProducer(int track, int pos, Mlt::Producer *prod)
2409 if (prod == NULL || !prod->is_valid()) {
2410 kDebug() << "// Warning, CLIP on track " << track << ", at: " << pos << " is invalid, cannot update it!!!";
2413 kDebug() << "NEW PROD ID: " << prod->get("id");
2415 kDebug() << "// TRYING TO UPDATE CLIP at: " << pos << ", TK: " << track;
2416 mlt_service_lock(m_mltConsumer->get_service());
2417 Mlt::Service service(m_mltProducer->parent().get_service());
2418 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
2420 Mlt::Tractor tractor(service);
2421 Mlt::Producer trackProducer(tractor.track(track));
2422 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2423 int clipIndex = trackPlaylist.get_clip_index_at(pos + 1);
2424 Mlt::Producer clipProducer(trackPlaylist.replace_with_blank(clipIndex));
2425 if (clipProducer.is_blank()) {
2426 kDebug() << "// ERROR UPDATING CLIP PROD";
2427 mlt_service_unlock(m_mltConsumer->get_service());
2431 Mlt::Producer *clip = prod->cut(clipProducer.get_in(), clipProducer.get_out());
2433 // move all effects to the correct producer
2434 mltPasteEffects(&clipProducer, clip);
2436 trackPlaylist.insert_at(pos, clip, 1);
2437 mlt_service_unlock(m_mltConsumer->get_service());
2441 bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd, Mlt::Producer *prod)
2445 Mlt::Service service(m_mltProducer->parent().get_service());
2446 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
2448 Mlt::Tractor tractor(service);
2449 mlt_service_lock(service.get_service());
2450 Mlt::Producer trackProducer(tractor.track(startTrack));
2451 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2452 int clipIndex = trackPlaylist.get_clip_index_at(moveStart);
2453 kDebug() << "////// LOOKING FOR CLIP TO MOVE, INDEX: " << clipIndex;
2454 bool checkLength = false;
2455 if (endTrack == startTrack) {
2456 Mlt::Producer clipProducer(trackPlaylist.replace_with_blank(clipIndex));
2457 if (!trackPlaylist.is_blank_at(moveEnd) || clipProducer.is_blank()) {
2458 // error, destination is not empty
2459 if (!trackPlaylist.is_blank_at(moveEnd)) trackPlaylist.insert_at(moveStart, clipProducer, 1);
2460 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
2461 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
2462 mlt_service_unlock(service.get_service());
2466 trackPlaylist.consolidate_blanks(0);
2467 int newIndex = trackPlaylist.insert_at(moveEnd, clipProducer, 1);
2468 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
2469 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
2471 if (newIndex + 1 == trackPlaylist.count()) checkLength = true;
2473 //mlt_service_unlock(service.get_service());
2475 Mlt::Producer destTrackProducer(tractor.track(endTrack));
2476 Mlt::Playlist destTrackPlaylist((mlt_playlist) destTrackProducer.get_service());
2477 if (!destTrackPlaylist.is_blank_at(moveEnd)) {
2478 // error, destination is not empty
2479 mlt_service_unlock(service.get_service());
2483 Mlt::Producer clipProducer(trackPlaylist.replace_with_blank(clipIndex));
2484 if (clipProducer.is_blank()) {
2485 // error, destination is not empty
2486 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
2487 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
2488 mlt_service_unlock(service.get_service());
2492 trackPlaylist.consolidate_blanks(0);
2493 destTrackPlaylist.consolidate_blanks(1);
2494 Mlt::Producer *clip;
2495 // check if we are moving a slowmotion producer
2496 QString serv = clipProducer.parent().get("mlt_service");
2497 QString currentid = clipProducer.parent().get("id");
2498 if (serv == "framebuffer" || currentid.endsWith("_video")) {
2499 clip = &clipProducer;
2502 // Special case: prod is null when using placeholder clips.
2503 // in that case, use the producer existing in playlist. Note that
2504 // it will bypass the one producer per track logic and might cause
2505 // Sound cracks if clip is moved so that it overlaps another copy of itself
2506 clip = clipProducer.cut(clipProducer.get_in(), clipProducer.get_out());
2507 } else clip = prod->cut(clipProducer.get_in(), clipProducer.get_out());
2510 // move all effects to the correct producer
2511 mltPasteEffects(&clipProducer, clip);
2513 int newIndex = destTrackPlaylist.insert_at(moveEnd, clip, 1);
2514 destTrackPlaylist.consolidate_blanks(0);
2515 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
2516 kDebug() << "//////// moving clip transparency";
2517 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
2519 if (clipIndex > trackPlaylist.count()) checkLength = true;
2520 else if (newIndex + 1 == destTrackPlaylist.count()) checkLength = true;
2523 mlt_service_unlock(service.get_service());
2524 if (checkLength) mltCheckLength();
2527 //m_mltConsumer->set("refresh", 1);
2531 bool Render::mltMoveTransition(QString type, int startTrack, int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut)
2533 int new_in = (int)newIn.frames(m_fps);
2534 int new_out = (int)newOut.frames(m_fps) - 1;
2535 if (new_in >= new_out) return false;
2537 mlt_service serv = m_mltProducer->parent().get_service();
2539 mlt_service_lock(serv);
2540 //m_mltConsumer->set("refresh", 0);
2543 mlt_service nextservice = mlt_service_get_producer(serv);
2544 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2545 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2546 QString resource = mlt_properties_get(properties, "mlt_service");
2547 int old_pos = (int)(oldIn.frames(m_fps) + oldOut.frames(m_fps)) / 2;
2549 while (mlt_type == "transition") {
2550 mlt_transition tr = (mlt_transition) nextservice;
2551 int currentTrack = mlt_transition_get_b_track(tr);
2552 int currentIn = (int) mlt_transition_get_in(tr);
2553 int currentOut = (int) mlt_transition_get_out(tr);
2555 if (resource == type && startTrack == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
2556 mlt_transition_set_in_and_out(tr, new_in, new_out);
2557 if (newTrack - startTrack != 0) {
2558 //kDebug() << "///// TRANSITION CHANGE TRACK. CUrrent (b): " << currentTrack << 'x' << mlt_transition_get_a_track(tr) << ", NEw: " << newTrack << 'x' << newTransitionTrack;
2560 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
2561 mlt_properties_set_int(properties, "a_track", newTransitionTrack);
2562 mlt_properties_set_int(properties, "b_track", newTrack);
2563 //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);
2567 nextservice = mlt_service_producer(nextservice);
2568 if (nextservice == NULL) break;
2569 properties = MLT_SERVICE_PROPERTIES(nextservice);
2570 mlt_type = mlt_properties_get(properties, "mlt_type");
2571 resource = mlt_properties_get(properties, "mlt_service");
2573 mlt_service_unlock(serv);
2576 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
2580 void Render::mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml)
2582 if (oldTag == tag) mltUpdateTransitionParams(tag, a_track, b_track, in, out, xml);
2584 mltDeleteTransition(oldTag, a_track, b_track, in, out, xml, false);
2585 mltAddTransition(tag, a_track, b_track, in, out, xml);
2587 //mltSavePlaylist();
2590 void Render::mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml)
2592 mlt_service serv = m_mltProducer->parent().get_service();
2593 mlt_service_lock(serv);
2597 mlt_service nextservice = mlt_service_get_producer(serv);
2598 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2599 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2600 QString resource = mlt_properties_get(properties, "mlt_service");
2601 int in_pos = (int) in.frames(m_fps);
2602 int out_pos = (int) out.frames(m_fps) - 1;
2604 while (mlt_type == "transition") {
2605 mlt_transition tr = (mlt_transition) nextservice;
2606 int currentTrack = mlt_transition_get_b_track(tr);
2607 int currentBTrack = mlt_transition_get_a_track(tr);
2608 int currentIn = (int) mlt_transition_get_in(tr);
2609 int currentOut = (int) mlt_transition_get_out(tr);
2611 // kDebug()<<"Looking for transition : " << currentIn <<'x'<<currentOut<< ", OLD oNE: "<<in_pos<<'x'<<out_pos;
2613 if (resource == type && b_track == currentTrack && currentIn == in_pos && currentOut == out_pos) {
2614 QMap<QString, QString> map = mltGetTransitionParamsFromXml(xml);
2615 QMap<QString, QString>::Iterator it;
2617 mlt_properties transproperties = MLT_TRANSITION_PROPERTIES(tr);
2618 mlt_properties_set_int(transproperties, "force_track", xml.attribute("force_track").toInt());
2619 if (currentBTrack != a_track) {
2620 mlt_properties_set_int(properties, "a_track", a_track);
2622 for (it = map.begin(); it != map.end(); ++it) {
2624 char *name = decodedString(key);
2625 char *value = decodedString(it.value());
2626 mlt_properties_set(transproperties, name, value);
2627 //kDebug() << " ------ UPDATING TRANS PARAM: " << name << ": " << value;
2628 //filter->set("kdenlive_id", id);
2634 nextservice = mlt_service_producer(nextservice);
2635 if (nextservice == NULL) break;
2636 properties = MLT_SERVICE_PROPERTIES(nextservice);
2637 mlt_type = mlt_properties_get(properties, "mlt_type");
2638 resource = mlt_properties_get(properties, "mlt_service");
2640 mlt_service_unlock(serv);
2643 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
2646 void Render::mltDeleteTransition(QString tag, int /*a_track*/, int b_track, GenTime in, GenTime out, QDomElement /*xml*/, bool /*do_refresh*/)
2648 mlt_service serv = m_mltProducer->parent().get_service();
2650 mlt_service_lock(serv);
2652 Mlt::Service service(serv);
2653 Mlt::Tractor tractor(service);
2654 Mlt::Field *field = tractor.field();
2656 //if (do_refresh) m_mltConsumer->set("refresh", 0);
2658 mlt_service nextservice = mlt_service_get_producer(serv);
2659 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2660 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2661 QString resource = mlt_properties_get(properties, "mlt_service");
2663 const int old_pos = (int)((in + out).frames(m_fps) / 2);
2664 kDebug() << " del trans pos: " << in.frames(25) << "-" << out.frames(25);
2666 while (mlt_type == "transition") {
2667 mlt_transition tr = (mlt_transition) nextservice;
2668 int currentTrack = mlt_transition_get_b_track(tr);
2669 int currentIn = (int) mlt_transition_get_in(tr);
2670 int currentOut = (int) mlt_transition_get_out(tr);
2671 //kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
2673 if (resource == tag && b_track == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
2674 mlt_field_disconnect_service(field->get_field(), nextservice);
2677 nextservice = mlt_service_producer(nextservice);
2678 if (nextservice == NULL) break;
2679 properties = MLT_SERVICE_PROPERTIES(nextservice);
2680 mlt_type = mlt_properties_get(properties, "mlt_type");
2681 resource = mlt_properties_get(properties, "mlt_service");
2683 mlt_service_unlock(serv);
2686 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
2689 QMap<QString, QString> Render::mltGetTransitionParamsFromXml(QDomElement xml)
2691 QDomNodeList attribs = xml.elementsByTagName("parameter");
2692 QMap<QString, QString> map;
2693 for (int i = 0; i < attribs.count(); i++) {
2694 QDomElement e = attribs.item(i).toElement();
2695 QString name = e.attribute("name");
2696 //kDebug()<<"-- TRANSITION PARAM: "<<name<<" = "<< e.attribute("name")<<" / " << e.attribute("value");
2697 map[name] = e.attribute("default");
2698 if (!e.attribute("value").isEmpty()) {
2699 map[name] = e.attribute("value");
2701 if (!e.attribute("factor").isEmpty() && e.attribute("factor").toDouble() > 0) {
2702 map[name] = QString::number(map[name].toDouble() / e.attribute("factor").toDouble());
2703 //map[name]=map[name].replace(".",","); //FIXME how to solve locale conversion of . ,
2706 if (e.attribute("namedesc").contains(';')) {
2707 QString format = e.attribute("format");
2708 QStringList separators = format.split("%d", QString::SkipEmptyParts);
2709 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
2711 QTextStream txtNeu(&neu);
2712 if (values.size() > 0)
2713 txtNeu << (int)values[0].toDouble();
2715 for (i = 0; i < separators.size() && i + 1 < values.size(); i++) {
2716 txtNeu << separators[i];
2717 txtNeu << (int)(values[i+1].toDouble());
2719 if (i < separators.size())
2720 txtNeu << separators[i];
2721 map[e.attribute("name")] = neu;
2728 void Render::mltAddClipTransparency(ItemInfo info, int transitiontrack, int id)
2730 kDebug() << "///////// ADDING CLIP TRANSPARENCY AT: " << info.startPos.frames(25);
2731 Mlt::Service service(m_mltProducer->parent().get_service());
2732 Mlt::Tractor tractor(service);
2733 Mlt::Field *field = tractor.field();
2735 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
2736 transition->set_in_and_out((int) info.startPos.frames(m_fps), (int) info.endPos.frames(m_fps) - 1);
2737 transition->set("transparency", id);
2738 transition->set("fill", 1);
2739 transition->set("internal_added", 237);
2740 field->plant_transition(*transition, transitiontrack, info.track);
2744 void Render::mltDeleteTransparency(int pos, int track, int id)
2746 Mlt::Service service(m_mltProducer->parent().get_service());
2747 Mlt::Tractor tractor(service);
2748 Mlt::Field *field = tractor.field();
2750 //if (do_refresh) m_mltConsumer->set("refresh", 0);
2751 mlt_service serv = m_mltProducer->parent().get_service();
2753 mlt_service nextservice = mlt_service_get_producer(serv);
2754 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2755 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2756 QString resource = mlt_properties_get(properties, "mlt_service");
2758 while (mlt_type == "transition") {
2759 mlt_transition tr = (mlt_transition) nextservice;
2760 int currentTrack = mlt_transition_get_b_track(tr);
2761 int currentIn = (int) mlt_transition_get_in(tr);
2762 int currentOut = (int) mlt_transition_get_out(tr);
2763 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
2764 kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
2766 if (resource == "composite" && track == currentTrack && currentIn == pos && transitionId == id) {
2767 //kDebug() << " / / / / /DELETE TRANS DOOOMNE";
2768 mlt_field_disconnect_service(field->get_field(), nextservice);
2771 nextservice = mlt_service_producer(nextservice);
2772 if (nextservice == NULL) break;
2773 properties = MLT_SERVICE_PROPERTIES(nextservice);
2774 mlt_type = mlt_properties_get(properties, "mlt_type");
2775 resource = mlt_properties_get(properties, "mlt_service");
2777 //if (do_refresh) m_mltConsumer->set("refresh", 1);
2780 void Render::mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id)
2782 Mlt::Service service(m_mltProducer->parent().get_service());
2783 Mlt::Tractor tractor(service);
2785 mlt_service_lock(service.get_service());
2786 m_mltConsumer->set("refresh", 0);
2789 mlt_service serv = m_mltProducer->parent().get_service();
2790 mlt_service nextservice = mlt_service_get_producer(serv);
2791 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2792 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2793 QString resource = mlt_properties_get(properties, "mlt_service");
2794 kDebug() << "// resize transpar from: " << oldStart << ", TO: " << newStart << 'x' << newEnd << ", " << track << ", " << id;
2795 while (mlt_type == "transition") {
2796 mlt_transition tr = (mlt_transition) nextservice;
2797 int currentTrack = mlt_transition_get_b_track(tr);
2798 int currentIn = (int) mlt_transition_get_in(tr);
2799 //mlt_properties props = MLT_TRANSITION_PROPERTIES(tr);
2800 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
2801 kDebug() << "// resize transpar current in: " << currentIn << ", Track: " << currentTrack << ", id: " << id << 'x' << transitionId ;
2802 if (resource == "composite" && track == currentTrack && currentIn == oldStart && transitionId == id) {
2803 kDebug() << " / / / / /RESIZE TRANS TO: " << newStart << 'x' << newEnd;
2804 mlt_transition_set_in_and_out(tr, newStart, newEnd);
2807 nextservice = mlt_service_producer(nextservice);
2808 if (nextservice == NULL) break;
2809 properties = MLT_SERVICE_PROPERTIES(nextservice);
2810 mlt_type = mlt_properties_get(properties, "mlt_type");
2811 resource = mlt_properties_get(properties, "mlt_service");
2813 mlt_service_unlock(service.get_service());
2815 if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
2819 void Render::mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id)
2821 Mlt::Service service(m_mltProducer->parent().get_service());
2822 Mlt::Tractor tractor(service);
2824 mlt_service_lock(service.get_service());
2825 m_mltConsumer->set("refresh", 0);
2828 mlt_service serv = m_mltProducer->parent().get_service();
2829 mlt_service nextservice = mlt_service_get_producer(serv);
2830 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2831 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2832 QString resource = mlt_properties_get(properties, "mlt_service");
2834 while (mlt_type == "transition") {
2835 mlt_transition tr = (mlt_transition) nextservice;
2836 int currentTrack = mlt_transition_get_b_track(tr);
2837 int currentaTrack = mlt_transition_get_a_track(tr);
2838 int currentIn = (int) mlt_transition_get_in(tr);
2839 int currentOut = (int) mlt_transition_get_out(tr);
2840 //mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
2841 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
2842 //kDebug()<<" + TRANSITION "<<id<<" == "<<transitionId<<", START TMIE: "<<currentIn<<", LOOK FR: "<<startTime<<", TRACK: "<<currentTrack<<'x'<<startTrack;
2843 if (resource == "composite" && transitionId == id && startTime == currentIn && startTrack == currentTrack) {
2844 kDebug() << "//////MOVING";
2845 mlt_transition_set_in_and_out(tr, endTime, endTime + currentOut - currentIn);
2846 if (endTrack != startTrack) {
2847 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
2848 mlt_properties_set_int(properties, "a_track", currentaTrack + endTrack - currentTrack);
2849 mlt_properties_set_int(properties, "b_track", endTrack);
2853 nextservice = mlt_service_producer(nextservice);
2854 if (nextservice == NULL) break;
2855 properties = MLT_SERVICE_PROPERTIES(nextservice);
2856 mlt_type = mlt_properties_get(properties, "mlt_type");
2857 resource = mlt_properties_get(properties, "mlt_service");
2860 mlt_service_unlock(service.get_service());
2861 m_mltConsumer->set("refresh", 1);
2865 bool Render::mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool /*do_refresh*/)
2867 if (in >= out) return false;
2868 QMap<QString, QString> args = mltGetTransitionParamsFromXml(xml);
2869 Mlt::Service service(m_mltProducer->parent().get_service());
2871 Mlt::Tractor tractor(service);
2872 Mlt::Field *field = tractor.field();
2874 char *transId = decodedString(tag);
2875 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, transId);
2876 if (out != GenTime())
2877 transition->set_in_and_out((int) in.frames(m_fps), (int) out.frames(m_fps) - 1);
2878 QMap<QString, QString>::Iterator it;
2880 if (xml.attribute("automatic") == "1") transition->set("automatic", 1);
2881 //kDebug() << " ------ ADDING TRANSITION PARAMs: " << args.count();
2882 if (xml.hasAttribute("id"))
2883 transition->set("kdenlive_id", xml.attribute("id").toUtf8().constData());
2885 for (it = args.begin(); it != args.end(); ++it) {
2887 char *name = decodedString(key);
2888 char *value = decodedString(it.value());
2889 if (it.value().isEmpty() == false) transition->set(name, value);
2890 //kDebug() << " ------ ADDING TRANS PARAM: " << name << ": " << value;
2894 // attach filter to the clip
2895 field->plant_transition(*transition, a_track, b_track);
2901 void Render::mltSavePlaylist()
2903 kWarning() << "// UPDATING PLAYLIST TO DISK++++++++++++++++";
2904 Mlt::Consumer fileConsumer(*m_mltProfile, "xml");
2905 fileConsumer.set("resource", "/tmp/playlist.mlt");
2907 Mlt::Service service(m_mltProducer->get_service());
2909 fileConsumer.connect(service);
2910 fileConsumer.start();
2913 QList <Mlt::Producer *> Render::producersList()
2915 QList <Mlt::Producer *> prods;
2916 Mlt::Service service(m_mltProducer->parent().get_service());
2917 if (service.type() != tractor_type) return prods;
2918 Mlt::Tractor tractor(service);
2921 int trackNb = tractor.count();
2922 for (int t = 1; t < trackNb; t++) {
2923 Mlt::Producer *tt = tractor.track(t);
2924 Mlt::Producer trackProducer(tt);
2926 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2927 int clipNb = trackPlaylist.count();
2928 //kDebug() << "// PARSING SCENE TRACK: " << t << ", CLIPS: " << clipNb;
2929 for (int i = 0; i < clipNb; i++) {
2930 Mlt::Producer *c = trackPlaylist.get_clip(i);
2931 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
2933 if (!nprod->is_blank() && !ids.contains(nprod->get("id"))) {
2934 ids.append(nprod->get("id"));
2935 prods.append(nprod);
2936 } else delete nprod;
2944 void Render::fillSlowMotionProducers()
2946 Mlt::Service service(m_mltProducer->parent().get_service());
2947 if (service.type() != tractor_type) return;
2949 Mlt::Tractor tractor(service);
2951 int trackNb = tractor.count();
2952 for (int t = 1; t < trackNb; t++) {
2953 Mlt::Producer *tt = tractor.track(t);
2954 Mlt::Producer trackProducer(tt);
2956 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2957 int clipNb = trackPlaylist.count();
2958 for (int i = 0; i < clipNb; i++) {
2959 Mlt::Producer *c = trackPlaylist.get_clip(i);
2960 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
2962 QString id = nprod->get("id");
2963 if (id.startsWith("slowmotion:") && !nprod->is_blank()) {
2964 // this is a slowmotion producer, add it to the list
2965 QString url = QString::fromUtf8(nprod->get("resource"));
2966 if (!m_slowmotionProducers.contains(url)) {
2967 m_slowmotionProducers.insert(url, nprod);
2969 } else delete nprod;
2976 void Render::mltInsertTrack(int ix, bool videoTrack)
2981 Mlt::Service service(m_mltProducer->parent().get_service());
2982 mlt_service_lock(service.get_service());
2983 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
2985 Mlt::Tractor tractor(service);
2987 Mlt::Playlist playlist;
2988 int ct = tractor.count();
2990 kDebug() << "// ERROR, TRYING TO insert TRACK " << ix << ", max: " << ct;
2996 Mlt::Producer *prodToMove = new Mlt::Producer(tractor.track(pos));
2997 tractor.set_track(playlist, pos);
2998 Mlt::Producer newProd(tractor.track(pos));
2999 if (!videoTrack) newProd.set("hide", 1);
3001 for (; pos <= ct; pos++) {
3002 Mlt::Producer *prodToMove2 = new Mlt::Producer(tractor.track(pos));
3003 tractor.set_track(*prodToMove, pos);
3004 prodToMove = prodToMove2;
3007 tractor.set_track(playlist, ix);
3008 Mlt::Producer newProd(tractor.track(ix));
3009 if (!videoTrack) newProd.set("hide", 1);
3013 mlt_service serv = m_mltProducer->parent().get_service();
3014 mlt_service nextservice = mlt_service_get_producer(serv);
3015 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3016 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3017 QString resource = mlt_properties_get(properties, "mlt_service");
3019 while (mlt_type == "transition") {
3020 if (resource != "mix") {
3021 mlt_transition tr = (mlt_transition) nextservice;
3022 int currentTrack = mlt_transition_get_b_track(tr);
3023 int currentaTrack = mlt_transition_get_a_track(tr);
3024 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3026 if (currentTrack >= ix) {
3027 mlt_properties_set_int(properties, "b_track", currentTrack + 1);
3028 mlt_properties_set_int(properties, "a_track", currentaTrack + 1);
3031 nextservice = mlt_service_producer(nextservice);
3032 if (nextservice == NULL) break;
3033 properties = MLT_SERVICE_PROPERTIES(nextservice);
3034 mlt_type = mlt_properties_get(properties, "mlt_type");
3035 resource = mlt_properties_get(properties, "mlt_service");
3038 // Add audio mix transition to last track
3039 Mlt::Field *field = tractor.field();
3040 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "mix");
3041 transition->set("a_track", 1);
3042 transition->set("b_track", ct);
3043 transition->set("always_active", 1);
3044 transition->set("internal_added", 237);
3045 transition->set("combine", 1);
3046 field->plant_transition(*transition, 1, ct);
3047 //mlt_service_unlock(m_mltConsumer->get_service());
3048 mlt_service_unlock(service.get_service());
3049 //tractor.multitrack()->refresh();
3050 //tractor.refresh();
3052 blockSignals(false);
3056 void Render::mltDeleteTrack(int ix)
3059 doc.setContent(sceneList(), false);
3060 int tracksCount = doc.elementsByTagName("track").count() - 1;
3061 QDomNode track = doc.elementsByTagName("track").at(ix);
3062 QDomNode tractor = doc.elementsByTagName("tractor").at(0);
3063 QDomNodeList transitions = doc.elementsByTagName("transition");
3064 for (int i = 0; i < transitions.count(); i++) {
3065 QDomElement e = transitions.at(i).toElement();
3066 QDomNodeList props = e.elementsByTagName("property");
3067 QMap <QString, QString> mappedProps;
3068 for (int j = 0; j < props.count(); j++) {
3069 QDomElement f = props.at(j).toElement();
3070 mappedProps.insert(f.attribute("name"), f.firstChild().nodeValue());
3072 if (mappedProps.value("mlt_service") == "mix" && mappedProps.value("b_track").toInt() == tracksCount) {
3073 tractor.removeChild(transitions.at(i));
3075 } else if (mappedProps.value("mlt_service") != "mix" && (mappedProps.value("b_track").toInt() >= ix || mappedProps.value("a_track").toInt() >= ix)) {
3076 // Transition needs to be moved
3077 int a_track = mappedProps.value("a_track").toInt();
3078 int b_track = mappedProps.value("b_track").toInt();
3079 if (a_track > 0 && a_track >= ix) a_track --;
3080 if (b_track > 0 && b_track >= ix) b_track --;
3081 for (int j = 0; j < props.count(); j++) {
3082 QDomElement f = props.at(j).toElement();
3083 if (f.attribute("name") == "a_track") f.firstChild().setNodeValue(QString::number(a_track));
3084 else if (f.attribute("name") == "b_track") f.firstChild().setNodeValue(QString::number(b_track));
3089 tractor.removeChild(track);
3090 //kDebug() << "/////////// RESULT SCENE: \n" << doc.toString();
3091 setSceneList(doc.toString(), m_framePosition);
3096 void Render::updatePreviewSettings()
3098 kDebug() << "////// RESTARTING CONSUMER";
3099 if (!m_mltConsumer || !m_mltProducer) return;
3100 if (m_mltProducer->get_playtime() == 0) return;
3101 Mlt::Service service(m_mltProducer->parent().get_service());
3102 if (service.type() != tractor_type) return;
3104 //m_mltConsumer->set("refresh", 0);
3105 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
3106 m_mltConsumer->purge();
3107 QString scene = sceneList();
3109 if (m_mltProducer) {
3110 pos = m_mltProducer->position();
3111 delete m_mltProducer;
3113 m_mltProducer = NULL;
3114 setSceneList(scene, pos);
3117 #include "renderer.moc"