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"
30 #include "slideshowclip.h"
31 #include "profilesdialog.h"
34 #include "blackmagic/devices.h"
37 #include <mlt++/Mlt.h>
40 #include <KStandardDirs>
41 #include <KMessageBox>
43 #include <KTemporaryFile>
48 #include <QApplication>
49 #include <QtConcurrentRun>
57 static void kdenlive_callback(void* /*ptr*/, int level, const char* fmt, va_list vl)
59 // kDebug() << "log level" << level << QString().vsprintf(fmt, vl).simplified();
60 if (level > MLT_LOG_ERROR) return;
62 QApplication::postEvent(qApp->activeWindow(), new MltErrorEvent(error.vsprintf(fmt, vl).simplified()));
67 static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr)
69 // detect if the producer has finished playing. Is there a better way to do it?
70 self->emitFrameNumber();
71 Mlt::Frame frame(frame_ptr);
72 if (!frame.is_valid()) return;
73 if (self->sendFrameForAnalysis && frame_ptr->convert_image) {
74 self->emitFrameUpdated(frame);
76 if (self->analyseAudio) {
77 self->showAudio(frame);
79 if (frame.get_double("_speed") < 0.0 && mlt_frame_get_position(frame_ptr) <= 0) {
81 self->emitConsumerStopped();
86 static void consumer_paused(mlt_consumer, Render * self, mlt_frame /*frame_ptr*/)
88 // detect if the producer has finished playing. Is there a better way to do it?
89 self->emitConsumerStopped();
93 static void consumer_gl_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr)
95 // detect if the producer has finished playing. Is there a better way to do it?
96 Mlt::Frame frame(frame_ptr);
97 self->showFrame(frame);
98 if (frame.get_double("_speed") == 0.0) {
99 self->emitConsumerStopped();
100 } else if (frame.get_double("_speed") < 0.0 && mlt_frame_get_position(frame_ptr) <= 0) {
102 self->emitConsumerStopped();
106 Render::Render(Kdenlive::MONITORID rendererName, int winid, QString profile, QWidget *parent) :
107 AbstractRender(rendererName, parent),
108 m_name(rendererName),
112 m_showFrameEvent(NULL),
114 m_externalConsumer(false),
117 m_isSplitView(false),
121 analyseAudio = KdenliveSettings::monitor_audio();
122 if (profile.isEmpty()) profile = KdenliveSettings::current_profile();
123 buildConsumer(profile);
124 m_mltProducer = m_blackClip->cut(0, 1);
125 m_mltConsumer->connect(*m_mltProducer);
126 m_mltProducer->set_speed(0.0);
127 m_refreshTimer.setSingleShot(true);
128 m_refreshTimer.setInterval(70);
129 connect(&m_refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
139 void Render::closeMlt()
142 m_requestList.clear();
143 m_infoThread.waitForFinished();
144 if (m_showFrameEvent) delete m_showFrameEvent;
145 if (m_pauseEvent) delete m_pauseEvent;
146 if (m_mltConsumer) delete m_mltConsumer;
147 if (m_mltProducer) delete m_mltProducer;
148 /*if (m_mltProducer) {
149 Mlt::Service service(m_mltProducer->parent().get_service());
152 if (service.type() == tractor_type) {
153 Mlt::Tractor tractor(service);
154 Mlt::Field *field = tractor.field();
155 mlt_service nextservice = mlt_service_get_producer(service.get_service());
156 mlt_service nextservicetodisconnect;
157 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
158 QString mlt_type = mlt_properties_get(properties, "mlt_type");
159 QString resource = mlt_properties_get(properties, "mlt_service");
160 // Delete all transitions
161 while (mlt_type == "transition") {
162 nextservicetodisconnect = nextservice;
163 nextservice = mlt_service_producer(nextservice);
164 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
165 if (nextservice == NULL) break;
166 properties = MLT_SERVICE_PROPERTIES(nextservice);
167 mlt_type = mlt_properties_get(properties, "mlt_type");
168 resource = mlt_properties_get(properties, "mlt_service");
177 kDebug() << "// // // CLOSE RENDERER " << m_name;
178 if (m_blackClip) delete m_blackClip;
182 void Render::slotSwitchFullscreen()
184 if (m_mltConsumer) m_mltConsumer->set("full_screen", 1);
187 void Render::buildConsumer(const QString &profileName)
192 //TODO: uncomment following line when everything is clean
193 // uncommented Feb 2011 --Granjow
194 if (m_mltProfile) delete m_mltProfile;
195 m_activeProfile = profileName;
196 char *tmp = qstrdup(m_activeProfile.toUtf8().constData());
197 setenv("MLT_PROFILE", tmp, 1);
198 m_mltProfile = new Mlt::Profile(tmp);
199 m_mltProfile->set_explicit(true);
202 m_blackClip = new Mlt::Producer(*m_mltProfile, "colour", "black");
203 m_blackClip->set("id", "black");
204 m_blackClip->set("mlt_type", "producer");
206 if (KdenliveSettings::external_display() && m_name != Kdenlive::clipMonitor) {
207 #ifdef USE_BLACKMAGIC
208 // Use blackmagic card for video output
209 QMap< QString, QString > profileProperties = ProfilesDialog::getSettingsFromFile(profileName);
210 int device = KdenliveSettings::blackmagic_output_device();
212 if (BMInterface::isSupportedProfile(device, profileProperties)) {
213 QString decklink = "decklink:" + QString::number(KdenliveSettings::blackmagic_output_device());
214 tmp = qstrdup(decklink.toUtf8().constData());
215 m_mltConsumer = new Mlt::Consumer(*m_mltProfile, tmp);
217 if (m_mltConsumer->is_valid()) {
218 m_externalConsumer = true;
219 m_showFrameEvent = m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_show);
220 m_mltConsumer->set("terminate_on_pause", 0);
221 m_mltConsumer->set("deinterlace_method", "onefield");
222 m_mltConsumer->set("real_time", KdenliveSettings::mltthreads());
223 mlt_log_set_callback(kdenlive_callback);
225 if (m_mltConsumer && m_mltConsumer->is_valid()) return;
226 } else KMessageBox::informationList(qApp->activeWindow(), i18n("Your project's profile %1 is not compatible with the blackmagic output card. Please see supported profiles below. Switching to normal video display.", m_mltProfile->description()), BMInterface::supportedModes(KdenliveSettings::blackmagic_output_device()));
230 m_externalConsumer = false;
231 QString videoDriver = KdenliveSettings::videodrivername();
232 if (!videoDriver.isEmpty()) {
233 if (videoDriver == "x11_noaccel") {
234 setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
237 unsetenv("SDL_VIDEO_YUV_HWACCEL");
240 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
242 //m_mltConsumer->set("fullscreen", 1);
245 m_mltConsumer = new Mlt::Consumer(*m_mltProfile, "sdl_audio");
246 m_mltConsumer->set("preview_off", 1);
247 m_mltConsumer->set("preview_format", mlt_image_rgb24a);
248 m_showFrameEvent = m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_gl_frame_show);
250 m_mltConsumer = new Mlt::Consumer(*m_mltProfile, "sdl_preview");
251 m_showFrameEvent = m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_show);
252 m_pauseEvent = m_mltConsumer->listen("consumer-sdl-paused", this, (mlt_listener) consumer_paused);
253 m_mltConsumer->set("window_id", m_winid);
255 m_mltConsumer->set("resize", 1);
256 m_mltConsumer->set("terminate_on_pause", 1);
257 m_mltConsumer->set("window_background", KdenliveSettings::window_background().name().toUtf8().constData());
258 m_mltConsumer->set("rescale", "nearest");
259 mlt_log_set_callback(kdenlive_callback);
261 QString audioDevice = KdenliveSettings::audiodevicename();
262 if (!audioDevice.isEmpty())
263 m_mltConsumer->set("audio_device", audioDevice.toUtf8().constData());
265 if (!videoDriver.isEmpty())
266 m_mltConsumer->set("video_driver", videoDriver.toUtf8().constData());
268 QString audioDriver = KdenliveSettings::audiodrivername();
271 // Disabled because the "auto" detected driver was sometimes wrong
272 if (audioDriver.isEmpty())
273 audioDriver = KdenliveSettings::autoaudiodrivername();
276 if (!audioDriver.isEmpty())
277 m_mltConsumer->set("audio_driver", audioDriver.toUtf8().constData());
279 m_mltConsumer->set("progressive", 1);
280 m_mltConsumer->set("audio_buffer", 1024);
281 m_mltConsumer->set("frequency", 48000);
282 m_mltConsumer->set("real_time", KdenliveSettings::mltthreads());
285 Mlt::Producer *Render::invalidProducer(const QString &id)
288 QString txt = "+" + i18n("Missing clip") + ".txt";
289 char *tmp = qstrdup(txt.toUtf8().constData());
290 clip = new Mlt::Producer(*m_mltProfile, tmp);
292 if (clip == NULL) clip = new Mlt::Producer(*m_mltProfile, "colour", "red");
294 clip->set("bgcolour", "0xff0000ff");
295 clip->set("pad", "10");
297 clip->set("id", id.toUtf8().constData());
298 clip->set("mlt_type", "producer");
302 bool Render::hasProfile(const QString &profileName) const
304 return m_activeProfile == profileName;
307 int Render::resetProfile(const QString &profileName, bool dropSceneList)
309 m_refreshTimer.stop();
311 if (m_externalConsumer == KdenliveSettings::external_display()) {
312 if (KdenliveSettings::external_display() && m_activeProfile == profileName) return 1;
313 QString videoDriver = KdenliveSettings::videodrivername();
314 QString currentDriver = m_mltConsumer->get("video_driver");
315 if (getenv("SDL_VIDEO_YUV_HWACCEL") != NULL && currentDriver == "x11") currentDriver = "x11_noaccel";
316 QString background = KdenliveSettings::window_background().name();
317 QString currentBackground = m_mltConsumer->get("window_background");
318 if (m_activeProfile == profileName && currentDriver == videoDriver && background == currentBackground) {
319 kDebug() << "reset to same profile, nothing to do";
324 if (m_isSplitView) slotSplitView(false);
325 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
326 m_mltConsumer->purge();
327 if (m_showFrameEvent) delete m_showFrameEvent;
328 m_showFrameEvent = NULL;
329 if (m_pauseEvent) delete m_pauseEvent;
331 delete m_mltConsumer;
332 m_mltConsumer = NULL;
335 if (!dropSceneList) scene = sceneList();
337 double current_fps = m_mltProfile->fps();
338 double current_dar = m_mltProfile->dar();
341 m_requestList.clear();
342 m_infoThread.waitForFinished();
345 pos = m_mltProducer->position();
347 Mlt::Service service(m_mltProducer->get_service());
348 if (service.type() == tractor_type) {
349 Mlt::Tractor tractor(service);
350 for (int trackNb = tractor.count() - 1; trackNb >= 0; --trackNb) {
351 Mlt::Producer trackProducer(tractor.track(trackNb));
352 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
353 trackPlaylist.clear();
357 delete m_mltProducer;
359 m_mltProducer = NULL;
360 buildConsumer(profileName);
361 double new_fps = m_mltProfile->fps();
362 double new_dar = m_mltProfile->dar();
364 if (!dropSceneList) {
365 // We need to recover our playlist
366 if (current_fps != new_fps) {
367 // fps changed, we must update the scenelist positions
368 scene = updateSceneListFps(current_fps, new_fps, scene);
370 setSceneList(scene, pos);
371 // producers have changed (different profile), so reset them...
372 emit refreshDocumentProducers(new_dar != current_dar, current_fps != new_fps);
377 void Render::seek(GenTime time)
382 m_mltProducer->seek((int)(time.frames(m_fps)));
383 if (m_mltProducer->get_speed() == 0) {
388 void Render::seek(int time)
393 m_mltProducer->seek(time);
394 if (m_mltProducer->get_speed() == 0) {
400 /*QPixmap Render::frameThumbnail(Mlt::Frame *frame, int width, int height, bool border) {
401 QPixmap pix(width, height);
403 mlt_image_format format = mlt_image_rgb24a;
404 uint8_t *thumb = frame->get_image(format, width, height);
405 QImage image(thumb, width, height, QImage::Format_ARGB32);
407 if (!image.isNull()) {
408 pix = pix.fromImage(image);
410 QPainter painter(&pix);
411 painter.drawRect(0, 0, width - 1, height - 1);
413 } else pix.fill(Qt::black);
417 int Render::frameRenderWidth() const
419 return m_mltProfile->width();
422 int Render::renderWidth() const
424 return (int)(m_mltProfile->height() * m_mltProfile->dar() + 0.5);
427 int Render::renderHeight() const
429 return m_mltProfile->height();
432 QImage Render::extractFrame(int frame_position, QString path, int width, int height)
435 width = frameRenderWidth();
436 height = renderHeight();
437 } else if (width % 2 == 1) width++;
438 int dwidth = height * frameRenderWidth() / renderHeight();
439 if (!path.isEmpty()) {
440 Mlt::Producer *producer = new Mlt::Producer(*m_mltProfile, path.toUtf8().constData());
442 if (producer->is_valid()) {
443 QImage img = KThumb::getFrame(producer, frame_position, dwidth, width, height);
447 else delete producer;
451 if (!m_mltProducer || !path.isEmpty()) {
452 QImage pix(width, height, QImage::Format_RGB32);
456 return KThumb::getFrame(m_mltProducer, frame_position, dwidth, width, height);
459 QPixmap Render::getImageThumbnail(KUrl url, int /*width*/, int /*height*/)
463 if (url.fileName().startsWith(".all.")) { // check for slideshow
464 QString fileType = url.fileName().right(3);
466 QStringList::Iterator it;
468 QDir dir(url.directory());
470 filter << "*." + fileType;
471 filter << "*." + fileType.toUpper();
472 more = dir.entryList(filter, QDir::Files);
473 im.load(url.directory() + '/' + more.at(0));
474 } else im.load(url.path());
475 //pixmap = im.scaled(width, height);
479 double Render::consumerRatio() const
481 if (!m_mltConsumer) return 1.0;
482 return (m_mltConsumer->get_double("aspect_ratio_num") / m_mltConsumer->get_double("aspect_ratio_den"));
486 int Render::getLength()
490 // kDebug()<<"////// LENGTH: "<<mlt_producer_get_playtime(m_mltProducer->get_producer());
491 return mlt_producer_get_playtime(m_mltProducer->get_producer());
496 bool Render::isValid(KUrl url)
498 Mlt::Producer producer(*m_mltProfile, url.path().toUtf8().constData());
499 if (producer.is_blank())
505 double Render::dar() const
507 return m_mltProfile->dar();
510 double Render::sar() const
512 return m_mltProfile->sar();
515 void Render::slotSplitView(bool doit)
517 m_isSplitView = doit;
518 Mlt::Service service(m_mltProducer->parent().get_service());
519 Mlt::Tractor tractor(service);
520 if (service.type() != tractor_type || tractor.count() < 2) return;
521 Mlt::Field *field = tractor.field();
523 for (int i = 1, screen = 0; i < tractor.count() && screen < 4; i++) {
524 Mlt::Producer trackProducer(tractor.track(i));
525 kDebug() << "// TRACK: " << i << ", HIDE: " << trackProducer.get("hide");
526 if (QString(trackProducer.get("hide")).toInt() != 1) {
527 kDebug() << "// ADIDNG TRACK: " << i;
528 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
529 transition->set("mlt_service", "composite");
530 transition->set("a_track", 0);
531 transition->set("b_track", i);
532 transition->set("distort", 1);
533 transition->set("internal_added", "200");
540 tmp = "50%,0:50%x50%";
543 tmp = "0,50%:50%x50%";
547 tmp = "50%,50%:50%x50%";
550 transition->set("geometry", tmp);
551 transition->set("always_active", "1");
552 field->plant_transition(*transition, 0, i);
557 m_mltConsumer->set("refresh", 1);
559 mlt_service serv = m_mltProducer->parent().get_service();
560 mlt_service nextservice = mlt_service_get_producer(serv);
561 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
562 QString mlt_type = mlt_properties_get(properties, "mlt_type");
563 QString resource = mlt_properties_get(properties, "mlt_service");
564 mlt_service nextservicetodisconnect;
566 while (mlt_type == "transition") {
567 QString added = mlt_properties_get(MLT_SERVICE_PROPERTIES(nextservice), "internal_added");
568 if (added == "200") {
569 nextservicetodisconnect = nextservice;
570 nextservice = mlt_service_producer(nextservice);
571 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
573 else nextservice = mlt_service_producer(nextservice);
574 if (nextservice == NULL) break;
575 properties = MLT_SERVICE_PROPERTIES(nextservice);
576 mlt_type = mlt_properties_get(properties, "mlt_type");
577 resource = mlt_properties_get(properties, "mlt_service");
578 m_mltConsumer->set("refresh", 1);
583 void Render::getFileProperties(const QDomElement &xml, const QString &clipId, int imageHeight, bool replaceProducer)
585 requestClipInfo info;
587 info.clipId = clipId;
588 info.imageHeight = imageHeight;
589 info.replaceProducer = replaceProducer;
590 // Make sure we don't request the info for same clip twice
592 m_requestList.removeAll(info);
593 m_requestList.append(info);
594 m_infoMutex.unlock();
595 if (!m_infoThread.isRunning()) {
596 m_infoThread = QtConcurrent::run(this, &Render::processFileProperties);
600 void Render::forceProcessing(const QString &id)
602 if (m_processingClipId == id) return;
603 QMutexLocker lock(&m_infoMutex);
604 for (int i = 0; i < m_requestList.count(); i++) {
605 requestClipInfo info = m_requestList.at(i);
606 if (info.clipId == id) {
609 m_requestList.removeAt(i);
610 m_requestList.prepend(info);
617 int Render::processingItems()
619 QMutexLocker lock(&m_infoMutex);
620 int count = m_requestList.count();
621 if (!m_processingClipId.isEmpty()) {
622 // one clip is currently processed
628 bool Render::isProcessing(const QString &id)
630 if (m_processingClipId == id) return true;
631 QMutexLocker lock(&m_infoMutex);
632 for (int i = 0; i < m_requestList.count(); i++) {
633 requestClipInfo info = m_requestList.at(i);
634 if (info.clipId == id) {
641 void Render::processFileProperties()
643 requestClipInfo info;
645 while (!m_requestList.isEmpty()) {
647 info = m_requestList.takeFirst();
648 m_processingClipId = info.clipId;
649 m_infoMutex.unlock();
653 if (info.xml.hasAttribute("proxy") && info.xml.attribute("proxy") != "-") {
654 path = info.xml.attribute("proxy");
655 // Check for missing proxies
656 if (QFileInfo(path).size() <= 0) {
657 // proxy is missing, re-create it
658 emit requestProxy(info.clipId);
659 proxyProducer = false;
660 path = info.xml.attribute("resource");
662 else proxyProducer = true;
665 path = info.xml.attribute("resource");
666 proxyProducer = false;
669 Mlt::Producer *producer = NULL;
670 CLIPTYPE type = (CLIPTYPE)info.xml.attribute("type").toInt();
673 producer = new Mlt::Producer(*m_mltProfile, 0, ("colour:" + info.xml.attribute("colour")).toUtf8().constData());
674 } else if (type == TEXT) {
675 producer = new Mlt::Producer(*m_mltProfile, 0, ("kdenlivetitle:" + info.xml.attribute("resource")).toUtf8().constData());
676 if (producer && producer->is_valid() && info.xml.hasAttribute("xmldata"))
677 producer->set("xmldata", info.xml.attribute("xmldata").toUtf8().constData());
678 } else if (url.isEmpty()) {
680 QDomElement mlt = doc.createElement("mlt");
681 QDomElement play = doc.createElement("playlist");
682 doc.appendChild(mlt);
683 mlt.appendChild(play);
684 play.appendChild(doc.importNode(info.xml, true));
685 producer = new Mlt::Producer(*m_mltProfile, "xml-string", doc.toString().toUtf8().constData());
687 producer = new Mlt::Producer(*m_mltProfile, path.toUtf8().constData());
690 if (producer == NULL || producer->is_blank() || !producer->is_valid()) {
691 kDebug() << " / / / / / / / / ERROR / / / / // CANNOT LOAD PRODUCER: "<<path;
692 m_processingClipId.clear();
694 // Proxy file is corrupted
695 emit removeInvalidProxy(info.clipId, false);
697 else emit removeInvalidClip(info.clipId, info.replaceProducer);
702 if (proxyProducer && info.xml.hasAttribute("proxy_out")) {
703 producer->set("length", info.xml.attribute("proxy_out").toInt() + 1);
704 producer->set("out", info.xml.attribute("proxy_out").toInt());
705 if (producer->get_out() != info.xml.attribute("proxy_out").toInt()) {
706 // Proxy file length is different than original clip length, this will corrupt project so disable this proxy clip
707 m_processingClipId.clear();
708 emit removeInvalidProxy(info.clipId, true);
714 if (info.xml.hasAttribute("force_aspect_ratio")) {
715 double aspect = info.xml.attribute("force_aspect_ratio").toDouble();
716 if (aspect > 0) producer->set("force_aspect_ratio", aspect);
719 if (info.xml.hasAttribute("force_aspect_num") && info.xml.hasAttribute("force_aspect_den")) {
720 int width = info.xml.attribute("frame_size").section('x', 0, 0).toInt();
721 int height = info.xml.attribute("frame_size").section('x', 1, 1).toInt();
722 int aspectNumerator = info.xml.attribute("force_aspect_num").toInt();
723 int aspectDenominator = info.xml.attribute("force_aspect_den").toInt();
724 if (aspectDenominator != 0 && width != 0)
725 producer->set("force_aspect_ratio", double(height) * aspectNumerator / aspectDenominator / width);
728 if (info.xml.hasAttribute("force_fps")) {
729 double fps = info.xml.attribute("force_fps").toDouble();
730 if (fps > 0) producer->set("force_fps", fps);
733 if (info.xml.hasAttribute("force_progressive")) {
735 int progressive = info.xml.attribute("force_progressive").toInt(&ok);
736 if (ok) producer->set("force_progressive", progressive);
738 if (info.xml.hasAttribute("force_tff")) {
740 int fieldOrder = info.xml.attribute("force_tff").toInt(&ok);
741 if (ok) producer->set("force_tff", fieldOrder);
743 if (info.xml.hasAttribute("threads")) {
744 int threads = info.xml.attribute("threads").toInt();
745 if (threads != 1) producer->set("threads", threads);
747 if (info.xml.hasAttribute("video_index")) {
748 int vindex = info.xml.attribute("video_index").toInt();
749 if (vindex != 0) producer->set("video_index", vindex);
751 if (info.xml.hasAttribute("audio_index")) {
752 int aindex = info.xml.attribute("audio_index").toInt();
753 if (aindex != 0) producer->set("audio_index", aindex);
755 if (info.xml.hasAttribute("force_colorspace")) {
756 int colorspace = info.xml.attribute("force_colorspace").toInt();
757 if (colorspace != 0) producer->set("force_colorspace", colorspace);
759 if (info.xml.hasAttribute("full_luma")) {
760 int full_luma = info.xml.attribute("full_luma").toInt();
761 if (full_luma != 0) producer->set("set.force_full_luma", full_luma);
766 if (info.xml.hasAttribute("out")) clipOut = info.xml.attribute("out").toInt();
768 // setup length here as otherwise default length (currently 15000 frames in MLT) will be taken even if outpoint is larger
769 if (type == COLOR || type == TEXT || type == IMAGE || type == SLIDESHOW) {
771 if (info.xml.hasAttribute("length")) {
772 if (clipOut > 0) duration = clipOut + 1;
773 length = info.xml.attribute("length").toInt();
774 clipOut = length - 1;
776 else length = info.xml.attribute("out").toInt() - info.xml.attribute("in").toInt();
777 producer->set("length", length);
780 if (clipOut > 0) producer->set_in_and_out(info.xml.attribute("in").toInt(), clipOut);
782 producer->set("id", info.clipId.toUtf8().constData());
784 if (info.xml.hasAttribute("templatetext"))
785 producer->set("templatetext", info.xml.attribute("templatetext").toUtf8().constData());
787 int imageWidth = (int)((double) info.imageHeight * m_mltProfile->width() / m_mltProfile->height() + 0.5);
788 int fullWidth = (int)((double) info.imageHeight * m_mltProfile->dar() + 0.5);
789 int frameNumber = info.xml.attribute("thumbnail", "-1").toInt();
791 if ((!info.replaceProducer && info.xml.hasAttribute("file_hash")) || proxyProducer) {
792 // Clip already has all properties
794 // Recreate clip thumb
795 if (frameNumber > 0) producer->seek(frameNumber);
796 Mlt::Frame *frame = producer->get_frame();
797 if (frame && frame->is_valid()) {
798 QImage img = KThumb::getFrame(frame, imageWidth, fullWidth, info.imageHeight);
799 emit replyGetImage(info.clipId, img);
801 if (frame) delete frame;
803 m_processingClipId.clear();
804 emit replyGetFileProperties(info.clipId, producer, stringMap(), stringMap(), info.replaceProducer);
808 stringMap filePropertyMap;
809 stringMap metadataPropertyMap;
812 if (frameNumber > 0) producer->seek(frameNumber);
814 duration = duration > 0 ? duration : producer->get_playtime();
815 filePropertyMap["duration"] = QString::number(duration);
816 //kDebug() << "/////// PRODUCER: " << url.path() << " IS: " << producer->get_playtime();
818 if (type == SLIDESHOW) {
819 int ttl = info.xml.hasAttribute("ttl") ? info.xml.attribute("ttl").toInt() : 0;
820 if (ttl) producer->set("ttl", ttl);
821 if (!info.xml.attribute("animation").isEmpty()) {
822 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "affine");
823 if (filter && filter->is_valid()) {
825 QString geometry = SlideshowClip::animationToGeometry(info.xml.attribute("animation"), cycle);
826 if (!geometry.isEmpty()) {
827 if (info.xml.attribute("animation").contains("low-pass")) {
828 Mlt::Filter *blur = new Mlt::Filter(*m_mltProfile, "boxblur");
829 if (blur && blur->is_valid())
830 producer->attach(*blur);
832 filter->set("transition.geometry", geometry.toUtf8().data());
833 filter->set("transition.cycle", cycle);
834 producer->attach(*filter);
838 if (info.xml.attribute("fade") == "1") {
839 // user wants a fade effect to slideshow
840 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "luma");
841 if (filter && filter->is_valid()) {
842 if (ttl) filter->set("cycle", ttl);
843 if (info.xml.hasAttribute("luma_duration") && !info.xml.attribute("luma_duration").isEmpty()) filter->set("duration", info.xml.attribute("luma_duration").toInt());
844 if (info.xml.hasAttribute("luma_file") && !info.xml.attribute("luma_file").isEmpty()) {
845 filter->set("luma.resource", info.xml.attribute("luma_file").toUtf8().constData());
846 if (info.xml.hasAttribute("softness")) {
847 int soft = info.xml.attribute("softness").toInt();
848 filter->set("luma.softness", (double) soft / 100.0);
851 producer->attach(*filter);
854 if (info.xml.attribute("crop") == "1") {
855 // user wants to center crop the slides
856 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "crop");
857 if (filter && filter->is_valid()) {
858 filter->set("center", 1);
859 producer->attach(*filter);
865 int vindex = producer->get_int("video_index");
867 snprintf(property, sizeof(property), "meta.media.%d.stream.frame_rate", vindex);
868 if (producer->get(property))
869 filePropertyMap["fps"] = producer->get(property);
872 if (!filePropertyMap.contains("fps")) {
873 if (producer->get_double("meta.media.frame_rate_den") > 0) {
874 filePropertyMap["fps"] = locale.toString(producer->get_double("meta.media.frame_rate_num") / producer->get_double("meta.media.frame_rate_den"));
875 } else filePropertyMap["fps"] = producer->get("source_fps");
878 Mlt::Frame *frame = producer->get_frame();
879 if (frame && frame->is_valid()) {
880 filePropertyMap["frame_size"] = QString::number(frame->get_int("width")) + 'x' + QString::number(frame->get_int("height"));
881 filePropertyMap["frequency"] = QString::number(frame->get_int("frequency"));
882 filePropertyMap["channels"] = QString::number(frame->get_int("channels"));
883 filePropertyMap["aspect_ratio"] = frame->get("aspect_ratio");
885 if (frame->get_int("test_image") == 0) {
886 if (url.path().endsWith(".mlt") || url.path().endsWith(".westley") || url.path().endsWith(".kdenlive")) {
887 filePropertyMap["type"] = "playlist";
888 metadataPropertyMap["comment"] = QString::fromUtf8(producer->get("title"));
889 } else if (frame->get_int("test_audio") == 0)
890 filePropertyMap["type"] = "av";
892 filePropertyMap["type"] = "video";
898 img = KThumb::getFrame(frame, imageWidth, fullWidth, info.imageHeight);
899 variance = KThumb::imageVariance(img);
900 if (frameNumber == -1 && variance< 6) {
901 // Thumbnail is not interesting (for example all black, seek to fetch better thumb
902 frameNumber = duration > 100 ? 100 : duration / 2 ;
903 producer->seek(frameNumber);
905 frame = producer->get_frame();
908 } while (variance == -1);
910 if (frameNumber > -1) filePropertyMap["thumbnail"] = QString::number(frameNumber);
911 emit replyGetImage(info.clipId, img);
912 } else if (frame->get_int("test_audio") == 0) {
913 emit replyGetImage(info.clipId, "audio-x-generic", fullWidth, info.imageHeight);
914 filePropertyMap["type"] = "audio";
917 // Retrieve audio / video codec name
921 /*if (context->duration == AV_NOPTS_VALUE) {
922 kDebug() << " / / / / / / / /ERROR / / / CLIP HAS UNKNOWN DURATION";
923 emit removeInvalidClip(clipId);
927 // Get the video_index
929 int default_audio = producer->get_int("audio_index");
932 int scan = producer->get_int("meta.media.progressive");
933 filePropertyMap["progressive"] = QString::number(scan);
935 // Find maximum stream index values
936 for (int ix = 0; ix < producer->get_int("meta.media.nb_streams"); ix++) {
937 snprintf(property, sizeof(property), "meta.media.%d.stream.type", ix);
938 QString type = producer->get(property);
941 else if (type == "audio")
944 filePropertyMap["default_video"] = QString::number(vindex);
945 filePropertyMap["video_max"] = QString::number(video_max);
946 filePropertyMap["default_audio"] = QString::number(default_audio);
947 filePropertyMap["audio_max"] = QString::number(audio_max);
949 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", vindex);
950 if (producer->get(property)) {
951 filePropertyMap["videocodec"] = producer->get(property);
953 snprintf(property, sizeof(property), "meta.media.%d.codec.name", vindex);
954 if (producer->get(property))
955 filePropertyMap["videocodec"] = producer->get(property);
958 query = QString("meta.media.%1.codec.pix_fmt").arg(vindex);
959 filePropertyMap["pix_fmt"] = producer->get(query.toUtf8().constData());
960 filePropertyMap["colorspace"] = producer->get("meta.media.colorspace");
962 } else kDebug() << " / / / / /WARNING, VIDEO CONTEXT IS NULL!!!!!!!!!!!!!!";
963 if (producer->get_int("audio_index") > -1) {
964 // Get the audio_index
965 int index = producer->get_int("audio_index");
966 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", index);
967 if (producer->get(property)) {
968 filePropertyMap["audiocodec"] = producer->get(property);
970 snprintf(property, sizeof(property), "meta.media.%d.codec.name", index);
971 if (producer->get(property))
972 filePropertyMap["audiocodec"] = producer->get(property);
977 Mlt::Properties metadata;
978 metadata.pass_values(*producer, "meta.attr.");
979 int count = metadata.count();
980 for (int i = 0; i < count; i ++) {
981 QString name = metadata.get_name(i);
982 QString value = QString::fromUtf8(metadata.get(i));
983 if (name.endsWith("markup") && !value.isEmpty())
984 metadataPropertyMap[ name.section('.', 0, -2)] = value;
987 m_processingClipId.clear();
988 emit replyGetFileProperties(info.clipId, producer, filePropertyMap, metadataPropertyMap, info.replaceProducer);
990 m_processingClipId.clear();
995 /** Create the producer from the MLT XML QDomDocument */
996 void Render::initSceneList()
998 kDebug() << "-------- INIT SCENE LIST ------_";
1000 QDomElement mlt = doc.createElement("mlt");
1001 doc.appendChild(mlt);
1002 QDomElement prod = doc.createElement("producer");
1003 prod.setAttribute("resource", "colour");
1004 prod.setAttribute("colour", "red");
1005 prod.setAttribute("id", "black");
1006 prod.setAttribute("in", "0");
1007 prod.setAttribute("out", "0");
1009 QDomElement tractor = doc.createElement("tractor");
1010 QDomElement multitrack = doc.createElement("multitrack");
1012 QDomElement playlist1 = doc.createElement("playlist");
1013 playlist1.appendChild(prod);
1014 multitrack.appendChild(playlist1);
1015 QDomElement playlist2 = doc.createElement("playlist");
1016 multitrack.appendChild(playlist2);
1017 QDomElement playlist3 = doc.createElement("playlist");
1018 multitrack.appendChild(playlist3);
1019 QDomElement playlist4 = doc.createElement("playlist");
1020 multitrack.appendChild(playlist4);
1021 QDomElement playlist5 = doc.createElement("playlist");
1022 multitrack.appendChild(playlist5);
1023 tractor.appendChild(multitrack);
1024 mlt.appendChild(tractor);
1025 // kDebug()<<doc.toString();
1027 QString tmp = QString("<mlt><producer resource=\"colour\" colour=\"red\" id=\"red\" /><tractor><multitrack><playlist></playlist><playlist></playlist><playlist /><playlist /><playlist></playlist></multitrack></tractor></mlt>");*/
1028 setSceneList(doc, 0);
1032 int Render::setProducer(Mlt::Producer *producer, int position)
1034 m_refreshTimer.stop();
1035 QMutexLocker locker(&m_mutex);
1037 int consumerPosition = 0;
1038 if (m_winid == -1 || !m_mltConsumer) {
1039 kDebug()<<" / / / / WARNING, MONITOR NOT READY";
1040 if (producer) delete producer;
1043 m_mltConsumer->set("refresh", 0);
1044 if (!m_mltConsumer->is_stopped()) {
1045 m_mltConsumer->stop();
1047 m_mltConsumer->purge();
1048 consumerPosition = m_mltConsumer->position();
1052 if (!producer || !producer->is_valid()) {
1053 if (producer) delete producer;
1054 producer = m_blackClip->cut(0, 1);
1057 if (!producer || !producer->is_valid()) {
1058 kDebug() << " WARNING - - - - -INVALID PLAYLIST: ";
1061 if (m_mltProducer) currentId = m_mltProducer->get("id");
1063 if (position == -1 && producer->get("id") == currentId) position = consumerPosition;
1064 if (position != -1) producer->seek(position);
1065 int volume = KdenliveSettings::volume();
1066 producer->set("meta.volume", (double)volume / 100);
1067 m_fps = producer->get_fps();
1068 blockSignals(false);
1069 m_mltConsumer->connect(*producer);
1071 if (m_mltProducer) {
1072 m_mltProducer->set_speed(0);
1073 delete m_mltProducer;
1074 m_mltProducer = NULL;
1076 m_mltProducer = producer;
1077 m_mltProducer->set_speed(0);
1078 emit durationChanged(m_mltProducer->get_playtime());
1079 if (m_mltConsumer->start() == -1) {
1080 // ARGH CONSUMER BROKEN!!!!
1081 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."));
1082 if (m_showFrameEvent) delete m_showFrameEvent;
1083 m_showFrameEvent = NULL;
1084 if (m_pauseEvent) delete m_pauseEvent;
1085 m_pauseEvent = NULL;
1086 delete m_mltConsumer;
1087 m_mltConsumer = NULL;
1091 position = m_mltProducer->position();
1092 m_mltConsumer->set("refresh", 1);
1093 // Make sure the first frame is displayed, otherwise if we change producer too fast
1094 // We can crash the avformat producer
1095 Mlt::Event *ev = m_mltConsumer->setup_wait_for("consumer-frame-show");
1096 m_mltConsumer->wait_for(ev);
1098 emit rendererPosition(position);
1102 int Render::setSceneList(QDomDocument list, int position)
1104 return setSceneList(list.toString(), position);
1107 int Render::setSceneList(QString playlist, int position)
1109 m_refreshTimer.stop();
1110 QMutexLocker locker(&m_mutex);
1111 if (m_winid == -1) return -1;
1114 //kDebug() << "////// RENDER, SET SCENE LIST:\n" << playlist <<"\n..........:::.";
1116 // Remove previous profile info
1118 doc.setContent(playlist);
1119 QDomElement profile = doc.documentElement().firstChildElement("profile");
1120 doc.documentElement().removeChild(profile);
1121 playlist = doc.toString();
1123 if (m_mltConsumer) {
1124 if (!m_mltConsumer->is_stopped()) {
1125 m_mltConsumer->stop();
1127 m_mltConsumer->set("refresh", 0);
1129 kWarning() << "/////// ERROR, TRYING TO USE NULL MLT CONSUMER";
1132 m_requestList.clear();
1133 m_infoThread.waitForFinished();
1135 if (m_mltProducer) {
1136 m_mltProducer->set_speed(0);
1137 //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
1139 /*Mlt::Service service(m_mltProducer->parent().get_service());
1142 if (service.type() == tractor_type) {
1143 Mlt::Tractor tractor(service);
1144 Mlt::Field *field = tractor.field();
1145 mlt_service nextservice = mlt_service_get_producer(service.get_service());
1146 mlt_service nextservicetodisconnect;
1147 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
1148 QString mlt_type = mlt_properties_get(properties, "mlt_type");
1149 QString resource = mlt_properties_get(properties, "mlt_service");
1150 // Delete all transitions
1151 while (mlt_type == "transition") {
1152 nextservicetodisconnect = nextservice;
1153 nextservice = mlt_service_producer(nextservice);
1154 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
1155 if (nextservice == NULL) break;
1156 properties = MLT_SERVICE_PROPERTIES(nextservice);
1157 mlt_type = mlt_properties_get(properties, "mlt_type");
1158 resource = mlt_properties_get(properties, "mlt_service");
1162 for (int trackNb = tractor.count() - 1; trackNb >= 0; --trackNb) {
1163 Mlt::Producer trackProducer(tractor.track(trackNb));
1164 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1165 if (trackPlaylist.type() == playlist_type) trackPlaylist.clear();
1171 qDeleteAll(m_slowmotionProducers.values());
1172 m_slowmotionProducers.clear();
1174 delete m_mltProducer;
1175 m_mltProducer = NULL;
1180 m_locale = QLocale();
1182 m_mltProducer = new Mlt::Producer(*m_mltProfile, "xml-string", playlist.toUtf8().constData());
1183 if (!m_mltProducer || !m_mltProducer->is_valid()) {
1184 kDebug() << " WARNING - - - - -INVALID PLAYLIST: " << playlist.toUtf8().constData();
1185 m_mltProducer = m_blackClip->cut(0, 1);
1189 int volume = KdenliveSettings::volume();
1190 m_mltProducer->set("meta.volume", (double)volume / 100);
1191 m_mltProducer->optimise();
1193 /*if (KdenliveSettings::osdtimecode()) {
1194 // Attach filter for on screen display of timecode
1196 QString attr = "attr_check";
1197 mlt_filter filter = mlt_factory_filter( "data_feed", (char*) attr.ascii() );
1198 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
1199 mlt_producer_attach( m_mltProducer->get_producer(), filter );
1200 mlt_filter_close( filter );
1202 m_osdInfo = new Mlt::Filter("data_show");
1203 m_osdInfo->set("resource", m_osdProfile.toUtf8().constData());
1204 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1205 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
1206 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
1207 m_osdInfo->set("dynamic", "1");
1209 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1211 m_osdInfo->set("dynamic", "0");
1214 m_fps = m_mltProducer->get_fps();
1215 if (position != 0) {
1216 // Seek to correct place after opening project.
1217 m_mltProducer->seek(position);
1220 kDebug() << "// NEW SCENE LIST DURATION SET TO: " << m_mltProducer->get_playtime();
1221 m_mltConsumer->connect(*m_mltProducer);
1222 m_mltProducer->set_speed(0);
1223 fillSlowMotionProducers();
1224 blockSignals(false);
1225 emit durationChanged(m_mltProducer->get_playtime());
1228 //kDebug()<<"// SETSCN LST, POS: "<<position;
1229 //if (position != 0) emit rendererPosition(position);
1232 void Render::checkMaxThreads()
1234 // Make sure we don't use too much threads, MLT avformat does not cope with too much threads
1235 // Currently, Kdenlive uses the following avformat threads:
1236 // One thread to get info when adding a clip
1237 // One thread to create the timeline video thumbnails
1238 // One thread to create the audio thumbnails
1239 Mlt::Service service(m_mltProducer->parent().get_service());
1240 if (service.type() != tractor_type) {
1241 kWarning() << "// TRACTOR PROBLEM";
1244 Mlt::Tractor tractor(service);
1245 int mltMaxThreads = mlt_service_cache_get_size(service.get_service(), "producer_avformat");
1246 int requestedThreads = tractor.count() + 4;
1247 if (requestedThreads > mltMaxThreads) {
1248 mlt_service_cache_set_size(service.get_service(), "producer_avformat", requestedThreads);
1249 kDebug()<<"// MLT threads updated to: "<<mlt_service_cache_get_size(service.get_service(), "producer_avformat");
1253 const QString Render::sceneList()
1256 Mlt::Profile profile((mlt_profile) 0);
1257 Mlt::Consumer xmlConsumer(profile, "xml:kdenlive_playlist");
1258 if (!xmlConsumer.is_valid()) return QString();
1259 m_mltProducer->optimise();
1260 xmlConsumer.set("terminate_on_pause", 1);
1261 Mlt::Producer prod(m_mltProducer->get_producer());
1262 if (!prod.is_valid()) return QString();
1263 bool split = m_isSplitView;
1264 if (split) slotSplitView(false);
1265 xmlConsumer.connect(prod);
1267 playlist = QString::fromUtf8(xmlConsumer.get("kdenlive_playlist"));
1268 if (split) slotSplitView(true);
1272 bool Render::saveSceneList(QString path, QDomElement kdenliveData)
1276 doc.setContent(sceneList(), false);
1277 if (doc.isNull()) return false;
1278 QDomElement root = doc.documentElement();
1279 if (!kdenliveData.isNull() && !root.isNull()) {
1280 // add Kdenlive specific tags
1281 root.appendChild(doc.importNode(kdenliveData, true));
1283 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
1284 kWarning() << "////// ERROR writing to file: " << path;
1287 file.write(doc.toString().toUtf8());
1288 if (file.error() != QFile::NoError) {
1296 void Render::saveZone(KUrl url, QString desc, QPoint zone)
1298 Mlt::Consumer xmlConsumer(*m_mltProfile, ("xml:" + url.path()).toUtf8().constData());
1299 m_mltProducer->optimise();
1300 xmlConsumer.set("terminate_on_pause", 1);
1301 if (m_name == Kdenlive::clipMonitor) {
1302 Mlt::Producer *prod = m_mltProducer->cut(zone.x(), zone.y());
1304 list.insert_at(0, prod, 0);
1306 list.set("title", desc.toUtf8().constData());
1307 xmlConsumer.connect(list);
1310 //TODO: not working yet, save zone from timeline
1311 Mlt::Producer *p1 = new Mlt::Producer(m_mltProducer->get_producer());
1312 /* Mlt::Service service(p1->parent().get_service());
1313 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";*/
1315 //Mlt::Producer *prod = p1->cut(zone.x(), zone.y());
1316 //prod->set("title", desc.toUtf8().constData());
1317 xmlConsumer.connect(*p1); //list);
1320 xmlConsumer.start();
1324 bool Render::saveClip(int track, GenTime position, KUrl url, QString desc)
1327 Mlt::Service service(m_mltProducer->parent().get_service());
1328 Mlt::Tractor tractor(service);
1329 Mlt::Producer trackProducer(tractor.track(track));
1330 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1332 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
1333 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
1335 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
1339 Mlt::Consumer xmlConsumer(*m_mltProfile, ("xml:" + url.path()).toUtf8().constData());
1340 xmlConsumer.set("terminate_on_pause", 1);
1342 list.insert_at(0, clip, 0);
1344 list.set("title", desc.toUtf8().constData());
1345 xmlConsumer.connect(list);
1347 kDebug()<<"// SAVED: "<<url;
1351 double Render::fps() const
1356 int Render::volume() const
1358 if (!m_mltConsumer || !m_mltProducer) return -1;
1359 return ((int) 100 * m_mltProducer->get_double("meta.volume"));
1362 void Render::slotSetVolume(int volume)
1364 if (!m_mltConsumer || !m_mltProducer) return;
1365 m_mltProducer->set("meta.volume", (double)volume / 100.0);
1368 m_mltConsumer->set("refresh", 0);
1369 // Attach filter for on screen display of timecode
1370 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1371 mlt_properties_set_double( properties, "meta.volume", volume );
1372 mlt_properties_set_int( properties, "meta.attr.osdvolume", 1);
1373 mlt_properties_set( properties, "meta.attr.osdvolume.markup", i18n("Volume: ") + QString::number(volume * 100));
1375 if (!KdenliveSettings::osdtimecode()) {
1376 m_mltProducer->detach(*m_osdInfo);
1377 mlt_properties_set_int( properties, "meta.attr.timecode", 0);
1378 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1381 //m_osdTimer->setSingleShot(2500);
1384 void Render::slotOsdTimeout()
1386 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1387 mlt_properties_set_int(properties, "meta.attr.osdvolume", 0);
1388 mlt_properties_set(properties, "meta.attr.osdvolume.markup", NULL);
1389 //if (!KdenliveSettings::osdtimecode()) m_mltProducer->detach(*m_osdInfo);
1393 void Render::start()
1395 m_refreshTimer.stop();
1396 QMutexLocker locker(&m_mutex);
1397 if (m_winid == -1) {
1398 kDebug() << "----- BROKEN MONITOR: " << m_name << ", RESTART";
1401 if (!m_mltConsumer) return;
1402 if (m_mltConsumer->is_stopped()) {
1403 if (m_mltConsumer->start() == -1) {
1404 //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."));
1405 kDebug(QtWarningMsg) << "/ / / / CANNOT START MONITOR";
1407 m_mltConsumer->purge();
1408 m_mltConsumer->set("refresh", 1);
1415 m_refreshTimer.stop();
1416 QMutexLocker locker(&m_mutex);
1417 if (m_mltProducer == NULL) return;
1418 if (m_mltConsumer && !m_mltConsumer->is_stopped()) {
1419 m_mltConsumer->stop();
1420 m_mltConsumer->purge();
1423 if (m_mltProducer) {
1424 if (m_isZoneMode) resetZoneMode();
1425 m_mltProducer->set_speed(0.0);
1429 void Render::stop(const GenTime & startTime)
1431 m_refreshTimer.stop();
1432 QMutexLocker locker(&m_mutex);
1433 if (m_mltProducer) {
1434 if (m_isZoneMode) resetZoneMode();
1435 m_mltProducer->set_speed(0.0);
1436 m_mltProducer->seek((int) startTime.frames(m_fps));
1438 m_mltConsumer->purge();
1441 void Render::pause()
1443 if (!m_mltProducer || !m_mltConsumer)
1445 if (m_mltProducer->get_speed() == 0.0) return;
1446 if (m_isZoneMode) resetZoneMode();
1447 m_mltConsumer->set("refresh", 0);
1448 m_mltProducer->set_speed(0.0);
1449 m_mltConsumer->purge();
1452 void Render::switchPlay(bool play)
1454 QMutexLocker locker(&m_mutex);
1455 if (!m_mltProducer || !m_mltConsumer)
1457 if (m_isZoneMode) resetZoneMode();
1458 if (play && m_mltProducer->get_speed() == 0.0) {
1459 if (m_name == Kdenlive::clipMonitor && m_mltConsumer->position() == m_mltProducer->get_out()) m_mltProducer->seek(0);
1460 if (m_mltConsumer->is_stopped()) {
1461 m_mltConsumer->start();
1463 m_mltProducer->set_speed(1.0);
1464 m_mltConsumer->set("refresh", 1);
1466 m_mltProducer->set_speed(0.0);
1467 m_mltConsumer->set("refresh", 0);
1468 m_mltProducer->seek(m_mltConsumer->position());
1469 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
1470 if (m_isZoneMode) resetZoneMode();
1472 //emitConsumerStopped();
1473 /*m_mltConsumer->set("refresh", 0);
1474 m_mltConsumer->stop();
1475 m_mltConsumer->purge();
1476 m_mltProducer->set_speed(0.0);
1477 //m_framePosition = m_mltProducer->position();
1478 m_mltProducer->seek(m_framePosition);
1479 emit rendererPosition(m_framePosition);*/
1483 void Render::play(double speed)
1487 // if (speed == 0.0) m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1488 m_mltProducer->set_speed(speed);
1489 /*if (speed == 0.0) {
1490 m_mltProducer->seek((int) m_framePosition + 1);
1491 m_mltConsumer->purge();
1496 void Render::play(const GenTime & startTime)
1498 if (!m_mltProducer || !m_mltConsumer)
1500 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1501 m_mltProducer->set_speed(1.0);
1502 m_mltConsumer->set("refresh", 1);
1505 void Render::loopZone(const GenTime & startTime, const GenTime & stopTime)
1507 if (!m_mltProducer || !m_mltConsumer)
1509 //m_mltProducer->set("eof", "loop");
1510 m_isLoopMode = true;
1511 m_loopStart = startTime;
1512 playZone(startTime, stopTime);
1515 void Render::playZone(const GenTime & startTime, const GenTime & stopTime)
1517 if (!m_mltProducer || !m_mltConsumer)
1519 if (!m_isZoneMode) m_originalOut = m_mltProducer->get_playtime() - 1;
1520 m_mltProducer->set("out", (int)(stopTime.frames(m_fps)));
1521 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1522 m_mltProducer->set_speed(1.0);
1523 m_mltConsumer->set("refresh", 1);
1524 if (m_mltConsumer->is_stopped()) m_mltConsumer->start();
1525 m_isZoneMode = true;
1528 void Render::resetZoneMode()
1530 if (!m_isZoneMode && !m_isLoopMode) return;
1531 m_mltProducer->set("out", m_originalOut);
1532 //m_mltProducer->set("eof", "pause");
1533 m_isZoneMode = false;
1534 m_isLoopMode = false;
1537 void Render::seekToFrame(int pos)
1542 m_mltProducer->seek(pos);
1543 if (m_mltProducer->get_speed() == 0) {
1548 void Render::seekToFrameDiff(int diff)
1553 m_mltProducer->seek(m_mltProducer->position() + diff);
1557 void Render::doRefresh()
1559 if (m_mltProducer && m_mltProducer->get_speed() == 0) m_refreshTimer.start();
1562 void Render::refresh()
1564 QMutexLocker locker(&m_mutex);
1567 if (m_mltConsumer) {
1568 if (m_mltConsumer->is_stopped()) m_mltConsumer->start();
1569 m_mltConsumer->purge();
1570 m_mltConsumer->set("refresh", 1);
1574 void Render::setDropFrames(bool show)
1576 QMutexLocker locker(&m_mutex);
1577 if (m_mltConsumer) {
1578 int dropFrames = KdenliveSettings::mltthreads();
1579 if (show == false) dropFrames = -dropFrames;
1580 m_mltConsumer->stop();
1582 m_mltConsumer->set("real_time", dropFrames);
1584 m_mltConsumer->set("play.real_time", dropFrames);
1586 if (m_mltConsumer->start() == -1) {
1587 kDebug(QtWarningMsg) << "ERROR, Cannot start monitor";
1593 double Render::playSpeed() const
1595 if (m_mltProducer) return m_mltProducer->get_speed();
1599 GenTime Render::seekPosition() const
1601 if (m_mltProducer) return GenTime((int) m_mltProducer->position(), m_fps);
1602 else return GenTime();
1605 int Render::seekFramePosition() const
1607 //if (m_mltProducer) return (int) m_mltProducer->position();
1608 if (m_mltConsumer) return (int) m_mltConsumer->position();
1612 void Render::emitFrameUpdated(Mlt::Frame& frame)
1614 mlt_image_format format = mlt_image_rgb24a;
1617 const uchar* image = frame.get_image(format, width, height);
1618 QImage qimage(width, height, QImage::Format_ARGB32_Premultiplied);
1619 memcpy(qimage.scanLine(0), image, width * height * 4);
1621 /*mlt_image_format format = mlt_image_rgb24;
1624 const uchar* image = frame.get_image(format, width, height);
1625 QImage qimage(width, height, QImage::Format_RGB888);
1626 memcpy(qimage.bits(), image, width * height * 3);*/
1627 emit frameUpdated(qimage.rgbSwapped());
1630 void Render::emitFrameNumber()
1632 if (m_mltConsumer) emit rendererPosition((int) m_mltConsumer->position());
1635 void Render::emitConsumerStopped()
1637 // This is used to know when the playing stopped
1638 if (m_mltProducer) {
1639 double pos = m_mltProducer->position();
1640 if (m_isLoopMode) play(m_loopStart);
1641 //else if (m_isZoneMode) resetZoneMode();
1642 emit rendererStopped((int) pos);
1646 void Render::exportFileToFirewire(QString /*srcFileName*/, int /*port*/, GenTime /*startTime*/, GenTime /*endTime*/)
1648 KMessageBox::sorry(0, i18n("Firewire is not enabled on your system.\n Please install Libiec61883 and recompile Kdenlive"));
1651 void Render::exportCurrentFrame(KUrl url, bool /*notify*/)
1653 if (!m_mltProducer) {
1654 KMessageBox::sorry(qApp->activeWindow(), i18n("There is no clip, cannot extract frame."));
1658 //int height = 1080;//KdenliveSettings::defaultheight();
1659 //int width = 1940; //KdenliveSettings::displaywidth();
1661 QPixmap pix; // = KThumb::getFrame(m_mltProducer, -1, width, height);
1663 QPixmap pix(width, height);
1664 Mlt::Filter m_convert(*m_mltProfile, "avcolour_space");
1665 m_convert.set("forced", mlt_image_rgb24a);
1666 m_mltProducer->attach(m_convert);
1667 Mlt::Frame * frame = m_mltProducer->get_frame();
1668 m_mltProducer->detach(m_convert);
1670 pix = frameThumbnail(frame, width, height);
1673 pix.save(url.path(), "PNG");
1674 //if (notify) QApplication::postEvent(qApp->activeWindow(), new UrlEvent(url, 10003));
1678 void Render::showFrame(Mlt::Frame& frame)
1680 emit rendererPosition((int) m_mltConsumer->position());
1681 mlt_image_format format = mlt_image_rgb24a;
1684 const uchar* image = frame.get_image(format, width, height);
1685 QImage qimage(width, height, QImage::Format_ARGB32_Premultiplied);
1686 memcpy(qimage.scanLine(0), image, width * height * 4);
1687 emit showImageSignal(qimage);
1688 if (analyseAudio) showAudio(frame);
1689 if (sendFrameForAnalysis && frame.get_frame()->convert_image) {
1690 emit frameUpdated(qimage.rgbSwapped());
1694 void Render::disablePreview(bool disable)
1696 if (m_mltConsumer) {
1697 m_mltConsumer->stop();
1698 m_mltConsumer->set("preview_off", (int) disable);
1699 m_mltConsumer->set("refresh", 0);
1700 m_mltConsumer->start();
1704 void Render::showAudio(Mlt::Frame& frame)
1706 if (!frame.is_valid() || frame.get_int("test_audio") != 0) {
1709 mlt_audio_format audio_format = mlt_audio_s16;
1711 int num_channels = 0;
1713 int16_t* data = (int16_t*)frame.get_audio(audio_format, freq, num_channels, samples);
1719 // Data format: [ c00 c10 c01 c11 c02 c12 c03 c13 ... c0{samples-1} c1{samples-1} for 2 channels.
1720 // So the vector is of size samples*channels.
1721 QVector<int16_t> sampleVector(samples*num_channels);
1722 memcpy(sampleVector.data(), data, samples*num_channels*sizeof(int16_t));
1725 emit audioSamplesSignal(sampleVector, freq, num_channels, samples);
1730 * MLT playlist direct manipulation.
1733 void Render::mltCheckLength(Mlt::Tractor *tractor)
1735 //kDebug()<<"checking track length: "<<track<<"..........";
1737 int trackNb = tractor->count();
1741 Mlt::Producer trackProducer(tractor->track(0));
1742 duration = trackProducer.get_playtime() - 1;
1743 m_mltProducer->set("out", duration);
1744 emit durationChanged(duration);
1747 while (trackNb > 1) {
1748 Mlt::Producer trackProducer(tractor->track(trackNb - 1));
1749 trackDuration = trackProducer.get_playtime() - 1;
1750 // kDebug() << " / / /DURATON FOR TRACK " << trackNb - 1 << " = " << trackDuration;
1751 if (trackDuration > duration) duration = trackDuration;
1755 Mlt::Producer blackTrackProducer(tractor->track(0));
1757 if (blackTrackProducer.get_playtime() - 1 != duration) {
1758 Mlt::Playlist blackTrackPlaylist((mlt_playlist) blackTrackProducer.get_service());
1759 Mlt::Producer *blackclip = blackTrackPlaylist.get_clip(0);
1760 if (blackclip && blackclip->is_blank()) {
1765 if (blackclip == NULL || blackTrackPlaylist.count() != 1) {
1766 if (blackclip) delete blackclip;
1767 blackTrackPlaylist.clear();
1768 m_blackClip->set("length", duration + 1);
1769 m_blackClip->set("out", duration);
1770 blackclip = m_blackClip->cut(0, duration);
1771 blackTrackPlaylist.insert_at(0, blackclip, 1);
1773 if (duration > blackclip->parent().get_length()) {
1774 blackclip->parent().set("length", duration + 1);
1775 blackclip->parent().set("out", duration);
1776 blackclip->set("length", duration + 1);
1778 blackTrackPlaylist.resize_clip(0, 0, duration);
1782 m_mltProducer->set("out", duration);
1783 emit durationChanged(duration);
1787 Mlt::Producer *Render::checkSlowMotionProducer(Mlt::Producer *prod, QDomElement element)
1789 if (element.attribute("speed", "1.0").toDouble() == 1.0 && element.attribute("strobe", "1").toInt() == 1) return prod;
1791 // We want a slowmotion producer
1792 double speed = element.attribute("speed", "1.0").toDouble();
1793 int strobe = element.attribute("strobe", "1").toInt();
1794 QString url = QString::fromUtf8(prod->get("resource"));
1795 url.append('?' + locale.toString(speed));
1796 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
1797 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1798 if (!slowprod || slowprod->get_producer() == NULL) {
1799 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
1800 if (strobe > 1) slowprod->set("strobe", strobe);
1801 QString id = prod->parent().get("id");
1802 if (id.contains('_')) id = id.section('_', 0, 0);
1803 QString producerid = "slowmotion:" + id + ':' + locale.toString(speed);
1804 if (strobe > 1) producerid.append(':' + QString::number(strobe));
1805 slowprod->set("id", producerid.toUtf8().constData());
1806 m_slowmotionProducers.insert(url, slowprod);
1811 int Render::mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod, bool overwrite, bool push)
1813 if (m_mltProducer == NULL) {
1814 kDebug() << "PLAYLIST NOT INITIALISED //////";
1818 kDebug() << "Cannot insert clip without producer //////";
1821 Mlt::Producer parentProd(m_mltProducer->parent());
1822 if (parentProd.get_producer() == NULL) {
1823 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1827 Mlt::Service service(parentProd.get_service());
1828 if (service.type() != tractor_type) {
1829 kWarning() << "// TRACTOR PROBLEM";
1832 Mlt::Tractor tractor(service);
1833 if (info.track > tractor.count() - 1) {
1834 kDebug() << "ERROR TRYING TO INSERT CLIP ON TRACK " << info.track << ", at POS: " << info.startPos.frames(25);
1838 Mlt::Producer trackProducer(tractor.track(info.track));
1839 int trackDuration = trackProducer.get_playtime() - 1;
1840 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1841 //kDebug()<<"/// INSERT cLIP: "<<info.cropStart.frames(m_fps)<<", "<<info.startPos.frames(m_fps)<<"-"<<info.endPos.frames(m_fps);
1842 prod = checkSlowMotionProducer(prod, element);
1843 if (prod == NULL || !prod->is_valid()) {
1848 int cutPos = (int) info.cropStart.frames(m_fps);
1849 if (cutPos < 0) cutPos = 0;
1850 int insertPos = (int) info.startPos.frames(m_fps);
1851 int cutDuration = (int)(info.endPos - info.startPos).frames(m_fps) - 1;
1852 Mlt::Producer *clip = prod->cut(cutPos, cutDuration + cutPos);
1853 if (overwrite && (insertPos < trackDuration)) {
1854 // Replace zone with blanks
1855 //trackPlaylist.split_at(insertPos, true);
1856 trackPlaylist.remove_region(insertPos, cutDuration + 1);
1857 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1858 trackPlaylist.insert_blank(clipIndex, cutDuration);
1860 trackPlaylist.split_at(insertPos, true);
1861 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1862 trackPlaylist.insert_blank(clipIndex, cutDuration);
1864 int newIndex = trackPlaylist.insert_at(insertPos, clip, 1);
1866 /*if (QString(prod->get("transparency")).toInt() == 1)
1867 mltAddClipTransparency(info, info.track - 1, QString(prod->get("id")).toInt());*/
1869 if (info.track != 0 && (newIndex + 1 == trackPlaylist.count())) mltCheckLength(&tractor);
1871 /*tractor.multitrack()->refresh();
1872 tractor.refresh();*/
1877 void Render::mltCutClip(int track, GenTime position)
1879 Mlt::Service service(m_mltProducer->parent().get_service());
1880 if (service.type() != tractor_type) {
1881 kWarning() << "// TRACTOR PROBLEM";
1885 Mlt::Tractor tractor(service);
1886 Mlt::Producer trackProducer(tractor.track(track));
1887 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1890 /* // Display playlist info
1891 kDebug()<<"//////////// BEFORE";
1892 for (int i = 0; i < trackPlaylist.count(); i++) {
1893 int blankStart = trackPlaylist.clip_start(i);
1894 int blankDuration = trackPlaylist.clip_length(i) - 1;
1896 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1897 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1900 int cutPos = (int) position.frames(m_fps);
1902 int clipIndex = trackPlaylist.get_clip_index_at(cutPos);
1903 if (trackPlaylist.is_blank(clipIndex)) {
1904 kDebug() << "// WARNING, TRYING TO CUT A BLANK";
1908 int clipStart = trackPlaylist.clip_start(clipIndex);
1909 trackPlaylist.split(clipIndex, cutPos - clipStart - 1);
1912 // duplicate effects
1913 Mlt::Producer *original = trackPlaylist.get_clip_at(clipStart);
1914 Mlt::Producer *clip = trackPlaylist.get_clip_at(cutPos);
1916 if (original == NULL || clip == NULL) {
1917 kDebug() << "// ERROR GRABBING CLIP AFTER SPLIT";
1919 Mlt::Service clipService(original->get_service());
1920 Mlt::Service dupService(clip->get_service());
1924 Mlt::Filter *filter = clipService.filter(ct);
1926 // Only duplicate Kdenlive filters, and skip the fade in effects
1927 if (filter->is_valid() && strcmp(filter->get("kdenlive_id"), "") && strcmp(filter->get("kdenlive_id"), "fadein") && strcmp(filter->get("kdenlive_id"), "fade_from_black")) {
1928 // looks like there is no easy way to duplicate a filter,
1929 // so we will create a new one and duplicate its properties
1930 Mlt::Filter *dup = new Mlt::Filter(*m_mltProfile, filter->get("mlt_service"));
1931 if (dup && dup->is_valid()) {
1932 Mlt::Properties entries(filter->get_properties());
1933 for (int i = 0; i < entries.count(); i++) {
1934 dup->set(entries.get_name(i), entries.get(i));
1936 dupService.attach(*dup);
1940 filter = clipService.filter(ct);
1943 /* // Display playlist info
1944 kDebug()<<"//////////// AFTER";
1945 for (int i = 0; i < trackPlaylist.count(); i++) {
1946 int blankStart = trackPlaylist.clip_start(i);
1947 int blankDuration = trackPlaylist.clip_length(i) - 1;
1949 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1950 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1955 Mlt::Tractor *Render::lockService()
1957 // we are going to replace some clips, purge consumer
1958 QMutexLocker locker(&m_mutex);
1959 if (!m_mltProducer) return NULL;
1960 if (m_mltConsumer) {
1961 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
1962 m_mltConsumer->purge();
1964 Mlt::Service service(m_mltProducer->parent().get_service());
1965 if (service.type() != tractor_type) {
1966 kWarning() << "// TRACTOR PROBLEM";
1970 return new Mlt::Tractor(service);
1974 void Render::unlockService(Mlt::Tractor *tractor)
1979 if (!m_mltProducer) return;
1980 Mlt::Service service(m_mltProducer->parent().get_service());
1981 if (service.type() != tractor_type) {
1982 kWarning() << "// TRACTOR PROBLEM";
1988 bool Render::mltUpdateClip(Mlt::Tractor *tractor, ItemInfo info, QDomElement element, Mlt::Producer *prod)
1991 if (prod == NULL || tractor == NULL) {
1992 kDebug() << "Cannot update clip with null producer //////";
1996 Mlt::Producer trackProducer(tractor->track(tractor->count() - 1 - info.track));
1997 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1998 int startPos = info.startPos.frames(m_fps);
1999 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
2000 if (trackPlaylist.is_blank(clipIndex)) {
2001 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << startPos;
2004 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2006 QList <Mlt::Filter *> filtersList;
2007 Mlt::Service sourceService(clip->get_service());
2009 Mlt::Filter *filter = sourceService.filter(ct);
2011 if (filter->get_int("kdenlive_ix") != 0) {
2012 filtersList.append(filter);
2015 filter = sourceService.filter(ct);
2018 clip = trackPlaylist.replace_with_blank(clipIndex);
2020 prod = checkSlowMotionProducer(prod, element);
2021 if (prod == NULL || !prod->is_valid()) {
2025 Mlt::Producer *clip2 = prod->cut(info.cropStart.frames(m_fps), (info.cropDuration + info.cropStart).frames(m_fps) - 1);
2026 trackPlaylist.insert_at(info.startPos.frames(m_fps), clip2, 1);
2027 Mlt::Service destService(clip2->get_service());
2030 if (!filtersList.isEmpty()) {
2031 for (int i = 0; i < filtersList.count(); i++)
2032 destService.attach(*(filtersList.at(i)));
2038 bool Render::mltRemoveClip(int track, GenTime position)
2040 Mlt::Service service(m_mltProducer->parent().get_service());
2041 if (service.type() != tractor_type) {
2042 kWarning() << "// TRACTOR PROBLEM";
2046 Mlt::Tractor tractor(service);
2047 Mlt::Producer trackProducer(tractor.track(track));
2048 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2049 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2051 if (trackPlaylist.is_blank(clipIndex)) {
2052 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << position.frames(m_fps);
2056 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2057 if (clip) delete clip;
2058 trackPlaylist.consolidate_blanks(0);
2060 /* // Display playlist info
2061 kDebug()<<"//// AFTER";
2062 for (int i = 0; i < trackPlaylist.count(); i++) {
2063 int blankStart = trackPlaylist.clip_start(i);
2064 int blankDuration = trackPlaylist.clip_length(i) - 1;
2066 if (trackPlaylist.is_blank(i)) blk = "(blank)";
2067 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
2070 if (track != 0 && trackPlaylist.count() <= clipIndex) mltCheckLength(&tractor);
2074 int Render::mltGetSpaceLength(const GenTime &pos, int track, bool fromBlankStart)
2076 if (!m_mltProducer) {
2077 kDebug() << "PLAYLIST NOT INITIALISED //////";
2080 Mlt::Producer parentProd(m_mltProducer->parent());
2081 if (parentProd.get_producer() == NULL) {
2082 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
2086 Mlt::Service service(parentProd.get_service());
2087 Mlt::Tractor tractor(service);
2088 int insertPos = pos.frames(m_fps);
2090 Mlt::Producer trackProducer(tractor.track(track));
2091 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2092 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
2093 if (clipIndex == trackPlaylist.count()) {
2094 // We are after the end of the playlist
2097 if (!trackPlaylist.is_blank(clipIndex)) return 0;
2098 if (fromBlankStart) return trackPlaylist.clip_length(clipIndex);
2099 return trackPlaylist.clip_length(clipIndex) + trackPlaylist.clip_start(clipIndex) - insertPos;
2102 int Render::mltTrackDuration(int track)
2104 if (!m_mltProducer) {
2105 kDebug() << "PLAYLIST NOT INITIALISED //////";
2108 Mlt::Producer parentProd(m_mltProducer->parent());
2109 if (parentProd.get_producer() == NULL) {
2110 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
2114 Mlt::Service service(parentProd.get_service());
2115 Mlt::Tractor tractor(service);
2117 Mlt::Producer trackProducer(tractor.track(track));
2118 return trackProducer.get_playtime() - 1;
2121 void Render::mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime &duration, const GenTime &timeOffset)
2123 if (!m_mltProducer) {
2124 kDebug() << "PLAYLIST NOT INITIALISED //////";
2127 Mlt::Producer parentProd(m_mltProducer->parent());
2128 if (parentProd.get_producer() == NULL) {
2129 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
2132 //kDebug()<<"// CLP STRT LST: "<<trackClipStartList;
2133 //kDebug()<<"// TRA STRT LST: "<<trackTransitionStartList;
2135 Mlt::Service service(parentProd.get_service());
2136 Mlt::Tractor tractor(service);
2138 int diff = duration.frames(m_fps);
2139 int offset = timeOffset.frames(m_fps);
2143 // insert space in one track only
2144 Mlt::Producer trackProducer(tractor.track(track));
2145 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2146 insertPos = trackClipStartList.value(track);
2147 if (insertPos != -1) {
2148 insertPos += offset;
2149 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
2151 trackPlaylist.insert_blank(clipIndex, diff - 1);
2153 if (!trackPlaylist.is_blank(clipIndex)) clipIndex --;
2154 if (!trackPlaylist.is_blank(clipIndex)) {
2155 kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
2157 int position = trackPlaylist.clip_start(clipIndex);
2158 int blankDuration = trackPlaylist.clip_length(clipIndex);
2159 if (blankDuration + diff == 0) {
2160 trackPlaylist.remove(clipIndex);
2161 } else trackPlaylist.remove_region(position, -diff);
2163 trackPlaylist.consolidate_blanks(0);
2165 // now move transitions
2166 mlt_service serv = m_mltProducer->parent().get_service();
2167 mlt_service nextservice = mlt_service_get_producer(serv);
2168 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2169 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2170 QString resource = mlt_properties_get(properties, "mlt_service");
2172 while (mlt_type == "transition") {
2173 mlt_transition tr = (mlt_transition) nextservice;
2174 int currentTrack = mlt_transition_get_b_track(tr);
2175 int currentIn = (int) mlt_transition_get_in(tr);
2176 int currentOut = (int) mlt_transition_get_out(tr);
2177 insertPos = trackTransitionStartList.value(track);
2178 if (insertPos != -1) {
2179 insertPos += offset;
2180 if (track == currentTrack && currentOut > insertPos && resource != "mix") {
2181 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
2184 nextservice = mlt_service_producer(nextservice);
2185 if (nextservice == NULL) break;
2186 properties = MLT_SERVICE_PROPERTIES(nextservice);
2187 mlt_type = mlt_properties_get(properties, "mlt_type");
2188 resource = mlt_properties_get(properties, "mlt_service");
2191 for (int trackNb = tractor.count() - 1; trackNb >= 1; --trackNb) {
2192 Mlt::Producer trackProducer(tractor.track(trackNb));
2193 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2195 //int clipNb = trackPlaylist.count();
2196 insertPos = trackClipStartList.value(trackNb);
2197 if (insertPos != -1) {
2198 insertPos += offset;
2200 /* kDebug()<<"-------------\nTRACK "<<trackNb<<" HAS "<<clipNb<<" CLPIS";
2201 kDebug() << "INSERT SPACE AT: "<<insertPos<<", DIFF: "<<diff<<", TK: "<<trackNb;
2202 for (int i = 0; i < clipNb; i++) {
2203 kDebug()<<"CLIP "<<i<<", START: "<<trackPlaylist.clip_start(i)<<", END: "<<trackPlaylist.clip_start(i) + trackPlaylist.clip_length(i);
2204 if (trackPlaylist.is_blank(i)) kDebug()<<"++ BLANK ++ ";
2205 kDebug()<<"-------------";
2207 kDebug()<<"END-------------";*/
2210 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
2212 trackPlaylist.insert_blank(clipIndex, diff - 1);
2214 if (!trackPlaylist.is_blank(clipIndex)) {
2217 if (!trackPlaylist.is_blank(clipIndex)) {
2218 kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
2220 int position = trackPlaylist.clip_start(clipIndex);
2221 int blankDuration = trackPlaylist.clip_length(clipIndex);
2222 if (diff + blankDuration == 0) {
2223 trackPlaylist.remove(clipIndex);
2224 } else trackPlaylist.remove_region(position, - diff);
2226 trackPlaylist.consolidate_blanks(0);
2229 // now move transitions
2230 mlt_service serv = m_mltProducer->parent().get_service();
2231 mlt_service nextservice = mlt_service_get_producer(serv);
2232 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2233 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2234 QString resource = mlt_properties_get(properties, "mlt_service");
2236 while (mlt_type == "transition") {
2237 mlt_transition tr = (mlt_transition) nextservice;
2238 int currentIn = (int) mlt_transition_get_in(tr);
2239 int currentOut = (int) mlt_transition_get_out(tr);
2240 int currentTrack = mlt_transition_get_b_track(tr);
2241 insertPos = trackTransitionStartList.value(currentTrack);
2242 if (insertPos != -1) {
2243 insertPos += offset;
2244 if (currentOut > insertPos && resource != "mix") {
2245 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
2248 nextservice = mlt_service_producer(nextservice);
2249 if (nextservice == NULL) break;
2250 properties = MLT_SERVICE_PROPERTIES(nextservice);
2251 mlt_type = mlt_properties_get(properties, "mlt_type");
2252 resource = mlt_properties_get(properties, "mlt_service");
2256 mltCheckLength(&tractor);
2257 m_mltConsumer->set("refresh", 1);
2261 void Render::mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest)
2263 if (source == dest) return;
2264 Mlt::Service sourceService(source->get_service());
2265 Mlt::Service destService(dest->get_service());
2267 // move all effects to the correct producer
2269 Mlt::Filter *filter = sourceService.filter(ct);
2271 if (filter->get_int("kdenlive_ix") != 0) {
2272 sourceService.detach(*filter);
2273 destService.attach(*filter);
2275 filter = sourceService.filter(ct);
2279 int Render::mltChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, double speed, double /*oldspeed*/, int strobe, Mlt::Producer *prod)
2282 Mlt::Service service(m_mltProducer->parent().get_service());
2283 if (service.type() != tractor_type) {
2284 kWarning() << "// TRACTOR PROBLEM";
2288 //kDebug() << "Changing clip speed, set in and out: " << info.cropStart.frames(m_fps) << " to " << (info.endPos - info.startPos).frames(m_fps) - 1;
2289 Mlt::Tractor tractor(service);
2290 Mlt::Producer trackProducer(tractor.track(info.track));
2291 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2292 int startPos = info.startPos.frames(m_fps);
2293 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
2294 int clipLength = trackPlaylist.clip_length(clipIndex);
2296 Mlt::Producer *original = trackPlaylist.get_clip(clipIndex);
2297 if (original == NULL) {
2300 if (!original->is_valid() || original->is_blank()) {
2305 Mlt::Producer clipparent = original->parent();
2306 if (!clipparent.is_valid() || clipparent.is_blank()) {
2312 QString serv = clipparent.get("mlt_service");
2313 QString id = clipparent.get("id");
2314 if (speed <= 0 && speed > -1) speed = 1.0;
2315 //kDebug() << "CLIP SERVICE: " << serv;
2316 if ((serv == "avformat" || serv == "avformat-novalidate") && (speed != 1.0 || strobe > 1)) {
2318 QString url = QString::fromUtf8(clipparent.get("resource"));
2319 url.append('?' + m_locale.toString(speed));
2320 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
2321 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
2322 if (!slowprod || slowprod->get_producer() == NULL) {
2323 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
2324 if (strobe > 1) slowprod->set("strobe", strobe);
2325 QString producerid = "slowmotion:" + id + ':' + m_locale.toString(speed);
2326 if (strobe > 1) producerid.append(':' + QString::number(strobe));
2327 slowprod->set("id", producerid.toUtf8().constData());
2328 // copy producer props
2329 double ar = original->parent().get_double("force_aspect_ratio");
2330 if (ar != 0.0) slowprod->set("force_aspect_ratio", ar);
2331 double fps = original->parent().get_double("force_fps");
2332 if (fps != 0.0) slowprod->set("force_fps", fps);
2333 int threads = original->parent().get_int("threads");
2334 if (threads != 0) slowprod->set("threads", threads);
2335 if (original->parent().get("force_progressive"))
2336 slowprod->set("force_progressive", original->parent().get_int("force_progressive"));
2337 if (original->parent().get("force_tff"))
2338 slowprod->set("force_tff", original->parent().get_int("force_tff"));
2339 int ix = original->parent().get_int("video_index");
2340 if (ix != 0) slowprod->set("video_index", ix);
2341 int colorspace = original->parent().get_int("force_colorspace");
2342 if (colorspace != 0) slowprod->set("force_colorspace", colorspace);
2343 int full_luma = original->parent().get_int("set.force_full_luma");
2344 if (full_luma != 0) slowprod->set("set.force_full_luma", full_luma);
2345 m_slowmotionProducers.insert(url, slowprod);
2347 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2348 trackPlaylist.consolidate_blanks(0);
2350 // Check that the blank space is long enough for our new duration
2351 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2352 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2354 if (clipIndex + 1 < trackPlaylist.count() && (startPos + clipLength / speed > blankEnd)) {
2355 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2356 cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)(info.cropStart.frames(m_fps) / speed + maxLength.frames(m_fps) - 1));
2357 } else cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)((info.cropStart.frames(m_fps) + clipLength) / speed - 1));
2359 // move all effects to the correct producer
2360 mltPasteEffects(clip, cut);
2361 trackPlaylist.insert_at(startPos, cut, 1);
2364 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2365 newLength = trackPlaylist.clip_length(clipIndex);
2367 } else if (speed == 1.0 && strobe < 2) {
2370 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2371 trackPlaylist.consolidate_blanks(0);
2373 // Check that the blank space is long enough for our new duration
2374 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2375 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2378 int originalStart = (int)(speedIndependantInfo.cropStart.frames(m_fps));
2379 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + speedIndependantInfo.cropDuration).frames(m_fps) > blankEnd) {
2380 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2381 cut = prod->cut(originalStart, (int)(originalStart + maxLength.frames(m_fps) - 1));
2382 } else cut = prod->cut(originalStart, (int)(originalStart + speedIndependantInfo.cropDuration.frames(m_fps)) - 1);
2384 // move all effects to the correct producer
2385 mltPasteEffects(clip, cut);
2387 trackPlaylist.insert_at(startPos, cut, 1);
2390 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2391 newLength = trackPlaylist.clip_length(clipIndex);
2394 } else if (serv == "framebuffer") {
2396 QString url = QString::fromUtf8(clipparent.get("resource"));
2397 url = url.section('?', 0, 0);
2398 url.append('?' + m_locale.toString(speed));
2399 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
2400 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
2401 if (!slowprod || slowprod->get_producer() == NULL) {
2402 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
2403 slowprod->set("strobe", strobe);
2404 QString producerid = "slowmotion:" + id.section(':', 1, 1) + ':' + m_locale.toString(speed);
2405 if (strobe > 1) producerid.append(':' + QString::number(strobe));
2406 slowprod->set("id", producerid.toUtf8().constData());
2407 // copy producer props
2408 double ar = original->parent().get_double("force_aspect_ratio");
2409 if (ar != 0.0) slowprod->set("force_aspect_ratio", ar);
2410 double fps = original->parent().get_double("force_fps");
2411 if (fps != 0.0) slowprod->set("force_fps", fps);
2412 if (original->parent().get("force_progressive"))
2413 slowprod->set("force_progressive", original->parent().get_int("force_progressive"));
2414 if (original->parent().get("force_tff"))
2415 slowprod->set("force_tff", original->parent().get_int("force_tff"));
2416 int threads = original->parent().get_int("threads");
2417 if (threads != 0) slowprod->set("threads", threads);
2418 int ix = original->parent().get_int("video_index");
2419 if (ix != 0) slowprod->set("video_index", ix);
2420 int colorspace = original->parent().get_int("force_colorspace");
2421 if (colorspace != 0) slowprod->set("force_colorspace", colorspace);
2422 int full_luma = original->parent().get_int("set.force_full_luma");
2423 if (full_luma != 0) slowprod->set("set.force_full_luma", full_luma);
2424 m_slowmotionProducers.insert(url, slowprod);
2426 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2427 trackPlaylist.consolidate_blanks(0);
2429 GenTime duration = speedIndependantInfo.cropDuration / speed;
2430 int originalStart = (int)(speedIndependantInfo.cropStart.frames(m_fps) / speed);
2432 // Check that the blank space is long enough for our new duration
2433 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2434 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2437 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + duration).frames(m_fps) > blankEnd) {
2438 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2439 cut = slowprod->cut(originalStart, (int)(originalStart + maxLength.frames(m_fps) - 1));
2440 } else cut = slowprod->cut(originalStart, (int)(originalStart + duration.frames(m_fps)) - 1);
2442 // move all effects to the correct producer
2443 mltPasteEffects(clip, cut);
2445 trackPlaylist.insert_at(startPos, cut, 1);
2448 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2449 newLength = trackPlaylist.clip_length(clipIndex);
2454 if (clipIndex + 1 == trackPlaylist.count()) mltCheckLength(&tractor);
2458 bool Render::mltRemoveTrackEffect(int track, int index, bool updateIndex)
2460 Mlt::Service service(m_mltProducer->parent().get_service());
2461 bool success = false;
2462 Mlt::Tractor tractor(service);
2463 Mlt::Producer trackProducer(tractor.track(track));
2464 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2465 Mlt::Service clipService(trackPlaylist.get_service());
2469 Mlt::Filter *filter = clipService.filter(ct);
2471 if ((index == -1 && strcmp(filter->get("kdenlive_id"), "")) || filter->get_int("kdenlive_ix") == index) {
2472 if (clipService.detach(*filter) == 0) success = true;
2473 } else if (updateIndex) {
2474 // Adjust the other effects index
2475 if (filter->get_int("kdenlive_ix") > index) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2478 filter = clipService.filter(ct);
2485 bool Render::mltRemoveEffect(int track, GenTime position, int index, bool updateIndex, bool doRefresh)
2487 if (position < GenTime()) {
2488 // Remove track effect
2489 return mltRemoveTrackEffect(track, index, updateIndex);
2491 Mlt::Service service(m_mltProducer->parent().get_service());
2492 bool success = false;
2493 Mlt::Tractor tractor(service);
2494 Mlt::Producer trackProducer(tractor.track(track));
2495 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2497 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2498 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2500 kDebug() << " / / / CANNOT FIND CLIP TO REMOVE EFFECT";
2504 Mlt::Service clipService(clip->get_service());
2505 int duration = clip->get_playtime();
2507 // Check if clip is visible in monitor
2508 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2509 if (diff < 0 || diff > duration) doRefresh = false;
2515 Mlt::Filter *filter = clipService.filter(ct);
2517 if ((index == -1 && strcmp(filter->get("kdenlive_id"), "")) || filter->get_int("kdenlive_ix") == index) {// && filter->get("kdenlive_id") == id) {
2518 if (clipService.detach(*filter) == 0) success = true;
2519 //kDebug()<<"Deleted filter id:"<<filter->get("kdenlive_id")<<", ix:"<<filter->get("kdenlive_ix")<<", SERVICE:"<<filter->get("mlt_service");
2520 } else if (updateIndex) {
2521 // Adjust the other effects index
2522 if (filter->get_int("kdenlive_ix") > index) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2525 filter = clipService.filter(ct);
2528 if (doRefresh) refresh();
2532 bool Render::mltAddTrackEffect(int track, EffectsParameterList params)
2534 Mlt::Service service(m_mltProducer->parent().get_service());
2535 Mlt::Tractor tractor(service);
2536 Mlt::Producer trackProducer(tractor.track(track));
2537 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2538 Mlt::Service trackService(trackProducer.get_service()); //trackPlaylist
2539 return mltAddEffect(trackService, params, trackProducer.get_playtime() - 1, true);
2543 bool Render::mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh)
2546 Mlt::Service service(m_mltProducer->parent().get_service());
2548 Mlt::Tractor tractor(service);
2549 Mlt::Producer trackProducer(tractor.track(track));
2550 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2552 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2553 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2558 Mlt::Service clipService(clip->get_service());
2559 int duration = clip->get_playtime();
2561 // Check if clip is visible in monitor
2562 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2563 if (diff < 0 || diff > duration) doRefresh = false;
2566 return mltAddEffect(clipService, params, duration, doRefresh);
2569 bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int duration, bool doRefresh)
2571 bool updateIndex = false;
2572 const int filter_ix = params.paramValue("kdenlive_ix").toInt();
2576 Mlt::Filter *filter = service.filter(ct);
2578 if (filter->get_int("kdenlive_ix") == filter_ix) {
2579 // A filter at that position already existed, so we will increase all indexes later
2584 filter = service.filter(ct);
2587 if (params.paramValue("id") == "speed") {
2588 // special case, speed effect is not really inserted, we just update the other effects index (kdenlive_ix)
2590 filter = service.filter(ct);
2592 if (filter->get_int("kdenlive_ix") >= filter_ix) {
2593 if (updateIndex) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") + 1);
2596 filter = service.filter(ct);
2599 if (doRefresh) refresh();
2604 // temporarily remove all effects after insert point
2605 QList <Mlt::Filter *> filtersList;
2607 filter = service.filter(ct);
2609 if (filter->get_int("kdenlive_ix") >= filter_ix) {
2610 filtersList.append(filter);
2611 service.detach(*filter);
2613 filter = service.filter(ct);
2616 addFilterToService(service, params, duration);
2618 // re-add following filters
2619 for (int i = 0; i < filtersList.count(); i++) {
2620 Mlt::Filter *filter = filtersList.at(i);
2622 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") + 1);
2623 service.attach(*filter);
2626 if (doRefresh) refresh();
2631 bool Render::addFilterToService(Mlt::Service service, EffectsParameterList params, int duration)
2634 QString tag = params.paramValue("tag");
2635 //kDebug() << " / / INSERTING EFFECT: " << tag << ", REGI: " << region;
2636 char *filterTag = qstrdup(tag.toUtf8().constData());
2637 char *filterId = qstrdup(params.paramValue("id").toUtf8().constData());
2638 QString kfr = params.paramValue("keyframes");
2639 if (!kfr.isEmpty()) {
2640 QStringList keyFrames = kfr.split(';', QString::SkipEmptyParts);
2641 //kDebug() << "// ADDING KEYFRAME EFFECT: " << params.paramValue("keyframes");
2642 char *starttag = qstrdup(params.paramValue("starttag", "start").toUtf8().constData());
2643 char *endtag = qstrdup(params.paramValue("endtag", "end").toUtf8().constData());
2644 //kDebug() << "// ADDING KEYFRAME TAGS: " << starttag << ", " << endtag;
2645 //double max = params.paramValue("max").toDouble();
2646 double min = params.paramValue("min").toDouble();
2647 double factor = params.paramValue("factor", "1").toDouble();
2648 double paramOffset = params.paramValue("offset", "0").toDouble();
2649 params.removeParam("starttag");
2650 params.removeParam("endtag");
2651 params.removeParam("keyframes");
2652 params.removeParam("min");
2653 params.removeParam("max");
2654 params.removeParam("factor");
2655 params.removeParam("offset");
2657 // Special case, only one keyframe, means we want a constant value
2658 if (keyFrames.count() == 1) {
2659 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2660 if (filter && filter->is_valid()) {
2661 filter->set("kdenlive_id", filterId);
2662 int x1 = keyFrames.at(0).section(':', 0, 0).toInt();
2663 double y1 = keyFrames.at(0).section(':', 1, 1).toDouble();
2664 for (int j = 0; j < params.count(); j++) {
2665 filter->set(params.at(j).name().toUtf8().constData(), params.at(j).value().toUtf8().constData());
2667 filter->set("in", x1);
2668 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2669 filter->set(starttag, m_locale.toString(((min + y1) - paramOffset) / factor).toUtf8().data());
2670 service.attach(*filter);
2672 } else for (int i = 0; i < keyFrames.size() - 1; ++i) {
2673 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2674 if (filter && filter->is_valid()) {
2675 filter->set("kdenlive_id", filterId);
2676 int x1 = keyFrames.at(i).section(':', 0, 0).toInt() + offset;
2677 double y1 = keyFrames.at(i).section(':', 1, 1).toDouble();
2678 int x2 = keyFrames.at(i + 1).section(':', 0, 0).toInt();
2679 double y2 = keyFrames.at(i + 1).section(':', 1, 1).toDouble();
2680 if (x2 == -1) x2 = duration;
2682 for (int j = 0; j < params.count(); j++) {
2683 filter->set(params.at(j).name().toUtf8().constData(), params.at(j).value().toUtf8().constData());
2686 filter->set("in", x1);
2687 filter->set("out", x2);
2688 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2689 filter->set(starttag, m_locale.toString(((min + y1) - paramOffset) / factor).toUtf8().data());
2690 filter->set(endtag, m_locale.toString(((min + y2) - paramOffset) / factor).toUtf8().data());
2691 service.attach(*filter);
2698 Mlt::Filter *filter;
2700 filter = new Mlt::Filter(*m_mltProfile, filterTag);
2701 if (filter && filter->is_valid()) {
2702 filter->set("kdenlive_id", filterId);
2704 kDebug() << "filter is NULL";
2708 params.removeParam("kdenlive_id");
2709 if (params.hasParam("_sync_in_out")) {
2710 // This effect must sync in / out with parent clip
2711 params.removeParam("_sync_in_out");
2712 filter->set_in_and_out(service.get_int("in"), service.get_int("out"));
2715 for (int j = 0; j < params.count(); j++) {
2716 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2720 QString effectArgs = params.paramValue("id").section('_', 1);
2722 params.removeParam("id");
2723 params.removeParam("kdenlive_ix");
2724 params.removeParam("tag");
2725 params.removeParam("disable");
2726 params.removeParam("region");
2728 for (int j = 0; j < params.count(); j++) {
2729 effectArgs.append(' ' + params.at(j).value());
2731 //kDebug() << "SOX EFFECTS: " << effectArgs.simplified();
2732 filter->set("effect", effectArgs.simplified().toUtf8().constData());
2734 // attach filter to the clip
2735 service.attach(*filter);
2743 bool Render::mltEditTrackEffect(int track, EffectsParameterList params)
2745 Mlt::Service service(m_mltProducer->parent().get_service());
2746 Mlt::Tractor tractor(service);
2747 Mlt::Producer trackProducer(tractor.track(track));
2748 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2749 Mlt::Service clipService(trackPlaylist.get_service());
2751 QString index = params.paramValue("kdenlive_ix");
2752 QString tag = params.paramValue("tag");
2754 Mlt::Filter *filter = clipService.filter(ct);
2756 if (filter->get_int("kdenlive_ix") == index.toInt()) {
2760 filter = clipService.filter(ct);
2764 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT! " << index << ", " << tag;
2765 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2767 bool success = false;//mltAddTrackEffect(track, params);
2771 QString ser = filter->get("mlt_service");
2772 if (ser == "region") prefix = "filter0.";
2774 for (int j = 0; j < params.count(); j++) {
2775 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2783 bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList params)
2785 int index = params.paramValue("kdenlive_ix").toInt();
2786 QString tag = params.paramValue("tag");
2788 if (!params.paramValue("keyframes").isEmpty() || (tag == "affine" && params.hasParam("background")) || tag.startsWith("ladspa") || tag == "sox" || tag == "autotrack_rectangle") {
2789 // This is a keyframe effect, to edit it, we remove it and re-add it.
2790 bool success = mltRemoveEffect(track, position, index, false);
2791 // if (!success) kDebug() << "// ERROR Removing effect : " << index;
2792 if (position < GenTime())
2793 success = mltAddTrackEffect(track, params);
2795 success = mltAddEffect(track, position, params);
2796 // if (!success) kDebug() << "// ERROR Adding effect : " << index;
2799 if (position < GenTime()) {
2800 return mltEditTrackEffect(track, params);
2803 Mlt::Service service(m_mltProducer->parent().get_service());
2804 Mlt::Tractor tractor(service);
2805 Mlt::Producer trackProducer(tractor.track(track));
2806 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2808 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2809 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2811 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2815 int duration = clip->get_playtime();
2816 bool doRefresh = true;
2817 // Check if clip is visible in monitor
2818 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2819 if (diff < 0 || diff > duration)
2823 Mlt::Filter *filter = clip->filter(ct);
2825 if (filter->get_int("kdenlive_ix") == index) {
2829 filter = clip->filter(ct);
2833 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT! " << index << ", " << tag;
2834 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2836 bool success = mltAddEffect(track, position, params);
2840 QString ser = filter->get("mlt_service");
2841 QList <Mlt::Filter *> filtersList;
2844 // Effect service changes, delete effect and re-add it
2845 clip->detach(*filter);
2847 // Delete all effects after deleted one
2848 filter = clip->filter(ct);
2850 if (filter->get_int("kdenlive_ix") > index) {
2851 filtersList.append(filter);
2852 clip->detach(*filter);
2855 filter = clip->filter(ct);
2859 addFilterToService(*clip, params, clip->get_playtime());
2863 if (doRefresh) refresh();
2866 if (params.hasParam("_sync_in_out")) {
2867 // This effect must sync in / out with parent clip
2868 params.removeParam("_sync_in_out");
2869 filter->set_in_and_out(clip->get_in(), clip->get_out());
2872 for (int j = 0; j < params.count(); j++) {
2873 filter->set((params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2876 for (int j = 0; j < filtersList.count(); j++) {
2877 clip->attach(*(filtersList.at(j)));
2883 if (doRefresh) refresh();
2887 bool Render::mltEnableEffects(int track, GenTime position, QList <int> effectIndexes, bool disable)
2889 if (position < GenTime()) {
2890 return mltEnableTrackEffects(track, effectIndexes, disable);
2893 Mlt::Service service(m_mltProducer->parent().get_service());
2894 Mlt::Tractor tractor(service);
2895 Mlt::Producer trackProducer(tractor.track(track));
2896 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2898 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2899 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2901 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2905 int duration = clip->get_playtime();
2906 bool doRefresh = true;
2907 // Check if clip is visible in monitor
2908 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2909 if (diff < 0 || diff > duration)
2913 Mlt::Filter *filter = clip->filter(ct);
2915 if (effectIndexes.contains(filter->get_int("kdenlive_ix"))) {
2916 filter->set("disable", (int) disable);
2919 filter = clip->filter(ct);
2925 if (doRefresh) refresh();
2929 bool Render::mltEnableTrackEffects(int track, QList <int> effectIndexes, bool disable)
2931 Mlt::Service service(m_mltProducer->parent().get_service());
2932 Mlt::Tractor tractor(service);
2933 Mlt::Producer trackProducer(tractor.track(track));
2934 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2935 Mlt::Service clipService(trackPlaylist.get_service());
2938 Mlt::Filter *filter = clipService.filter(ct);
2940 if (effectIndexes.contains(filter->get_int("kdenlive_ix"))) {
2941 filter->set("disable", (int) disable);
2944 filter = clipService.filter(ct);
2952 void Render::mltUpdateEffectPosition(int track, GenTime position, int oldPos, int newPos)
2954 Mlt::Service service(m_mltProducer->parent().get_service());
2955 Mlt::Tractor tractor(service);
2956 Mlt::Producer trackProducer(tractor.track(track));
2957 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2959 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2960 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2962 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2966 Mlt::Service clipService(clip->get_service());
2967 int duration = clip->get_playtime();
2968 bool doRefresh = true;
2969 // Check if clip is visible in monitor
2970 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2971 if (diff < 0 || diff > duration) doRefresh = false;
2975 Mlt::Filter *filter = clipService.filter(ct);
2977 int pos = filter->get_int("kdenlive_ix");
2978 if (pos == oldPos) {
2979 filter->set("kdenlive_ix", newPos);
2981 filter = clipService.filter(ct);
2983 if (doRefresh) refresh();
2986 void Render::mltMoveEffect(int track, GenTime position, int oldPos, int newPos)
2988 if (position < GenTime()) {
2989 mltMoveTrackEffect(track, oldPos, newPos);
2992 Mlt::Service service(m_mltProducer->parent().get_service());
2993 Mlt::Tractor tractor(service);
2994 Mlt::Producer trackProducer(tractor.track(track));
2995 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2997 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2998 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
3000 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
3004 Mlt::Service clipService(clip->get_service());
3005 int duration = clip->get_playtime();
3006 bool doRefresh = true;
3007 // Check if clip is visible in monitor
3008 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
3009 if (diff < 0 || diff > duration) doRefresh = false;
3013 QList <Mlt::Filter *> filtersList;
3014 Mlt::Filter *filter = clipService.filter(ct);
3016 if (newPos > oldPos) {
3018 if (!found && filter->get_int("kdenlive_ix") == oldPos) {
3019 filter->set("kdenlive_ix", newPos);
3020 filtersList.append(filter);
3021 clipService.detach(*filter);
3022 filter = clipService.filter(ct);
3023 while (filter && filter->get_int("kdenlive_ix") <= newPos) {
3024 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
3026 filter = clipService.filter(ct);
3030 if (filter && filter->get_int("kdenlive_ix") > newPos) {
3031 filtersList.append(filter);
3032 clipService.detach(*filter);
3034 filter = clipService.filter(ct);
3038 if (filter->get_int("kdenlive_ix") == oldPos) {
3039 filter->set("kdenlive_ix", newPos);
3040 filtersList.append(filter);
3041 clipService.detach(*filter);
3043 filter = clipService.filter(ct);
3047 filter = clipService.filter(ct);
3049 int pos = filter->get_int("kdenlive_ix");
3050 if (pos >= newPos) {
3051 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
3052 filtersList.append(filter);
3053 clipService.detach(*filter);
3055 filter = clipService.filter(ct);
3059 for (int i = 0; i < filtersList.count(); i++) {
3060 clipService.attach(*(filtersList.at(i)));
3063 if (doRefresh) refresh();
3066 void Render::mltMoveTrackEffect(int track, int oldPos, int newPos)
3068 Mlt::Service service(m_mltProducer->parent().get_service());
3069 Mlt::Tractor tractor(service);
3070 Mlt::Producer trackProducer(tractor.track(track));
3071 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3072 Mlt::Service clipService(trackPlaylist.get_service());
3074 QList <Mlt::Filter *> filtersList;
3075 Mlt::Filter *filter = clipService.filter(ct);
3077 if (newPos > oldPos) {
3079 if (!found && filter->get_int("kdenlive_ix") == oldPos) {
3080 filter->set("kdenlive_ix", newPos);
3081 filtersList.append(filter);
3082 clipService.detach(*filter);
3083 filter = clipService.filter(ct);
3084 while (filter && filter->get_int("kdenlive_ix") <= newPos) {
3085 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
3087 filter = clipService.filter(ct);
3091 if (filter && filter->get_int("kdenlive_ix") > newPos) {
3092 filtersList.append(filter);
3093 clipService.detach(*filter);
3095 filter = clipService.filter(ct);
3099 if (filter->get_int("kdenlive_ix") == oldPos) {
3100 filter->set("kdenlive_ix", newPos);
3101 filtersList.append(filter);
3102 clipService.detach(*filter);
3104 filter = clipService.filter(ct);
3108 filter = clipService.filter(ct);
3110 int pos = filter->get_int("kdenlive_ix");
3111 if (pos >= newPos) {
3112 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
3113 filtersList.append(filter);
3114 clipService.detach(*filter);
3116 filter = clipService.filter(ct);
3120 for (int i = 0; i < filtersList.count(); i++) {
3121 clipService.attach(*(filtersList.at(i)));
3126 bool Render::mltResizeClipEnd(ItemInfo info, GenTime clipDuration)
3128 Mlt::Service service(m_mltProducer->parent().get_service());
3129 Mlt::Tractor tractor(service);
3130 Mlt::Producer trackProducer(tractor.track(info.track));
3131 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3133 /* // Display playlist info
3134 kDebug()<<"//////////// BEFORE RESIZE";
3135 for (int i = 0; i < trackPlaylist.count(); i++) {
3136 int blankStart = trackPlaylist.clip_start(i);
3137 int blankDuration = trackPlaylist.clip_length(i) - 1;
3139 if (trackPlaylist.is_blank(i)) blk = "(blank)";
3140 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
3143 if (trackPlaylist.is_blank_at((int) info.startPos.frames(m_fps))) {
3144 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
3148 int clipIndex = trackPlaylist.get_clip_index_at((int) info.startPos.frames(m_fps));
3149 //kDebug() << "// SELECTED CLIP START: " << trackPlaylist.clip_start(clipIndex);
3150 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
3152 int previousStart = clip->get_in();
3153 int newDuration = (int) clipDuration.frames(m_fps) - 1;
3154 int diff = newDuration - (trackPlaylist.clip_length(clipIndex) - 1);
3156 int currentOut = newDuration + previousStart;
3157 if (currentOut > clip->get_length()) {
3158 clip->parent().set("length", currentOut + 1);
3159 clip->parent().set("out", currentOut);
3160 clip->set("length", currentOut + 1);
3163 /*if (newDuration > clip->get_out()) {
3164 clip->parent().set_in_and_out(0, newDuration + 1);
3165 clip->set_in_and_out(0, newDuration + 1);
3168 trackPlaylist.resize_clip(clipIndex, previousStart, newDuration + previousStart);
3169 trackPlaylist.consolidate_blanks(0);
3170 // skip to next clip
3172 //kDebug() << "//////// RESIZE CLIP: " << clipIndex << "( pos: " << info.startPos.frames(25) << "), DIFF: " << diff << ", CURRENT DUR: " << previousDuration << ", NEW DUR: " << newDuration << ", IX: " << clipIndex << ", MAX: " << trackPlaylist.count();
3174 // clip was made longer, trim next blank if there is one.
3175 if (clipIndex < trackPlaylist.count()) {
3176 // If this is not the last clip in playlist
3177 if (trackPlaylist.is_blank(clipIndex)) {
3178 int blankStart = trackPlaylist.clip_start(clipIndex);
3179 int blankDuration = trackPlaylist.clip_length(clipIndex);
3180 if (diff > blankDuration) {
3181 kDebug() << "// ERROR blank clip is not large enough to get back required space!!!";
3183 if (diff - blankDuration == 0) {
3184 trackPlaylist.remove(clipIndex);
3185 } else trackPlaylist.remove_region(blankStart, diff);
3187 kDebug() << "/// RESIZE ERROR, NXT CLIP IS NOT BLK: " << clipIndex;
3190 } else if (clipIndex != trackPlaylist.count()) trackPlaylist.insert_blank(clipIndex, 0 - diff - 1);
3191 trackPlaylist.consolidate_blanks(0);
3194 if (info.track != 0 && clipIndex == trackPlaylist.count()) mltCheckLength(&tractor);
3195 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
3196 //mltResizeTransparency(previousStart, previousStart, previousStart + newDuration, track, QString(clip->parent().get("id")).toInt());
3197 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
3198 ItemInfo transpinfo;
3199 transpinfo.startPos = info.startPos;
3200 transpinfo.endPos = info.startPos + clipDuration;
3201 transpinfo.track = info.track;
3202 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
3204 m_mltConsumer->set("refresh", 1);
3208 void Render::mltChangeTrackState(int track, bool mute, bool blind)
3210 Mlt::Service service(m_mltProducer->parent().get_service());
3211 Mlt::Tractor tractor(service);
3212 Mlt::Producer trackProducer(tractor.track(track));
3214 // Make sure muting will not produce problems with our audio mixing transition,
3215 // because audio mixing is done between each track and the lowest one
3216 bool audioMixingBroken = false;
3217 if (mute && trackProducer.get_int("hide") < 2 ) {
3218 // We mute a track with sound
3219 if (track == getLowestNonMutedAudioTrack(tractor)) audioMixingBroken = true;
3220 kDebug()<<"Muting track: "<<track <<" / "<<getLowestNonMutedAudioTrack(tractor);
3222 else if (!mute && trackProducer.get_int("hide") > 1 ) {
3223 // We un-mute a previously muted track
3224 if (track < getLowestNonMutedAudioTrack(tractor)) audioMixingBroken = true;
3228 if (blind) trackProducer.set("hide", 3);
3229 else trackProducer.set("hide", 2);
3231 trackProducer.set("hide", 1);
3233 trackProducer.set("hide", 0);
3235 if (audioMixingBroken) fixAudioMixing(tractor);
3237 tractor.multitrack()->refresh();
3242 int Render::getLowestNonMutedAudioTrack(Mlt::Tractor tractor)
3244 for (int i = 1; i < tractor.count(); i++) {
3245 Mlt::Producer trackProducer(tractor.track(i));
3246 if (trackProducer.get_int("hide") < 2) return i;
3248 return tractor.count() - 1;
3251 void Render::fixAudioMixing(Mlt::Tractor tractor)
3253 // Make sure the audio mixing transitions are applied to the lowest audible (non muted) track
3254 int lowestTrack = getLowestNonMutedAudioTrack(tractor);
3256 mlt_service serv = m_mltProducer->parent().get_service();
3257 Mlt::Field *field = tractor.field();
3258 mlt_service_lock(serv);
3260 mlt_service nextservice = mlt_service_get_producer(serv);
3261 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3262 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3263 QString resource = mlt_properties_get(properties, "mlt_service");
3265 mlt_service nextservicetodisconnect;
3266 // Delete all audio mixing transitions
3267 while (mlt_type == "transition") {
3268 if (resource == "mix") {
3269 nextservicetodisconnect = nextservice;
3270 nextservice = mlt_service_producer(nextservice);
3271 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
3273 else nextservice = mlt_service_producer(nextservice);
3274 if (nextservice == NULL) break;
3275 properties = MLT_SERVICE_PROPERTIES(nextservice);
3276 mlt_type = mlt_properties_get(properties, "mlt_type");
3277 resource = mlt_properties_get(properties, "mlt_service");
3280 // Re-add correct audio transitions
3281 for (int i = lowestTrack + 1; i < tractor.count(); i++) {
3282 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "mix");
3283 transition->set("always_active", 1);
3284 transition->set("combine", 1);
3285 transition->set("internal_added", 237);
3286 field->plant_transition(*transition, lowestTrack, i);
3288 mlt_service_unlock(serv);
3291 bool Render::mltResizeClipCrop(ItemInfo info, GenTime diff)
3293 Mlt::Service service(m_mltProducer->parent().get_service());
3294 int frameOffset = (int) diff.frames(m_fps);
3295 Mlt::Tractor tractor(service);
3296 Mlt::Producer trackProducer(tractor.track(info.track));
3297 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3298 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
3299 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
3303 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
3304 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
3306 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
3310 int previousStart = clip->get_in();
3311 int previousOut = clip->get_out();
3313 trackPlaylist.resize_clip(clipIndex, previousStart + frameOffset, previousOut + frameOffset);
3315 m_mltConsumer->set("refresh", 1);
3319 bool Render::mltResizeClipStart(ItemInfo info, GenTime diff)
3321 //kDebug() << "//////// RSIZING CLIP from: "<<info.startPos.frames(25)<<" to "<<diff.frames(25);
3322 Mlt::Service service(m_mltProducer->parent().get_service());
3323 int moveFrame = (int) diff.frames(m_fps);
3324 Mlt::Tractor tractor(service);
3325 Mlt::Producer trackProducer(tractor.track(info.track));
3326 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3327 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
3328 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
3332 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
3333 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
3334 if (clip == NULL || clip->is_blank()) {
3335 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
3339 int previousStart = clip->get_in();
3340 int previousOut = clip->get_out();
3342 previousStart += moveFrame;
3344 if (previousStart < 0) {
3345 // this is possible for images and color clips
3346 previousOut -= previousStart;
3350 int length = previousOut + 1;
3351 if (length > clip->get_length()) {
3352 clip->parent().set("length", length + 1);
3353 clip->parent().set("out", length);
3354 clip->set("length", length + 1);
3358 // kDebug() << "RESIZE, new start: " << previousStart << ", " << previousOut;
3359 trackPlaylist.resize_clip(clipIndex, previousStart, previousOut);
3360 if (moveFrame > 0) {
3361 trackPlaylist.insert_blank(clipIndex, moveFrame - 1);
3363 //int midpos = info.startPos.frames(m_fps) + moveFrame - 1;
3364 int blankIndex = clipIndex - 1;
3365 int blankLength = trackPlaylist.clip_length(blankIndex);
3366 // kDebug() << " + resizing blank length " << blankLength << ", SIZE DIFF: " << moveFrame;
3367 if (! trackPlaylist.is_blank(blankIndex)) {
3368 kDebug() << "WARNING, CLIP TO RESIZE IS NOT BLANK";
3370 if (blankLength + moveFrame == 0)
3371 trackPlaylist.remove(blankIndex);
3373 trackPlaylist.resize_clip(blankIndex, 0, blankLength + moveFrame - 1);
3375 trackPlaylist.consolidate_blanks(0);
3376 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
3377 //mltResizeTransparency(previousStart, (int) moveEnd.frames(m_fps), (int) (moveEnd + out - in).frames(m_fps), track, QString(clip->parent().get("id")).toInt());
3378 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
3379 ItemInfo transpinfo;
3380 transpinfo.startPos = info.startPos + diff;
3381 transpinfo.endPos = info.startPos + diff + (info.endPos - info.startPos);
3382 transpinfo.track = info.track;
3383 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
3385 //m_mltConsumer->set("refresh", 1);
3387 m_mltConsumer->set("refresh", 1);
3391 bool Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd, Mlt::Producer *prod, bool overwrite, bool insert)
3393 return mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps), prod, overwrite, insert);
3397 bool Render::mltUpdateClipProducer(Mlt::Tractor *tractor, int track, int pos, Mlt::Producer *prod)
3399 if (prod == NULL || !prod->is_valid() || tractor == NULL || !tractor->is_valid()) {
3400 kDebug() << "// Warning, CLIP on track " << track << ", at: " << pos << " is invalid, cannot update it!!!";
3404 Mlt::Producer trackProducer(tractor->track(track));
3405 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3406 int clipIndex = trackPlaylist.get_clip_index_at(pos);
3407 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3408 if (clipProducer == NULL || clipProducer->is_blank()) {
3409 kDebug() << "// ERROR UPDATING CLIP PROD";
3410 delete clipProducer;
3413 Mlt::Producer *clip = prod->cut(clipProducer->get_in(), clipProducer->get_out());
3414 if (!clip || !clip->is_valid()) {
3415 if (clip) delete clip;
3416 delete clipProducer;
3419 // move all effects to the correct producer
3420 mltPasteEffects(clipProducer, clip);
3421 trackPlaylist.insert_at(pos, clip, 1);
3423 delete clipProducer;
3427 bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd, Mlt::Producer *prod, bool overwrite, bool /*insert*/)
3429 Mlt::Service service(m_mltProducer->parent().get_service());
3430 if (service.type() != tractor_type) {
3431 kWarning() << "// TRACTOR PROBLEM";
3435 Mlt::Tractor tractor(service);
3437 Mlt::Producer trackProducer(tractor.track(startTrack));
3438 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3439 int clipIndex = trackPlaylist.get_clip_index_at(moveStart);
3440 int clipDuration = trackPlaylist.clip_length(clipIndex);
3441 bool checkLength = false;
3442 if (endTrack == startTrack) {
3443 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3445 bool success = true;
3446 if (!trackPlaylist.is_blank_at(moveEnd) || !clipProducer || !clipProducer->is_valid() || clipProducer->is_blank()) {
3450 // Check that the destination region is empty
3451 trackPlaylist.consolidate_blanks(0);
3452 int destinationIndex = trackPlaylist.get_clip_index_at(moveEnd);
3453 if (destinationIndex < trackPlaylist.count() - 1) {
3454 // We are not at the end of the track
3455 int blankSize = trackPlaylist.blanks_from(destinationIndex, 1);
3456 // Make sure we have enough place to insert clip
3457 if (blankSize - clipDuration - (moveEnd - trackPlaylist.clip_start(destinationIndex)) < 0) success = false;
3462 trackPlaylist.insert_at(moveStart, clipProducer, 1);
3463 delete clipProducer;
3465 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
3472 trackPlaylist.remove_region(moveEnd, clipProducer->get_playtime());
3473 int clipIndex = trackPlaylist.get_clip_index_at(moveEnd);
3474 trackPlaylist.insert_blank(clipIndex, clipProducer->get_playtime() - 1);
3476 int newIndex = trackPlaylist.insert_at(moveEnd, clipProducer, 1);
3477 if (newIndex == -1) {
3478 kDebug()<<"// CANNOT MOVE CLIP TO: "<<moveEnd;
3479 trackPlaylist.insert_at(moveStart, clipProducer, 1);
3480 delete clipProducer;
3484 trackPlaylist.consolidate_blanks(1);
3485 delete clipProducer;
3486 if (newIndex + 1 == trackPlaylist.count()) checkLength = true;
3488 Mlt::Producer destTrackProducer(tractor.track(endTrack));
3489 Mlt::Playlist destTrackPlaylist((mlt_playlist) destTrackProducer.get_service());
3490 if (!overwrite && !destTrackPlaylist.is_blank_at(moveEnd)) {
3491 // error, destination is not empty
3492 kDebug() << "Cannot move: Destination is not empty";
3496 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3497 if (!clipProducer || clipProducer->is_blank()) {
3498 // error, destination is not empty
3499 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
3500 if (clipProducer) delete clipProducer;
3501 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
3505 trackPlaylist.consolidate_blanks(0);
3506 destTrackPlaylist.consolidate_blanks(1);
3507 Mlt::Producer *clip;
3508 // check if we are moving a slowmotion producer
3509 QString serv = clipProducer->parent().get("mlt_service");
3510 QString currentid = clipProducer->parent().get("id");
3511 if (serv == "framebuffer" || currentid.endsWith("_video")) {
3512 clip = clipProducer;
3515 // Special case: prod is null when using placeholder clips.
3516 // in that case, use the producer existing in playlist. Note that
3517 // it will bypass the one producer per track logic and might cause
3518 // Sound cracks if clip is moved so that it overlaps another copy of itself
3519 clip = clipProducer->cut(clipProducer->get_in(), clipProducer->get_out());
3520 } else clip = prod->cut(clipProducer->get_in(), clipProducer->get_out());
3523 // move all effects to the correct producer
3524 mltPasteEffects(clipProducer, clip);
3527 destTrackPlaylist.remove_region(moveEnd, clip->get_playtime());
3528 int clipIndex = destTrackPlaylist.get_clip_index_at(moveEnd);
3529 destTrackPlaylist.insert_blank(clipIndex, clip->get_playtime() - 1);
3532 int newIndex = destTrackPlaylist.insert_at(moveEnd, clip, 1);
3534 if (clip == clipProducer) {
3539 delete clipProducer;
3541 destTrackPlaylist.consolidate_blanks(0);
3542 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
3543 kDebug() << "//////// moving clip transparency";
3544 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
3546 if (clipIndex > trackPlaylist.count()) checkLength = true;
3547 else if (newIndex + 1 == destTrackPlaylist.count()) checkLength = true;
3551 if (checkLength) mltCheckLength(&tractor);
3553 //m_mltConsumer->set("refresh", 1);
3558 QList <int> Render::checkTrackSequence(int track)
3561 Mlt::Service service(m_mltProducer->parent().get_service());
3562 if (service.type() != tractor_type) {
3563 kWarning() << "// TRACTOR PROBLEM";
3566 Mlt::Tractor tractor(service);
3568 Mlt::Producer trackProducer(tractor.track(track));
3569 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3570 int clipNb = trackPlaylist.count();
3571 //kDebug() << "// PARSING SCENE TRACK: " << t << ", CLIPS: " << clipNb;
3572 for (int i = 0; i < clipNb; i++) {
3573 Mlt::Producer *c = trackPlaylist.get_clip(i);
3574 int pos = trackPlaylist.clip_start(i);
3575 if (!list.contains(pos)) list.append(pos);
3576 pos += c->get_playtime();
3577 if (!list.contains(pos)) list.append(pos);
3583 bool Render::mltMoveTransition(QString type, int startTrack, int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut)
3585 int new_in = (int)newIn.frames(m_fps);
3586 int new_out = (int)newOut.frames(m_fps) - 1;
3587 if (new_in >= new_out) return false;
3588 int old_in = (int)oldIn.frames(m_fps);
3589 int old_out = (int)oldOut.frames(m_fps) - 1;
3591 Mlt::Service service(m_mltProducer->parent().get_service());
3592 Mlt::Tractor tractor(service);
3593 Mlt::Field *field = tractor.field();
3595 bool doRefresh = true;
3596 // Check if clip is visible in monitor
3597 int diff = old_out - m_mltProducer->position();
3598 if (diff < 0 || diff > old_out - old_in) doRefresh = false;
3600 diff = new_out - m_mltProducer->position();
3601 if (diff < 0 || diff > new_out - new_in) doRefresh = false;
3605 mlt_service nextservice = mlt_service_get_producer(service.get_service());
3606 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3607 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3608 QString resource = mlt_properties_get(properties, "mlt_service");
3609 int old_pos = (int)(old_in + old_out) / 2;
3612 while (mlt_type == "transition") {
3613 Mlt::Transition transition((mlt_transition) nextservice);
3614 nextservice = mlt_service_producer(nextservice);
3615 int currentTrack = transition.get_b_track();
3616 int currentIn = (int) transition.get_in();
3617 int currentOut = (int) transition.get_out();
3619 if (resource == type && startTrack == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
3621 if (newTrack - startTrack != 0) {
3622 Mlt::Properties trans_props(transition.get_properties());
3623 Mlt::Transition new_transition(*m_mltProfile, transition.get("mlt_service"));
3624 Mlt::Properties new_trans_props(new_transition.get_properties());
3625 new_trans_props.inherit(trans_props);
3626 new_transition.set_in_and_out(new_in, new_out);
3627 field->disconnect_service(transition);
3628 mltPlantTransition(field, new_transition, newTransitionTrack, newTrack);
3629 //field->plant_transition(new_transition, newTransitionTrack, newTrack);
3630 } else transition.set_in_and_out(new_in, new_out);
3633 if (nextservice == NULL) break;
3634 properties = MLT_SERVICE_PROPERTIES(nextservice);
3635 mlt_type = mlt_properties_get(properties, "mlt_type");
3636 resource = mlt_properties_get(properties, "mlt_service");
3639 if (doRefresh) refresh();
3640 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3645 void Render::mltPlantTransition(Mlt::Field *field, Mlt::Transition &tr, int a_track, int b_track)
3647 mlt_service nextservice = mlt_service_get_producer(field->get_service());
3648 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3649 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3650 QString resource = mlt_properties_get(properties, "mlt_service");
3651 QList <Mlt::Transition *> trList;
3652 mlt_properties insertproperties = tr.get_properties();
3653 QString insertresource = mlt_properties_get(insertproperties, "mlt_service");
3654 bool isMixTransition = insertresource == "mix";
3656 while (mlt_type == "transition") {
3657 Mlt::Transition transition((mlt_transition) nextservice);
3658 nextservice = mlt_service_producer(nextservice);
3659 int aTrack = transition.get_a_track();
3660 int bTrack = transition.get_b_track();
3661 if ((isMixTransition || resource != "mix") && (aTrack < a_track || (aTrack == a_track && bTrack > b_track))) {
3662 Mlt::Properties trans_props(transition.get_properties());
3663 Mlt::Transition *cp = new Mlt::Transition(*m_mltProfile, transition.get("mlt_service"));
3664 Mlt::Properties new_trans_props(cp->get_properties());
3665 new_trans_props.inherit(trans_props);
3667 field->disconnect_service(transition);
3669 //else kDebug() << "// FOUND TRANS OK, "<<resource<< ", A_: " << aTrack << ", B_ "<<bTrack;
3671 if (nextservice == NULL) break;
3672 properties = MLT_SERVICE_PROPERTIES(nextservice);
3673 mlt_type = mlt_properties_get(properties, "mlt_type");
3674 resource = mlt_properties_get(properties, "mlt_service");
3676 field->plant_transition(tr, a_track, b_track);
3678 // re-add upper transitions
3679 for (int i = trList.count() - 1; i >= 0; i--) {
3680 //kDebug()<< "REPLANT ON TK: "<<trList.at(i)->get_a_track()<<", "<<trList.at(i)->get_b_track();
3681 field->plant_transition(*trList.at(i), trList.at(i)->get_a_track(), trList.at(i)->get_b_track());
3686 void Render::mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool force)
3688 if (oldTag == tag && !force) mltUpdateTransitionParams(tag, a_track, b_track, in, out, xml);
3690 //kDebug()<<"// DELETING TRANS: "<<a_track<<"-"<<b_track;
3691 mltDeleteTransition(oldTag, a_track, b_track, in, out, xml, false);
3692 mltAddTransition(tag, a_track, b_track, in, out, xml, false);
3695 if (m_mltProducer->position() >= in.frames(m_fps) && m_mltProducer->position() <= out.frames(m_fps)) refresh();
3698 void Render::mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml)
3700 mlt_service serv = m_mltProducer->parent().get_service();
3701 mlt_service_lock(serv);
3703 mlt_service nextservice = mlt_service_get_producer(serv);
3704 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3705 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3706 QString resource = mlt_properties_get(properties, "mlt_service");
3707 int in_pos = (int) in.frames(m_fps);
3708 int out_pos = (int) out.frames(m_fps) - 1;
3710 while (mlt_type == "transition") {
3711 mlt_transition tr = (mlt_transition) nextservice;
3712 int currentTrack = mlt_transition_get_b_track(tr);
3713 int currentBTrack = mlt_transition_get_a_track(tr);
3714 int currentIn = (int) mlt_transition_get_in(tr);
3715 int currentOut = (int) mlt_transition_get_out(tr);
3717 // kDebug()<<"Looking for transition : " << currentIn <<'x'<<currentOut<< ", OLD oNE: "<<in_pos<<'x'<<out_pos;
3718 if (resource == type && b_track == currentTrack && currentIn == in_pos && currentOut == out_pos) {
3719 QMap<QString, QString> map = mltGetTransitionParamsFromXml(xml);
3720 QMap<QString, QString>::Iterator it;
3722 mlt_properties transproperties = MLT_TRANSITION_PROPERTIES(tr);
3724 QString currentId = mlt_properties_get(transproperties, "kdenlive_id");
3725 if (currentId != xml.attribute("id")) {
3726 // The transition ID is not the same, so reset all properties
3727 mlt_properties_set(transproperties, "kdenlive_id", xml.attribute("id").toUtf8().constData());
3728 // Cleanup previous properties
3729 QStringList permanentProps;
3730 permanentProps << "factory" << "kdenlive_id" << "mlt_service" << "mlt_type" << "in";
3731 permanentProps << "out" << "a_track" << "b_track";
3732 for (int i = 0; i < mlt_properties_count(transproperties); i++) {
3733 QString propName = mlt_properties_get_name(transproperties, i);
3734 if (!propName.startsWith('_') && ! permanentProps.contains(propName)) {
3735 mlt_properties_set(transproperties, propName.toUtf8().constData(), "");
3740 mlt_properties_set_int(transproperties, "force_track", xml.attribute("force_track").toInt());
3741 mlt_properties_set_int(transproperties, "automatic", xml.attribute("automatic", "0").toInt());
3743 if (currentBTrack != a_track) {
3744 mlt_properties_set_int(transproperties, "a_track", a_track);
3746 for (it = map.begin(); it != map.end(); ++it) {
3748 mlt_properties_set(transproperties, key.toUtf8().constData(), it.value().toUtf8().constData());
3749 //kDebug() << " ------ UPDATING TRANS PARAM: " << key.toUtf8().constData() << ": " << it.value().toUtf8().constData();
3750 //filter->set("kdenlive_id", id);
3754 nextservice = mlt_service_producer(nextservice);
3755 if (nextservice == NULL) break;
3756 properties = MLT_SERVICE_PROPERTIES(nextservice);
3757 mlt_type = mlt_properties_get(properties, "mlt_type");
3758 resource = mlt_properties_get(properties, "mlt_service");
3760 mlt_service_unlock(serv);
3762 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3765 void Render::mltDeleteTransition(QString tag, int /*a_track*/, int b_track, GenTime in, GenTime out, QDomElement /*xml*/, bool /*do_refresh*/)
3767 mlt_service serv = m_mltProducer->parent().get_service();
3768 mlt_service_lock(serv);
3770 Mlt::Service service(serv);
3771 Mlt::Tractor tractor(service);
3772 Mlt::Field *field = tractor.field();
3774 //if (do_refresh) m_mltConsumer->set("refresh", 0);
3776 mlt_service nextservice = mlt_service_get_producer(serv);
3777 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3778 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3779 QString resource = mlt_properties_get(properties, "mlt_service");
3781 const int old_pos = (int)((in + out).frames(m_fps) / 2);
3782 //kDebug() << " del trans pos: " << in.frames(25) << "-" << out.frames(25);
3784 while (mlt_type == "transition") {
3785 mlt_transition tr = (mlt_transition) nextservice;
3786 int currentTrack = mlt_transition_get_b_track(tr);
3787 int currentIn = (int) mlt_transition_get_in(tr);
3788 int currentOut = (int) mlt_transition_get_out(tr);
3789 //kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
3791 if (resource == tag && b_track == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
3792 mlt_field_disconnect_service(field->get_field(), nextservice);
3795 nextservice = mlt_service_producer(nextservice);
3796 if (nextservice == NULL) break;
3797 properties = MLT_SERVICE_PROPERTIES(nextservice);
3798 mlt_type = mlt_properties_get(properties, "mlt_type");
3799 resource = mlt_properties_get(properties, "mlt_service");
3801 mlt_service_unlock(serv);
3803 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3806 QMap<QString, QString> Render::mltGetTransitionParamsFromXml(QDomElement xml)
3808 QDomNodeList attribs = xml.elementsByTagName("parameter");
3809 QMap<QString, QString> map;
3810 for (int i = 0; i < attribs.count(); i++) {
3811 QDomElement e = attribs.item(i).toElement();
3812 QString name = e.attribute("name");
3813 //kDebug()<<"-- TRANSITION PARAM: "<<name<<" = "<< e.attribute("name")<<" / " << e.attribute("value");
3814 map[name] = e.attribute("default");
3815 if (!e.attribute("value").isEmpty()) {
3816 map[name] = e.attribute("value");
3818 if (e.attribute("type") != "addedgeometry" && (e.attribute("factor", "1") != "1" || e.attribute("offset", "0") != "0")) {
3819 map[name] = m_locale.toString((map.value(name).toDouble() - e.attribute("offset", "0").toDouble()) / e.attribute("factor", "1").toDouble());
3820 //map[name]=map[name].replace(".",","); //FIXME how to solve locale conversion of . ,
3823 if (e.attribute("namedesc").contains(';')) {
3824 QString format = e.attribute("format");
3825 QStringList separators = format.split("%d", QString::SkipEmptyParts);
3826 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
3828 QTextStream txtNeu(&neu);
3829 if (values.size() > 0)
3830 txtNeu << (int)values[0].toDouble();
3832 for (i = 0; i < separators.size() && i + 1 < values.size(); i++) {
3833 txtNeu << separators[i];
3834 txtNeu << (int)(values[i+1].toDouble());
3836 if (i < separators.size())
3837 txtNeu << separators[i];
3838 map[e.attribute("name")] = neu;
3845 void Render::mltAddClipTransparency(ItemInfo info, int transitiontrack, int id)
3847 kDebug() << "///////// ADDING CLIP TRANSPARENCY AT: " << info.startPos.frames(25);
3848 Mlt::Service service(m_mltProducer->parent().get_service());
3849 Mlt::Tractor tractor(service);
3850 Mlt::Field *field = tractor.field();
3852 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
3853 transition->set_in_and_out((int) info.startPos.frames(m_fps), (int) info.endPos.frames(m_fps) - 1);
3854 transition->set("transparency", id);
3855 transition->set("fill", 1);
3856 transition->set("internal_added", 237);
3857 field->plant_transition(*transition, transitiontrack, info.track);
3861 void Render::mltDeleteTransparency(int pos, int track, int id)
3863 Mlt::Service service(m_mltProducer->parent().get_service());
3864 Mlt::Tractor tractor(service);
3865 Mlt::Field *field = tractor.field();
3867 //if (do_refresh) m_mltConsumer->set("refresh", 0);
3868 mlt_service serv = m_mltProducer->parent().get_service();
3870 mlt_service nextservice = mlt_service_get_producer(serv);
3871 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3872 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3873 QString resource = mlt_properties_get(properties, "mlt_service");
3875 while (mlt_type == "transition") {
3876 mlt_transition tr = (mlt_transition) nextservice;
3877 int currentTrack = mlt_transition_get_b_track(tr);
3878 int currentIn = (int) mlt_transition_get_in(tr);
3879 int currentOut = (int) mlt_transition_get_out(tr);
3880 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3881 kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
3883 if (resource == "composite" && track == currentTrack && currentIn == pos && transitionId == id) {
3884 //kDebug() << " / / / / /DELETE TRANS DOOOMNE";
3885 mlt_field_disconnect_service(field->get_field(), nextservice);
3888 nextservice = mlt_service_producer(nextservice);
3889 if (nextservice == NULL) break;
3890 properties = MLT_SERVICE_PROPERTIES(nextservice);
3891 mlt_type = mlt_properties_get(properties, "mlt_type");
3892 resource = mlt_properties_get(properties, "mlt_service");
3894 //if (do_refresh) m_mltConsumer->set("refresh", 1);
3897 void Render::mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id)
3899 Mlt::Service service(m_mltProducer->parent().get_service());
3900 Mlt::Tractor tractor(service);
3903 m_mltConsumer->set("refresh", 0);
3905 mlt_service serv = m_mltProducer->parent().get_service();
3906 mlt_service nextservice = mlt_service_get_producer(serv);
3907 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3908 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3909 QString resource = mlt_properties_get(properties, "mlt_service");
3910 kDebug() << "// resize transpar from: " << oldStart << ", TO: " << newStart << 'x' << newEnd << ", " << track << ", " << id;
3911 while (mlt_type == "transition") {
3912 mlt_transition tr = (mlt_transition) nextservice;
3913 int currentTrack = mlt_transition_get_b_track(tr);
3914 int currentIn = (int) mlt_transition_get_in(tr);
3915 //mlt_properties props = MLT_TRANSITION_PROPERTIES(tr);
3916 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3917 kDebug() << "// resize transpar current in: " << currentIn << ", Track: " << currentTrack << ", id: " << id << 'x' << transitionId ;
3918 if (resource == "composite" && track == currentTrack && currentIn == oldStart && transitionId == id) {
3919 kDebug() << " / / / / /RESIZE TRANS TO: " << newStart << 'x' << newEnd;
3920 mlt_transition_set_in_and_out(tr, newStart, newEnd);
3923 nextservice = mlt_service_producer(nextservice);
3924 if (nextservice == NULL) break;
3925 properties = MLT_SERVICE_PROPERTIES(nextservice);
3926 mlt_type = mlt_properties_get(properties, "mlt_type");
3927 resource = mlt_properties_get(properties, "mlt_service");
3930 m_mltConsumer->set("refresh", 1);
3934 void Render::mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id)
3936 Mlt::Service service(m_mltProducer->parent().get_service());
3937 Mlt::Tractor tractor(service);
3940 m_mltConsumer->set("refresh", 0);
3942 mlt_service serv = m_mltProducer->parent().get_service();
3943 mlt_service nextservice = mlt_service_get_producer(serv);
3944 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3945 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3946 QString resource = mlt_properties_get(properties, "mlt_service");
3948 while (mlt_type == "transition") {
3949 mlt_transition tr = (mlt_transition) nextservice;
3950 int currentTrack = mlt_transition_get_b_track(tr);
3951 int currentaTrack = mlt_transition_get_a_track(tr);
3952 int currentIn = (int) mlt_transition_get_in(tr);
3953 int currentOut = (int) mlt_transition_get_out(tr);
3954 //mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3955 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3956 //kDebug()<<" + TRANSITION "<<id<<" == "<<transitionId<<", START TMIE: "<<currentIn<<", LOOK FR: "<<startTime<<", TRACK: "<<currentTrack<<'x'<<startTrack;
3957 if (resource == "composite" && transitionId == id && startTime == currentIn && startTrack == currentTrack) {
3958 kDebug() << "//////MOVING";
3959 mlt_transition_set_in_and_out(tr, endTime, endTime + currentOut - currentIn);
3960 if (endTrack != startTrack) {
3961 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3962 mlt_properties_set_int(properties, "a_track", currentaTrack + endTrack - currentTrack);
3963 mlt_properties_set_int(properties, "b_track", endTrack);
3967 nextservice = mlt_service_producer(nextservice);
3968 if (nextservice == NULL) break;
3969 properties = MLT_SERVICE_PROPERTIES(nextservice);
3970 mlt_type = mlt_properties_get(properties, "mlt_type");
3971 resource = mlt_properties_get(properties, "mlt_service");
3974 m_mltConsumer->set("refresh", 1);
3978 bool Render::mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool do_refresh)
3980 if (in >= out) return false;
3981 QMap<QString, QString> args = mltGetTransitionParamsFromXml(xml);
3982 Mlt::Service service(m_mltProducer->parent().get_service());
3984 Mlt::Tractor tractor(service);
3985 Mlt::Field *field = tractor.field();
3987 Mlt::Transition transition(*m_mltProfile, tag.toUtf8().constData());
3988 if (out != GenTime())
3989 transition.set_in_and_out((int) in.frames(m_fps), (int) out.frames(m_fps) - 1);
3991 if (do_refresh && (m_mltProducer->position() < in.frames(m_fps) || m_mltProducer->position() > out.frames(m_fps))) do_refresh = false;
3992 QMap<QString, QString>::Iterator it;
3994 if (xml.attribute("automatic") == "1") transition.set("automatic", 1);
3995 //kDebug() << " ------ ADDING TRANSITION PARAMs: " << args.count();
3996 if (xml.hasAttribute("id"))
3997 transition.set("kdenlive_id", xml.attribute("id").toUtf8().constData());
3998 if (xml.hasAttribute("force_track"))
3999 transition.set("force_track", xml.attribute("force_track").toInt());
4001 for (it = args.begin(); it != args.end(); ++it) {
4003 if (!it.value().isEmpty())
4004 transition.set(key.toUtf8().constData(), it.value().toUtf8().constData());
4005 //kDebug() << " ------ ADDING TRANS PARAM: " << key << ": " << it.value();
4007 // attach transition
4009 mltPlantTransition(field, transition, a_track, b_track);
4010 // field->plant_transition(*transition, a_track, b_track);
4012 if (do_refresh) refresh();
4016 void Render::mltSavePlaylist()
4018 kWarning() << "// UPDATING PLAYLIST TO DISK++++++++++++++++";
4019 Mlt::Consumer fileConsumer(*m_mltProfile, "xml");
4020 fileConsumer.set("resource", "/tmp/playlist.mlt");
4022 Mlt::Service service(m_mltProducer->get_service());
4024 fileConsumer.connect(service);
4025 fileConsumer.start();
4028 const QList <Mlt::Producer *> Render::producersList()
4030 QList <Mlt::Producer *> prods;
4031 if (m_mltProducer == NULL) return prods;
4032 Mlt::Service service(m_mltProducer->parent().get_service());
4033 if (service.type() != tractor_type) return prods;
4034 Mlt::Tractor tractor(service);
4037 int trackNb = tractor.count();
4038 for (int t = 1; t < trackNb; t++) {
4039 Mlt::Producer *tt = tractor.track(t);
4040 Mlt::Producer trackProducer(tt);
4042 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
4043 int clipNb = trackPlaylist.count();
4044 for (int i = 0; i < clipNb; i++) {
4045 Mlt::Producer *c = trackPlaylist.get_clip(i);
4046 if (c == NULL) continue;
4047 QString prodId = c->parent().get("id");
4048 if (!c->is_blank() && !ids.contains(prodId) && !prodId.startsWith("slowmotion") && !prodId.isEmpty()) {
4049 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
4052 prods.append(nprod);
4061 void Render::fillSlowMotionProducers()
4063 if (m_mltProducer == NULL) return;
4064 Mlt::Service service(m_mltProducer->parent().get_service());
4065 if (service.type() != tractor_type) return;
4067 Mlt::Tractor tractor(service);
4069 int trackNb = tractor.count();
4070 for (int t = 1; t < trackNb; t++) {
4071 Mlt::Producer *tt = tractor.track(t);
4072 Mlt::Producer trackProducer(tt);
4074 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
4075 int clipNb = trackPlaylist.count();
4076 for (int i = 0; i < clipNb; i++) {
4077 Mlt::Producer *c = trackPlaylist.get_clip(i);
4078 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
4080 QString id = nprod->parent().get("id");
4081 if (id.startsWith("slowmotion:") && !nprod->is_blank()) {
4082 // this is a slowmotion producer, add it to the list
4083 QString url = QString::fromUtf8(nprod->get("resource"));
4084 int strobe = nprod->get_int("strobe");
4085 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
4086 if (!m_slowmotionProducers.contains(url)) {
4087 m_slowmotionProducers.insert(url, nprod);
4089 } else delete nprod;
4096 QList <TransitionInfo> Render::mltInsertTrack(int ix, bool videoTrack)
4098 Mlt::Service service(m_mltProducer->parent().get_service());
4099 if (service.type() != tractor_type) {
4100 kWarning() << "// TRACTOR PROBLEM";
4101 return QList <TransitionInfo> ();
4105 Mlt::Tractor tractor(service);
4106 QList <TransitionInfo> transitionInfos;
4107 Mlt::Playlist playlist;
4108 int ct = tractor.count();
4110 kDebug() << "// ERROR, TRYING TO insert TRACK " << ix << ", max: " << ct;
4116 Mlt::Producer *prodToMove = new Mlt::Producer(tractor.track(pos));
4117 tractor.set_track(playlist, pos);
4118 Mlt::Producer newProd(tractor.track(pos));
4119 if (!videoTrack) newProd.set("hide", 1);
4121 for (; pos <= ct; pos++) {
4122 Mlt::Producer *prodToMove2 = new Mlt::Producer(tractor.track(pos));
4123 tractor.set_track(*prodToMove, pos);
4124 prodToMove = prodToMove2;
4127 tractor.set_track(playlist, ix);
4128 Mlt::Producer newProd(tractor.track(ix));
4129 if (!videoTrack) newProd.set("hide", 1);
4134 mlt_service serv = m_mltProducer->parent().get_service();
4135 mlt_service nextservice = mlt_service_get_producer(serv);
4136 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
4137 QString mlt_type = mlt_properties_get(properties, "mlt_type");
4138 QString resource = mlt_properties_get(properties, "mlt_service");
4139 Mlt::Field *field = tractor.field();
4140 QList <Mlt::Transition *> trList;
4142 while (mlt_type == "transition") {
4143 if (resource != "mix") {
4144 Mlt::Transition transition((mlt_transition) nextservice);
4145 nextservice = mlt_service_producer(nextservice);
4146 int currentbTrack = transition.get_b_track();
4147 int currentaTrack = transition.get_a_track();
4148 bool trackChanged = false;
4149 bool forceTransitionTrack = false;
4150 if (currentbTrack >= ix) {
4151 if (currentbTrack == ix && currentaTrack < ix) forceTransitionTrack = true;
4153 trackChanged = true;
4155 if (currentaTrack >= ix) {
4157 trackChanged = true;
4159 kDebug()<<"// Newtrans: "<<currentaTrack<<"/"<<currentbTrack;
4161 // disconnect all transitions
4162 Mlt::Properties trans_props(transition.get_properties());
4163 Mlt::Transition *cp = new Mlt::Transition(*m_mltProfile, transition.get("mlt_service"));
4164 Mlt::Properties new_trans_props(cp->get_properties());
4165 new_trans_props.inherit(trans_props);
4168 // Transition track needs to be adjusted
4169 cp->set("a_track", currentaTrack);
4170 cp->set("b_track", currentbTrack);
4171 // Check if transition track was changed and needs to be forced
4172 if (forceTransitionTrack) cp->set("force_track", 1);
4173 TransitionInfo trInfo;
4174 trInfo.startPos = GenTime(transition.get_in(), m_fps);
4175 trInfo.a_track = currentaTrack;
4176 trInfo.b_track = currentbTrack;
4177 trInfo.forceTrack = cp->get_int("force_track");
4178 transitionInfos.append(trInfo);
4181 field->disconnect_service(transition);
4183 else nextservice = mlt_service_producer(nextservice);
4184 if (nextservice == NULL) break;
4185 properties = MLT_SERVICE_PROPERTIES(nextservice);
4186 mlt_type = mlt_properties_get(properties, "mlt_type");
4187 resource = mlt_properties_get(properties, "mlt_service");
4190 // Add audio mix transition to last track
4191 Mlt::Transition transition(*m_mltProfile, "mix");
4192 transition.set("a_track", 1);
4193 transition.set("b_track", ct);
4194 transition.set("always_active", 1);
4195 transition.set("internal_added", 237);
4196 transition.set("combine", 1);
4197 mltPlantTransition(field, transition, 1, ct);
4199 // re-add transitions
4200 for (int i = trList.count() - 1; i >= 0; i--) {
4201 field->plant_transition(*trList.at(i), trList.at(i)->get_a_track(), trList.at(i)->get_b_track());
4206 blockSignals(false);
4207 return transitionInfos;
4211 void Render::mltDeleteTrack(int ix)
4214 doc.setContent(sceneList(), false);
4215 int tracksCount = doc.elementsByTagName("track").count() - 1;
4216 QDomNode track = doc.elementsByTagName("track").at(ix);
4217 QDomNode tractor = doc.elementsByTagName("tractor").at(0);
4218 QDomNodeList transitions = doc.elementsByTagName("transition");
4219 for (int i = 0; i < transitions.count(); i++) {
4220 QDomElement e = transitions.at(i).toElement();
4221 QDomNodeList props = e.elementsByTagName("property");
4222 QMap <QString, QString> mappedProps;
4223 for (int j = 0; j < props.count(); j++) {
4224 QDomElement f = props.at(j).toElement();
4225 mappedProps.insert(f.attribute("name"), f.firstChild().nodeValue());
4227 if (mappedProps.value("mlt_service") == "mix" && mappedProps.value("b_track").toInt() == tracksCount) {
4228 tractor.removeChild(transitions.at(i));
4230 } else if (mappedProps.value("mlt_service") != "mix" && (mappedProps.value("b_track").toInt() >= ix || mappedProps.value("a_track").toInt() >= ix)) {
4231 // Transition needs to be moved
4232 int a_track = mappedProps.value("a_track").toInt();
4233 int b_track = mappedProps.value("b_track").toInt();
4234 if (a_track > 0 && a_track >= ix) a_track --;
4235 if (b_track == ix) {
4236 // transition was on the deleted track, so remove it
4237 tractor.removeChild(transitions.at(i));
4241 if (b_track > 0 && b_track > ix) b_track --;
4242 for (int j = 0; j < props.count(); j++) {
4243 QDomElement f = props.at(j).toElement();
4244 if (f.attribute("name") == "a_track") f.firstChild().setNodeValue(QString::number(a_track));
4245 else if (f.attribute("name") == "b_track") f.firstChild().setNodeValue(QString::number(b_track));
4250 tractor.removeChild(track);
4251 //kDebug() << "/////////// RESULT SCENE: \n" << doc.toString();
4252 setSceneList(doc.toString(), m_mltConsumer->position());
4253 emit refreshDocumentProducers(false, false);
4257 void Render::updatePreviewSettings()
4259 kDebug() << "////// RESTARTING CONSUMER";
4260 if (!m_mltConsumer || !m_mltProducer) return;
4261 if (m_mltProducer->get_playtime() == 0) return;
4262 QMutexLocker locker(&m_mutex);
4263 Mlt::Service service(m_mltProducer->parent().get_service());
4264 if (service.type() != tractor_type) return;
4266 //m_mltConsumer->set("refresh", 0);
4267 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
4268 m_mltConsumer->purge();
4269 QString scene = sceneList();
4271 if (m_mltProducer) {
4272 pos = m_mltProducer->position();
4275 setSceneList(scene, pos);
4279 QString Render::updateSceneListFps(double current_fps, double new_fps, QString scene)
4281 // Update all frame positions to the new fps value
4282 //WARNING: there are probably some effects or other that hold a frame value
4283 // as parameter and will also need to be updated here!
4285 doc.setContent(scene);
4287 double factor = new_fps / current_fps;
4288 QDomNodeList producers = doc.elementsByTagName("producer");
4289 for (int i = 0; i < producers.count(); i++) {
4290 QDomElement prod = producers.at(i).toElement();
4291 prod.removeAttribute("in");
4292 prod.removeAttribute("out");
4294 QDomNodeList props = prod.childNodes();
4295 for (int j = 0; j < props.count(); j++) {
4296 QDomElement param = props.at(j).toElement();
4297 QString paramName = param.attribute("name");
4298 if (paramName.startsWith("meta.") || paramName == "length") {
4299 prod.removeChild(props.at(j));
4305 QDomNodeList entries = doc.elementsByTagName("entry");
4306 for (int i = 0; i < entries.count(); i++) {
4307 QDomElement entry = entries.at(i).toElement();
4308 int in = entry.attribute("in").toInt();
4309 int out = entry.attribute("out").toInt();
4310 in = factor * in + 0.5;
4311 out = factor * out + 0.5;
4312 entry.setAttribute("in", in);
4313 entry.setAttribute("out", out);
4316 QDomNodeList blanks = doc.elementsByTagName("blank");
4317 for (int i = 0; i < blanks.count(); i++) {
4318 QDomElement blank = blanks.at(i).toElement();
4319 int length = blank.attribute("length").toInt();
4320 length = factor * length + 0.5;
4321 blank.setAttribute("length", QString::number(length));
4324 QDomNodeList filters = doc.elementsByTagName("filter");
4325 for (int i = 0; i < filters.count(); i++) {
4326 QDomElement filter = filters.at(i).toElement();
4327 int in = filter.attribute("in").toInt();
4328 int out = filter.attribute("out").toInt();
4329 in = factor * in + 0.5;
4330 out = factor * out + 0.5;
4331 filter.setAttribute("in", in);
4332 filter.setAttribute("out", out);
4335 QDomNodeList transitions = doc.elementsByTagName("transition");
4336 for (int i = 0; i < transitions.count(); i++) {
4337 QDomElement transition = transitions.at(i).toElement();
4338 int in = transition.attribute("in").toInt();
4339 int out = transition.attribute("out").toInt();
4340 in = factor * in + 0.5;
4341 out = factor * out + 0.5;
4342 transition.setAttribute("in", in);
4343 transition.setAttribute("out", out);
4344 QDomNodeList props = transition.childNodes();
4345 for (int j = 0; j < props.count(); j++) {
4346 QDomElement param = props.at(j).toElement();
4347 QString paramName = param.attribute("name");
4348 if (paramName == "geometry") {
4349 QString geom = param.firstChild().nodeValue();
4350 QStringList keys = geom.split(';');
4351 QStringList newKeys;
4352 for (int k = 0; k < keys.size(); ++k) {
4353 if (keys.at(k).contains('=')) {
4354 int pos = keys.at(k).section('=', 0, 0).toInt();
4355 pos = factor * pos + 0.5;
4356 newKeys.append(QString::number(pos) + '=' + keys.at(k).section('=', 1));
4357 } else newKeys.append(keys.at(k));
4359 param.firstChild().setNodeValue(newKeys.join(";"));
4363 QDomElement root = doc.documentElement();
4364 if (!root.isNull()) {
4365 QDomElement tractor = root.firstChildElement("tractor");
4366 int out = tractor.attribute("out").toInt();
4367 out = factor * out + 0.5;
4368 tractor.setAttribute("out", out);
4369 emit durationChanged(out);
4372 //kDebug() << "///////////////////////////// " << out << " \n" << doc.toString() << "\n-------------------------";
4373 return doc.toString();
4377 void Render::sendFrameUpdate()
4379 if (m_mltProducer) {
4380 Mlt::Frame * frame = m_mltProducer->get_frame();
4381 emitFrameUpdated(*frame);
4386 Mlt::Producer* Render::getProducer()
4388 return m_mltProducer;
4391 const QString Render::activeClipId()
4393 if (m_mltProducer) return m_mltProducer->get("id");
4398 bool Render::getBlackMagicDeviceList(KComboBox *devicelist)
4400 Mlt::Profile profile;
4401 Mlt::Producer bm(profile, "decklink");
4402 int found_devices = 0;
4403 if (bm.is_valid()) {
4404 bm.set("list_devices", 1);
4405 found_devices = bm.get_int("devices");
4407 if (found_devices <= 0) {
4408 devicelist->setEnabled(false);
4411 for (int i = 0; i < found_devices; i++) {
4412 char *tmp = qstrdup(QString("device.%1").arg(i).toUtf8().constData());
4413 devicelist->addItem(bm.get(tmp));
4419 bool Render::getBlackMagicOutputDeviceList(KComboBox *devicelist)
4421 Mlt::Profile profile;
4422 Mlt::Consumer bm(profile, "decklink");
4423 int found_devices = 0;
4424 if (bm.is_valid()) {
4425 bm.set("list_devices", 1);
4426 found_devices = bm.get_int("devices");
4428 if (found_devices <= 0) {
4429 devicelist->setEnabled(false);
4432 for (int i = 0; i < found_devices; i++) {
4433 char *tmp = qstrdup(QString("device.%1").arg(i).toUtf8().constData());
4434 devicelist->addItem(bm.get(tmp));
4440 #include "renderer.moc"