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");
565 while (mlt_type == "transition") {
566 QString added = mlt_properties_get(MLT_SERVICE_PROPERTIES(nextservice), "internal_added");
567 if (added == "200") {
568 mlt_field_disconnect_service(field->get_field(), nextservice);
570 nextservice = mlt_service_producer(nextservice);
571 if (nextservice == NULL) break;
572 properties = MLT_SERVICE_PROPERTIES(nextservice);
573 mlt_type = mlt_properties_get(properties, "mlt_type");
574 resource = mlt_properties_get(properties, "mlt_service");
575 m_mltConsumer->set("refresh", 1);
580 void Render::getFileProperties(const QDomElement &xml, const QString &clipId, int imageHeight, bool replaceProducer)
582 requestClipInfo info;
584 info.clipId = clipId;
585 info.imageHeight = imageHeight;
586 info.replaceProducer = replaceProducer;
587 // Make sure we don't request the info for same clip twice
589 m_requestList.removeAll(info);
590 m_requestList.append(info);
591 m_infoMutex.unlock();
592 if (!m_infoThread.isRunning()) {
593 m_infoThread = QtConcurrent::run(this, &Render::processFileProperties);
597 void Render::forceProcessing(const QString &id)
599 if (m_processingClipId == id) return;
600 QMutexLocker lock(&m_infoMutex);
601 for (int i = 0; i < m_requestList.count(); i++) {
602 requestClipInfo info = m_requestList.at(i);
603 if (info.clipId == id) {
606 m_requestList.removeAt(i);
607 m_requestList.prepend(info);
614 int Render::processingItems()
616 QMutexLocker lock(&m_infoMutex);
617 int count = m_requestList.count();
618 if (!m_processingClipId.isEmpty()) {
619 // one clip is currently processed
625 bool Render::isProcessing(const QString &id)
627 if (m_processingClipId == id) return true;
628 QMutexLocker lock(&m_infoMutex);
629 for (int i = 0; i < m_requestList.count(); i++) {
630 requestClipInfo info = m_requestList.at(i);
631 if (info.clipId == id) {
638 void Render::processFileProperties()
640 requestClipInfo info;
642 while (!m_requestList.isEmpty()) {
644 info = m_requestList.takeFirst();
645 m_processingClipId = info.clipId;
646 m_infoMutex.unlock();
650 if (info.xml.hasAttribute("proxy") && info.xml.attribute("proxy") != "-") {
651 path = info.xml.attribute("proxy");
652 // Check for missing proxies
653 if (QFileInfo(path).size() <= 0) {
654 // proxy is missing, re-create it
655 emit requestProxy(info.clipId);
656 proxyProducer = false;
657 path = info.xml.attribute("resource");
659 else proxyProducer = true;
662 path = info.xml.attribute("resource");
663 proxyProducer = false;
666 Mlt::Producer *producer = NULL;
667 CLIPTYPE type = (CLIPTYPE)info.xml.attribute("type").toInt();
670 producer = new Mlt::Producer(*m_mltProfile, 0, ("colour:" + info.xml.attribute("colour")).toUtf8().constData());
671 } else if (type == TEXT) {
672 producer = new Mlt::Producer(*m_mltProfile, 0, ("kdenlivetitle:" + info.xml.attribute("resource")).toUtf8().constData());
673 if (producer && producer->is_valid() && info.xml.hasAttribute("xmldata"))
674 producer->set("xmldata", info.xml.attribute("xmldata").toUtf8().constData());
675 } else if (url.isEmpty()) {
677 QDomElement mlt = doc.createElement("mlt");
678 QDomElement play = doc.createElement("playlist");
679 doc.appendChild(mlt);
680 mlt.appendChild(play);
681 play.appendChild(doc.importNode(info.xml, true));
682 producer = new Mlt::Producer(*m_mltProfile, "xml-string", doc.toString().toUtf8().constData());
684 producer = new Mlt::Producer(*m_mltProfile, path.toUtf8().constData());
687 if (producer == NULL || producer->is_blank() || !producer->is_valid()) {
688 kDebug() << " / / / / / / / / ERROR / / / / // CANNOT LOAD PRODUCER: "<<path;
689 m_processingClipId.clear();
691 // Proxy file is corrupted
692 emit removeInvalidProxy(info.clipId, false);
694 else emit removeInvalidClip(info.clipId, info.replaceProducer);
699 if (proxyProducer && info.xml.hasAttribute("proxy_out")) {
700 producer->set("length", info.xml.attribute("proxy_out").toInt() + 1);
701 producer->set("out", info.xml.attribute("proxy_out").toInt());
702 if (producer->get_out() != info.xml.attribute("proxy_out").toInt()) {
703 // Proxy file length is different than original clip length, this will corrupt project so disable this proxy clip
704 m_processingClipId.clear();
705 emit removeInvalidProxy(info.clipId, true);
711 if (info.xml.hasAttribute("force_aspect_ratio")) {
712 double aspect = info.xml.attribute("force_aspect_ratio").toDouble();
713 if (aspect > 0) producer->set("force_aspect_ratio", aspect);
716 if (info.xml.hasAttribute("force_aspect_num") && info.xml.hasAttribute("force_aspect_den")) {
717 int width = info.xml.attribute("frame_size").section('x', 0, 0).toInt();
718 int height = info.xml.attribute("frame_size").section('x', 1, 1).toInt();
719 int aspectNumerator = info.xml.attribute("force_aspect_num").toInt();
720 int aspectDenominator = info.xml.attribute("force_aspect_den").toInt();
721 if (aspectDenominator != 0 && width != 0)
722 producer->set("force_aspect_ratio", double(height) * aspectNumerator / aspectDenominator / width);
725 if (info.xml.hasAttribute("force_fps")) {
726 double fps = info.xml.attribute("force_fps").toDouble();
727 if (fps > 0) producer->set("force_fps", fps);
730 if (info.xml.hasAttribute("force_progressive")) {
732 int progressive = info.xml.attribute("force_progressive").toInt(&ok);
733 if (ok) producer->set("force_progressive", progressive);
735 if (info.xml.hasAttribute("force_tff")) {
737 int fieldOrder = info.xml.attribute("force_tff").toInt(&ok);
738 if (ok) producer->set("force_tff", fieldOrder);
740 if (info.xml.hasAttribute("threads")) {
741 int threads = info.xml.attribute("threads").toInt();
742 if (threads != 1) producer->set("threads", threads);
744 if (info.xml.hasAttribute("video_index")) {
745 int vindex = info.xml.attribute("video_index").toInt();
746 if (vindex != 0) producer->set("video_index", vindex);
748 if (info.xml.hasAttribute("audio_index")) {
749 int aindex = info.xml.attribute("audio_index").toInt();
750 if (aindex != 0) producer->set("audio_index", aindex);
752 if (info.xml.hasAttribute("force_colorspace")) {
753 int colorspace = info.xml.attribute("force_colorspace").toInt();
754 if (colorspace != 0) producer->set("force_colorspace", colorspace);
756 if (info.xml.hasAttribute("full_luma")) {
757 int full_luma = info.xml.attribute("full_luma").toInt();
758 if (full_luma != 0) producer->set("set.force_full_luma", full_luma);
763 if (info.xml.hasAttribute("out")) clipOut = info.xml.attribute("out").toInt();
765 // setup length here as otherwise default length (currently 15000 frames in MLT) will be taken even if outpoint is larger
766 if (type == COLOR || type == TEXT || type == IMAGE || type == SLIDESHOW) {
768 if (info.xml.hasAttribute("length")) {
769 if (clipOut > 0) duration = clipOut + 1;
770 length = info.xml.attribute("length").toInt();
771 clipOut = length - 1;
773 else length = info.xml.attribute("out").toInt() - info.xml.attribute("in").toInt();
774 producer->set("length", length);
777 if (clipOut > 0) producer->set_in_and_out(info.xml.attribute("in").toInt(), clipOut);
779 producer->set("id", info.clipId.toUtf8().constData());
781 if (info.xml.hasAttribute("templatetext"))
782 producer->set("templatetext", info.xml.attribute("templatetext").toUtf8().constData());
784 int imageWidth = (int)((double) info.imageHeight * m_mltProfile->width() / m_mltProfile->height() + 0.5);
785 int fullWidth = (int)((double) info.imageHeight * m_mltProfile->dar() + 0.5);
786 int frameNumber = info.xml.attribute("thumbnail", "-1").toInt();
788 if ((!info.replaceProducer && info.xml.hasAttribute("file_hash")) || proxyProducer) {
789 // Clip already has all properties
791 // Recreate clip thumb
792 if (frameNumber > 0) producer->seek(frameNumber);
793 Mlt::Frame *frame = producer->get_frame();
794 if (frame && frame->is_valid()) {
795 QImage img = KThumb::getFrame(frame, imageWidth, fullWidth, info.imageHeight);
796 emit replyGetImage(info.clipId, img);
798 if (frame) delete frame;
800 m_processingClipId.clear();
801 emit replyGetFileProperties(info.clipId, producer, stringMap(), stringMap(), info.replaceProducer);
805 stringMap filePropertyMap;
806 stringMap metadataPropertyMap;
809 if (frameNumber > 0) producer->seek(frameNumber);
811 duration = duration > 0 ? duration : producer->get_playtime();
812 filePropertyMap["duration"] = QString::number(duration);
813 //kDebug() << "/////// PRODUCER: " << url.path() << " IS: " << producer->get_playtime();
815 if (type == SLIDESHOW) {
816 int ttl = info.xml.hasAttribute("ttl") ? info.xml.attribute("ttl").toInt() : 0;
817 if (ttl) producer->set("ttl", ttl);
818 if (!info.xml.attribute("animation").isEmpty()) {
819 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "affine");
820 if (filter && filter->is_valid()) {
822 QString geometry = SlideshowClip::animationToGeometry(info.xml.attribute("animation"), cycle);
823 if (!geometry.isEmpty()) {
824 if (info.xml.attribute("animation").contains("low-pass")) {
825 Mlt::Filter *blur = new Mlt::Filter(*m_mltProfile, "boxblur");
826 if (blur && blur->is_valid())
827 producer->attach(*blur);
829 filter->set("transition.geometry", geometry.toUtf8().data());
830 filter->set("transition.cycle", cycle);
831 producer->attach(*filter);
835 if (info.xml.attribute("fade") == "1") {
836 // user wants a fade effect to slideshow
837 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "luma");
838 if (filter && filter->is_valid()) {
839 if (ttl) filter->set("cycle", ttl);
840 if (info.xml.hasAttribute("luma_duration") && !info.xml.attribute("luma_duration").isEmpty()) filter->set("duration", info.xml.attribute("luma_duration").toInt());
841 if (info.xml.hasAttribute("luma_file") && !info.xml.attribute("luma_file").isEmpty()) {
842 filter->set("luma.resource", info.xml.attribute("luma_file").toUtf8().constData());
843 if (info.xml.hasAttribute("softness")) {
844 int soft = info.xml.attribute("softness").toInt();
845 filter->set("luma.softness", (double) soft / 100.0);
848 producer->attach(*filter);
851 if (info.xml.attribute("crop") == "1") {
852 // user wants to center crop the slides
853 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "crop");
854 if (filter && filter->is_valid()) {
855 filter->set("center", 1);
856 producer->attach(*filter);
862 int vindex = producer->get_int("video_index");
864 snprintf(property, sizeof(property), "meta.media.%d.stream.frame_rate", vindex);
865 if (producer->get(property))
866 filePropertyMap["fps"] = producer->get(property);
869 if (!filePropertyMap.contains("fps")) {
870 if (producer->get_double("meta.media.frame_rate_den") > 0) {
871 filePropertyMap["fps"] = locale.toString(producer->get_double("meta.media.frame_rate_num") / producer->get_double("meta.media.frame_rate_den"));
872 } else filePropertyMap["fps"] = producer->get("source_fps");
875 Mlt::Frame *frame = producer->get_frame();
876 if (frame && frame->is_valid()) {
877 filePropertyMap["frame_size"] = QString::number(frame->get_int("width")) + 'x' + QString::number(frame->get_int("height"));
878 filePropertyMap["frequency"] = QString::number(frame->get_int("frequency"));
879 filePropertyMap["channels"] = QString::number(frame->get_int("channels"));
880 filePropertyMap["aspect_ratio"] = frame->get("aspect_ratio");
882 if (frame->get_int("test_image") == 0) {
883 if (url.path().endsWith(".mlt") || url.path().endsWith(".westley") || url.path().endsWith(".kdenlive")) {
884 filePropertyMap["type"] = "playlist";
885 metadataPropertyMap["comment"] = QString::fromUtf8(producer->get("title"));
886 } else if (frame->get_int("test_audio") == 0)
887 filePropertyMap["type"] = "av";
889 filePropertyMap["type"] = "video";
895 img = KThumb::getFrame(frame, imageWidth, fullWidth, info.imageHeight);
896 variance = KThumb::imageVariance(img);
897 if (frameNumber == -1 && variance< 6) {
898 // Thumbnail is not interesting (for example all black, seek to fetch better thumb
899 frameNumber = duration > 100 ? 100 : duration / 2 ;
900 producer->seek(frameNumber);
902 frame = producer->get_frame();
905 } while (variance == -1);
907 if (frameNumber > -1) filePropertyMap["thumbnail"] = QString::number(frameNumber);
908 emit replyGetImage(info.clipId, img);
909 } else if (frame->get_int("test_audio") == 0) {
910 emit replyGetImage(info.clipId, "audio-x-generic", fullWidth, info.imageHeight);
911 filePropertyMap["type"] = "audio";
914 // Retrieve audio / video codec name
918 /*if (context->duration == AV_NOPTS_VALUE) {
919 kDebug() << " / / / / / / / /ERROR / / / CLIP HAS UNKNOWN DURATION";
920 emit removeInvalidClip(clipId);
924 // Get the video_index
926 int default_audio = producer->get_int("audio_index");
929 int scan = producer->get_int("meta.media.progressive");
930 filePropertyMap["progressive"] = QString::number(scan);
932 // Find maximum stream index values
933 for (int ix = 0; ix < producer->get_int("meta.media.nb_streams"); ix++) {
934 snprintf(property, sizeof(property), "meta.media.%d.stream.type", ix);
935 QString type = producer->get(property);
938 else if (type == "audio")
941 filePropertyMap["default_video"] = QString::number(vindex);
942 filePropertyMap["video_max"] = QString::number(video_max);
943 filePropertyMap["default_audio"] = QString::number(default_audio);
944 filePropertyMap["audio_max"] = QString::number(audio_max);
946 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", vindex);
947 if (producer->get(property)) {
948 filePropertyMap["videocodec"] = producer->get(property);
950 snprintf(property, sizeof(property), "meta.media.%d.codec.name", vindex);
951 if (producer->get(property))
952 filePropertyMap["videocodec"] = producer->get(property);
955 query = QString("meta.media.%1.codec.pix_fmt").arg(vindex);
956 filePropertyMap["pix_fmt"] = producer->get(query.toUtf8().constData());
957 filePropertyMap["colorspace"] = producer->get("meta.media.colorspace");
959 } else kDebug() << " / / / / /WARNING, VIDEO CONTEXT IS NULL!!!!!!!!!!!!!!";
960 if (producer->get_int("audio_index") > -1) {
961 // Get the audio_index
962 int index = producer->get_int("audio_index");
963 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", index);
964 if (producer->get(property)) {
965 filePropertyMap["audiocodec"] = producer->get(property);
967 snprintf(property, sizeof(property), "meta.media.%d.codec.name", index);
968 if (producer->get(property))
969 filePropertyMap["audiocodec"] = producer->get(property);
974 Mlt::Properties metadata;
975 metadata.pass_values(*producer, "meta.attr.");
976 int count = metadata.count();
977 for (int i = 0; i < count; i ++) {
978 QString name = metadata.get_name(i);
979 QString value = QString::fromUtf8(metadata.get(i));
980 if (name.endsWith("markup") && !value.isEmpty())
981 metadataPropertyMap[ name.section('.', 0, -2)] = value;
984 m_processingClipId.clear();
985 emit replyGetFileProperties(info.clipId, producer, filePropertyMap, metadataPropertyMap, info.replaceProducer);
987 m_processingClipId.clear();
992 /** Create the producer from the MLT XML QDomDocument */
993 void Render::initSceneList()
995 kDebug() << "-------- INIT SCENE LIST ------_";
997 QDomElement mlt = doc.createElement("mlt");
998 doc.appendChild(mlt);
999 QDomElement prod = doc.createElement("producer");
1000 prod.setAttribute("resource", "colour");
1001 prod.setAttribute("colour", "red");
1002 prod.setAttribute("id", "black");
1003 prod.setAttribute("in", "0");
1004 prod.setAttribute("out", "0");
1006 QDomElement tractor = doc.createElement("tractor");
1007 QDomElement multitrack = doc.createElement("multitrack");
1009 QDomElement playlist1 = doc.createElement("playlist");
1010 playlist1.appendChild(prod);
1011 multitrack.appendChild(playlist1);
1012 QDomElement playlist2 = doc.createElement("playlist");
1013 multitrack.appendChild(playlist2);
1014 QDomElement playlist3 = doc.createElement("playlist");
1015 multitrack.appendChild(playlist3);
1016 QDomElement playlist4 = doc.createElement("playlist");
1017 multitrack.appendChild(playlist4);
1018 QDomElement playlist5 = doc.createElement("playlist");
1019 multitrack.appendChild(playlist5);
1020 tractor.appendChild(multitrack);
1021 mlt.appendChild(tractor);
1022 // kDebug()<<doc.toString();
1024 QString tmp = QString("<mlt><producer resource=\"colour\" colour=\"red\" id=\"red\" /><tractor><multitrack><playlist></playlist><playlist></playlist><playlist /><playlist /><playlist></playlist></multitrack></tractor></mlt>");*/
1025 setSceneList(doc, 0);
1029 int Render::setProducer(Mlt::Producer *producer, int position)
1031 m_refreshTimer.stop();
1032 QMutexLocker locker(&m_mutex);
1034 int consumerPosition = 0;
1035 if (m_winid == -1 || !m_mltConsumer) {
1036 kDebug()<<" / / / / WARNING, MONITOR NOT READY";
1037 if (producer) delete producer;
1040 m_mltConsumer->set("refresh", 0);
1041 if (!m_mltConsumer->is_stopped()) {
1042 m_mltConsumer->stop();
1044 m_mltConsumer->purge();
1045 consumerPosition = m_mltConsumer->position();
1049 if (!producer || !producer->is_valid()) {
1050 if (producer) delete producer;
1051 producer = m_blackClip->cut(0, 1);
1054 if (!producer || !producer->is_valid()) {
1055 kDebug() << " WARNING - - - - -INVALID PLAYLIST: ";
1058 if (m_mltProducer) currentId = m_mltProducer->get("id");
1060 if (position == -1 && producer->get("id") == currentId) position = consumerPosition;
1061 if (position != -1) producer->seek(position);
1062 int volume = KdenliveSettings::volume();
1063 producer->set("meta.volume", (double)volume / 100);
1064 m_fps = producer->get_fps();
1065 blockSignals(false);
1066 m_mltConsumer->connect(*producer);
1068 if (m_mltProducer) {
1069 m_mltProducer->set_speed(0);
1070 delete m_mltProducer;
1071 m_mltProducer = NULL;
1073 m_mltProducer = producer;
1074 m_mltProducer->set_speed(0);
1075 emit durationChanged(m_mltProducer->get_playtime());
1076 if (m_mltConsumer->start() == -1) {
1077 // ARGH CONSUMER BROKEN!!!!
1078 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."));
1079 if (m_showFrameEvent) delete m_showFrameEvent;
1080 m_showFrameEvent = NULL;
1081 if (m_pauseEvent) delete m_pauseEvent;
1082 m_pauseEvent = NULL;
1083 delete m_mltConsumer;
1084 m_mltConsumer = NULL;
1088 position = m_mltProducer->position();
1089 m_mltConsumer->set("refresh", 1);
1090 // Make sure the first frame is displayed, otherwise if we change producer too fast
1091 // We can crash the avformat producer
1092 Mlt::Event *ev = m_mltConsumer->setup_wait_for("consumer-frame-show");
1093 m_mltConsumer->wait_for(ev);
1095 emit rendererPosition(position);
1099 int Render::setSceneList(QDomDocument list, int position)
1101 return setSceneList(list.toString(), position);
1104 int Render::setSceneList(QString playlist, int position)
1106 m_refreshTimer.stop();
1107 QMutexLocker locker(&m_mutex);
1108 if (m_winid == -1) return -1;
1111 //kDebug() << "////// RENDER, SET SCENE LIST:\n" << playlist <<"\n..........:::.";
1113 // Remove previous profile info
1115 doc.setContent(playlist);
1116 QDomElement profile = doc.documentElement().firstChildElement("profile");
1117 doc.documentElement().removeChild(profile);
1118 playlist = doc.toString();
1120 if (m_mltConsumer) {
1121 if (!m_mltConsumer->is_stopped()) {
1122 m_mltConsumer->stop();
1124 m_mltConsumer->set("refresh", 0);
1126 kWarning() << "/////// ERROR, TRYING TO USE NULL MLT CONSUMER";
1129 m_requestList.clear();
1130 m_infoThread.waitForFinished();
1132 if (m_mltProducer) {
1133 m_mltProducer->set_speed(0);
1134 //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
1136 /*Mlt::Service service(m_mltProducer->parent().get_service());
1139 if (service.type() == tractor_type) {
1140 Mlt::Tractor tractor(service);
1141 Mlt::Field *field = tractor.field();
1142 mlt_service nextservice = mlt_service_get_producer(service.get_service());
1143 mlt_service nextservicetodisconnect;
1144 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
1145 QString mlt_type = mlt_properties_get(properties, "mlt_type");
1146 QString resource = mlt_properties_get(properties, "mlt_service");
1147 // Delete all transitions
1148 while (mlt_type == "transition") {
1149 nextservicetodisconnect = nextservice;
1150 nextservice = mlt_service_producer(nextservice);
1151 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
1152 if (nextservice == NULL) break;
1153 properties = MLT_SERVICE_PROPERTIES(nextservice);
1154 mlt_type = mlt_properties_get(properties, "mlt_type");
1155 resource = mlt_properties_get(properties, "mlt_service");
1159 for (int trackNb = tractor.count() - 1; trackNb >= 0; --trackNb) {
1160 Mlt::Producer trackProducer(tractor.track(trackNb));
1161 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1162 if (trackPlaylist.type() == playlist_type) trackPlaylist.clear();
1168 qDeleteAll(m_slowmotionProducers.values());
1169 m_slowmotionProducers.clear();
1171 delete m_mltProducer;
1172 m_mltProducer = NULL;
1177 m_locale = QLocale();
1179 m_mltProducer = new Mlt::Producer(*m_mltProfile, "xml-string", playlist.toUtf8().constData());
1180 if (!m_mltProducer || !m_mltProducer->is_valid()) {
1181 kDebug() << " WARNING - - - - -INVALID PLAYLIST: " << playlist.toUtf8().constData();
1182 m_mltProducer = m_blackClip->cut(0, 1);
1186 int volume = KdenliveSettings::volume();
1187 m_mltProducer->set("meta.volume", (double)volume / 100);
1188 m_mltProducer->optimise();
1190 /*if (KdenliveSettings::osdtimecode()) {
1191 // Attach filter for on screen display of timecode
1193 QString attr = "attr_check";
1194 mlt_filter filter = mlt_factory_filter( "data_feed", (char*) attr.ascii() );
1195 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
1196 mlt_producer_attach( m_mltProducer->get_producer(), filter );
1197 mlt_filter_close( filter );
1199 m_osdInfo = new Mlt::Filter("data_show");
1200 m_osdInfo->set("resource", m_osdProfile.toUtf8().constData());
1201 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1202 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
1203 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
1204 m_osdInfo->set("dynamic", "1");
1206 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1208 m_osdInfo->set("dynamic", "0");
1211 m_fps = m_mltProducer->get_fps();
1212 if (position != 0) {
1213 // Seek to correct place after opening project.
1214 m_mltProducer->seek(position);
1217 kDebug() << "// NEW SCENE LIST DURATION SET TO: " << m_mltProducer->get_playtime();
1218 m_mltConsumer->connect(*m_mltProducer);
1219 m_mltProducer->set_speed(0);
1220 fillSlowMotionProducers();
1221 blockSignals(false);
1222 emit durationChanged(m_mltProducer->get_playtime());
1225 //kDebug()<<"// SETSCN LST, POS: "<<position;
1226 //if (position != 0) emit rendererPosition(position);
1229 void Render::checkMaxThreads()
1231 // Make sure we don't use too much threads, MLT avformat does not cope with too much threads
1232 // Currently, Kdenlive uses the following avformat threads:
1233 // One thread to get info when adding a clip
1234 // One thread to create the timeline video thumbnails
1235 // One thread to create the audio thumbnails
1236 Mlt::Service service(m_mltProducer->parent().get_service());
1237 if (service.type() != tractor_type) {
1238 kWarning() << "// TRACTOR PROBLEM";
1241 Mlt::Tractor tractor(service);
1242 int mltMaxThreads = mlt_service_cache_get_size(service.get_service(), "producer_avformat");
1243 int requestedThreads = tractor.count() + 4;
1244 if (requestedThreads > mltMaxThreads) {
1245 mlt_service_cache_set_size(service.get_service(), "producer_avformat", requestedThreads);
1246 kDebug()<<"// MLT threads updated to: "<<mlt_service_cache_get_size(service.get_service(), "producer_avformat");
1250 const QString Render::sceneList()
1253 Mlt::Profile profile((mlt_profile) 0);
1254 Mlt::Consumer xmlConsumer(profile, "xml:kdenlive_playlist");
1255 if (!xmlConsumer.is_valid()) return QString();
1256 m_mltProducer->optimise();
1257 xmlConsumer.set("terminate_on_pause", 1);
1258 Mlt::Producer prod(m_mltProducer->get_producer());
1259 if (!prod.is_valid()) return QString();
1260 bool split = m_isSplitView;
1261 if (split) slotSplitView(false);
1262 xmlConsumer.connect(prod);
1264 playlist = QString::fromUtf8(xmlConsumer.get("kdenlive_playlist"));
1265 if (split) slotSplitView(true);
1269 bool Render::saveSceneList(QString path, QDomElement kdenliveData)
1273 doc.setContent(sceneList(), false);
1274 if (doc.isNull()) return false;
1275 QDomElement root = doc.documentElement();
1276 if (!kdenliveData.isNull() && !root.isNull()) {
1277 // add Kdenlive specific tags
1278 root.appendChild(doc.importNode(kdenliveData, true));
1280 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
1281 kWarning() << "////// ERROR writing to file: " << path;
1284 file.write(doc.toString().toUtf8());
1285 if (file.error() != QFile::NoError) {
1293 void Render::saveZone(KUrl url, QString desc, QPoint zone)
1295 Mlt::Consumer xmlConsumer(*m_mltProfile, ("xml:" + url.path()).toUtf8().constData());
1296 m_mltProducer->optimise();
1297 xmlConsumer.set("terminate_on_pause", 1);
1298 if (m_name == Kdenlive::clipMonitor) {
1299 Mlt::Producer *prod = m_mltProducer->cut(zone.x(), zone.y());
1301 list.insert_at(0, prod, 0);
1303 list.set("title", desc.toUtf8().constData());
1304 xmlConsumer.connect(list);
1307 //TODO: not working yet, save zone from timeline
1308 Mlt::Producer *p1 = new Mlt::Producer(m_mltProducer->get_producer());
1309 /* Mlt::Service service(p1->parent().get_service());
1310 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";*/
1312 //Mlt::Producer *prod = p1->cut(zone.x(), zone.y());
1313 //prod->set("title", desc.toUtf8().constData());
1314 xmlConsumer.connect(*p1); //list);
1317 xmlConsumer.start();
1320 double Render::fps() const
1325 int Render::volume() const
1327 if (!m_mltConsumer || !m_mltProducer) return -1;
1328 return ((int) 100 * m_mltProducer->get_double("meta.volume"));
1331 void Render::slotSetVolume(int volume)
1333 if (!m_mltConsumer || !m_mltProducer) return;
1334 m_mltProducer->set("meta.volume", (double)volume / 100.0);
1337 m_mltConsumer->set("refresh", 0);
1338 // Attach filter for on screen display of timecode
1339 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1340 mlt_properties_set_double( properties, "meta.volume", volume );
1341 mlt_properties_set_int( properties, "meta.attr.osdvolume", 1);
1342 mlt_properties_set( properties, "meta.attr.osdvolume.markup", i18n("Volume: ") + QString::number(volume * 100));
1344 if (!KdenliveSettings::osdtimecode()) {
1345 m_mltProducer->detach(*m_osdInfo);
1346 mlt_properties_set_int( properties, "meta.attr.timecode", 0);
1347 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1350 //m_osdTimer->setSingleShot(2500);
1353 void Render::slotOsdTimeout()
1355 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1356 mlt_properties_set_int(properties, "meta.attr.osdvolume", 0);
1357 mlt_properties_set(properties, "meta.attr.osdvolume.markup", NULL);
1358 //if (!KdenliveSettings::osdtimecode()) m_mltProducer->detach(*m_osdInfo);
1362 void Render::start()
1364 m_refreshTimer.stop();
1365 QMutexLocker locker(&m_mutex);
1366 if (m_winid == -1) {
1367 kDebug() << "----- BROKEN MONITOR: " << m_name << ", RESTART";
1370 if (!m_mltConsumer) return;
1371 if (m_mltConsumer->is_stopped()) {
1372 if (m_mltConsumer->start() == -1) {
1373 //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."));
1374 kDebug(QtWarningMsg) << "/ / / / CANNOT START MONITOR";
1376 m_mltConsumer->purge();
1377 m_mltConsumer->set("refresh", 1);
1384 m_refreshTimer.stop();
1385 QMutexLocker locker(&m_mutex);
1386 if (m_mltProducer == NULL) return;
1387 if (m_mltConsumer && !m_mltConsumer->is_stopped()) {
1388 m_mltConsumer->stop();
1389 m_mltConsumer->purge();
1392 if (m_mltProducer) {
1393 if (m_isZoneMode) resetZoneMode();
1394 m_mltProducer->set_speed(0.0);
1398 void Render::stop(const GenTime & startTime)
1400 m_refreshTimer.stop();
1401 QMutexLocker locker(&m_mutex);
1402 if (m_mltProducer) {
1403 if (m_isZoneMode) resetZoneMode();
1404 m_mltProducer->set_speed(0.0);
1405 m_mltProducer->seek((int) startTime.frames(m_fps));
1407 m_mltConsumer->purge();
1410 void Render::pause()
1412 if (!m_mltProducer || !m_mltConsumer)
1414 if (m_mltProducer->get_speed() == 0.0) return;
1415 if (m_isZoneMode) resetZoneMode();
1416 m_mltConsumer->set("refresh", 0);
1417 m_mltProducer->set_speed(0.0);
1418 m_mltConsumer->purge();
1421 void Render::switchPlay(bool play)
1423 QMutexLocker locker(&m_mutex);
1424 if (!m_mltProducer || !m_mltConsumer)
1426 if (m_isZoneMode) resetZoneMode();
1427 if (play && m_mltProducer->get_speed() == 0.0) {
1428 if (m_name == Kdenlive::clipMonitor && m_mltConsumer->position() == m_mltProducer->get_out()) m_mltProducer->seek(0);
1429 if (m_mltConsumer->is_stopped()) {
1430 m_mltConsumer->start();
1432 m_mltProducer->set_speed(1.0);
1433 m_mltConsumer->set("refresh", 1);
1435 m_mltProducer->set_speed(0.0);
1436 m_mltConsumer->set("refresh", 0);
1437 m_mltProducer->seek(m_mltConsumer->position());
1438 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
1439 if (m_isZoneMode) resetZoneMode();
1441 //emitConsumerStopped();
1442 /*m_mltConsumer->set("refresh", 0);
1443 m_mltConsumer->stop();
1444 m_mltConsumer->purge();
1445 m_mltProducer->set_speed(0.0);
1446 //m_framePosition = m_mltProducer->position();
1447 m_mltProducer->seek(m_framePosition);
1448 emit rendererPosition(m_framePosition);*/
1452 void Render::play(double speed)
1456 // if (speed == 0.0) m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1457 m_mltProducer->set_speed(speed);
1458 /*if (speed == 0.0) {
1459 m_mltProducer->seek((int) m_framePosition + 1);
1460 m_mltConsumer->purge();
1465 void Render::play(const GenTime & startTime)
1467 if (!m_mltProducer || !m_mltConsumer)
1469 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1470 m_mltProducer->set_speed(1.0);
1471 m_mltConsumer->set("refresh", 1);
1474 void Render::loopZone(const GenTime & startTime, const GenTime & stopTime)
1476 if (!m_mltProducer || !m_mltConsumer)
1478 //m_mltProducer->set("eof", "loop");
1479 m_isLoopMode = true;
1480 m_loopStart = startTime;
1481 playZone(startTime, stopTime);
1484 void Render::playZone(const GenTime & startTime, const GenTime & stopTime)
1486 if (!m_mltProducer || !m_mltConsumer)
1488 if (!m_isZoneMode) m_originalOut = m_mltProducer->get_playtime() - 1;
1489 m_mltProducer->set("out", (int)(stopTime.frames(m_fps)));
1490 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1491 m_mltProducer->set_speed(1.0);
1492 m_mltConsumer->set("refresh", 1);
1493 if (m_mltConsumer->is_stopped()) m_mltConsumer->start();
1494 m_isZoneMode = true;
1497 void Render::resetZoneMode()
1499 if (!m_isZoneMode && !m_isLoopMode) return;
1500 m_mltProducer->set("out", m_originalOut);
1501 //m_mltProducer->set("eof", "pause");
1502 m_isZoneMode = false;
1503 m_isLoopMode = false;
1506 void Render::seekToFrame(int pos)
1511 m_mltProducer->seek(pos);
1512 if (m_mltProducer->get_speed() == 0) {
1517 void Render::seekToFrameDiff(int diff)
1522 m_mltProducer->seek(m_mltProducer->position() + diff);
1526 void Render::doRefresh()
1528 if (m_mltProducer && m_mltProducer->get_speed() == 0) m_refreshTimer.start();
1531 void Render::refresh()
1533 QMutexLocker locker(&m_mutex);
1536 if (m_mltConsumer) {
1537 if (m_mltConsumer->is_stopped()) m_mltConsumer->start();
1538 m_mltConsumer->purge();
1539 m_mltConsumer->set("refresh", 1);
1543 void Render::setDropFrames(bool show)
1545 QMutexLocker locker(&m_mutex);
1546 if (m_mltConsumer) {
1547 int dropFrames = KdenliveSettings::mltthreads();
1548 if (show == false) dropFrames = -dropFrames;
1549 m_mltConsumer->stop();
1551 m_mltConsumer->set("real_time", dropFrames);
1553 m_mltConsumer->set("play.real_time", dropFrames);
1555 if (m_mltConsumer->start() == -1) {
1556 kDebug(QtWarningMsg) << "ERROR, Cannot start monitor";
1562 double Render::playSpeed() const
1564 if (m_mltProducer) return m_mltProducer->get_speed();
1568 GenTime Render::seekPosition() const
1570 if (m_mltProducer) return GenTime((int) m_mltProducer->position(), m_fps);
1571 else return GenTime();
1574 int Render::seekFramePosition() const
1576 //if (m_mltProducer) return (int) m_mltProducer->position();
1577 if (m_mltConsumer) return (int) m_mltConsumer->position();
1581 void Render::emitFrameUpdated(Mlt::Frame& frame)
1583 mlt_image_format format = mlt_image_rgb24a;
1586 const uchar* image = frame.get_image(format, width, height);
1587 QImage qimage(width, height, QImage::Format_ARGB32_Premultiplied);
1588 memcpy(qimage.scanLine(0), image, width * height * 4);
1590 /*mlt_image_format format = mlt_image_rgb24;
1593 const uchar* image = frame.get_image(format, width, height);
1594 QImage qimage(width, height, QImage::Format_RGB888);
1595 memcpy(qimage.bits(), image, width * height * 3);*/
1596 emit frameUpdated(qimage.rgbSwapped());
1599 void Render::emitFrameNumber()
1601 if (m_mltConsumer) emit rendererPosition((int) m_mltConsumer->position());
1604 void Render::emitConsumerStopped()
1606 // This is used to know when the playing stopped
1607 if (m_mltProducer) {
1608 double pos = m_mltProducer->position();
1609 if (m_isLoopMode) play(m_loopStart);
1610 //else if (m_isZoneMode) resetZoneMode();
1611 emit rendererStopped((int) pos);
1615 void Render::exportFileToFirewire(QString /*srcFileName*/, int /*port*/, GenTime /*startTime*/, GenTime /*endTime*/)
1617 KMessageBox::sorry(0, i18n("Firewire is not enabled on your system.\n Please install Libiec61883 and recompile Kdenlive"));
1620 void Render::exportCurrentFrame(KUrl url, bool /*notify*/)
1622 if (!m_mltProducer) {
1623 KMessageBox::sorry(qApp->activeWindow(), i18n("There is no clip, cannot extract frame."));
1627 //int height = 1080;//KdenliveSettings::defaultheight();
1628 //int width = 1940; //KdenliveSettings::displaywidth();
1630 QPixmap pix; // = KThumb::getFrame(m_mltProducer, -1, width, height);
1632 QPixmap pix(width, height);
1633 Mlt::Filter m_convert(*m_mltProfile, "avcolour_space");
1634 m_convert.set("forced", mlt_image_rgb24a);
1635 m_mltProducer->attach(m_convert);
1636 Mlt::Frame * frame = m_mltProducer->get_frame();
1637 m_mltProducer->detach(m_convert);
1639 pix = frameThumbnail(frame, width, height);
1642 pix.save(url.path(), "PNG");
1643 //if (notify) QApplication::postEvent(qApp->activeWindow(), new UrlEvent(url, 10003));
1647 void Render::showFrame(Mlt::Frame& frame)
1649 emit rendererPosition((int) m_mltConsumer->position());
1650 mlt_image_format format = mlt_image_rgb24a;
1653 const uchar* image = frame.get_image(format, width, height);
1654 QImage qimage(width, height, QImage::Format_ARGB32_Premultiplied);
1655 memcpy(qimage.scanLine(0), image, width * height * 4);
1656 emit showImageSignal(qimage);
1657 if (analyseAudio) showAudio(frame);
1658 if (sendFrameForAnalysis && frame.get_frame()->convert_image) {
1659 emit frameUpdated(qimage.rgbSwapped());
1663 void Render::showAudio(Mlt::Frame& frame)
1665 if (!frame.is_valid() || frame.get_int("test_audio") != 0) {
1668 mlt_audio_format audio_format = mlt_audio_s16;
1670 int num_channels = 0;
1672 int16_t* data = (int16_t*)frame.get_audio(audio_format, freq, num_channels, samples);
1678 // Data format: [ c00 c10 c01 c11 c02 c12 c03 c13 ... c0{samples-1} c1{samples-1} for 2 channels.
1679 // So the vector is of size samples*channels.
1680 QVector<int16_t> sampleVector(samples*num_channels);
1681 memcpy(sampleVector.data(), data, samples*num_channels*sizeof(int16_t));
1684 emit audioSamplesSignal(sampleVector, freq, num_channels, samples);
1689 * MLT playlist direct manipulation.
1692 void Render::mltCheckLength(Mlt::Tractor *tractor)
1694 //kDebug()<<"checking track length: "<<track<<"..........";
1696 int trackNb = tractor->count();
1700 Mlt::Producer trackProducer(tractor->track(0));
1701 duration = trackProducer.get_playtime() - 1;
1702 m_mltProducer->set("out", duration);
1703 emit durationChanged(duration);
1706 while (trackNb > 1) {
1707 Mlt::Producer trackProducer(tractor->track(trackNb - 1));
1708 trackDuration = trackProducer.get_playtime() - 1;
1709 // kDebug() << " / / /DURATON FOR TRACK " << trackNb - 1 << " = " << trackDuration;
1710 if (trackDuration > duration) duration = trackDuration;
1714 Mlt::Producer blackTrackProducer(tractor->track(0));
1716 if (blackTrackProducer.get_playtime() - 1 != duration) {
1717 Mlt::Playlist blackTrackPlaylist((mlt_playlist) blackTrackProducer.get_service());
1718 Mlt::Producer *blackclip = blackTrackPlaylist.get_clip(0);
1719 if (blackclip && blackclip->is_blank()) {
1724 if (blackclip == NULL || blackTrackPlaylist.count() != 1) {
1725 if (blackclip) delete blackclip;
1726 blackTrackPlaylist.clear();
1727 m_blackClip->set("length", duration + 1);
1728 m_blackClip->set("out", duration);
1729 blackclip = m_blackClip->cut(0, duration);
1730 blackTrackPlaylist.insert_at(0, blackclip, 1);
1732 if (duration > blackclip->parent().get_length()) {
1733 blackclip->parent().set("length", duration + 1);
1734 blackclip->parent().set("out", duration);
1735 blackclip->set("length", duration + 1);
1737 blackTrackPlaylist.resize_clip(0, 0, duration);
1741 m_mltProducer->set("out", duration);
1742 emit durationChanged(duration);
1746 Mlt::Producer *Render::checkSlowMotionProducer(Mlt::Producer *prod, QDomElement element)
1748 if (element.attribute("speed", "1.0").toDouble() == 1.0 && element.attribute("strobe", "1").toInt() == 1) return prod;
1750 // We want a slowmotion producer
1751 double speed = element.attribute("speed", "1.0").toDouble();
1752 int strobe = element.attribute("strobe", "1").toInt();
1753 QString url = QString::fromUtf8(prod->get("resource"));
1754 url.append('?' + locale.toString(speed));
1755 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
1756 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1757 if (!slowprod || slowprod->get_producer() == NULL) {
1758 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
1759 if (strobe > 1) slowprod->set("strobe", strobe);
1760 QString id = prod->parent().get("id");
1761 if (id.contains('_')) id = id.section('_', 0, 0);
1762 QString producerid = "slowmotion:" + id + ':' + locale.toString(speed);
1763 if (strobe > 1) producerid.append(':' + QString::number(strobe));
1764 slowprod->set("id", producerid.toUtf8().constData());
1765 m_slowmotionProducers.insert(url, slowprod);
1770 int Render::mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod, bool overwrite, bool push)
1772 if (m_mltProducer == NULL) {
1773 kDebug() << "PLAYLIST NOT INITIALISED //////";
1777 kDebug() << "Cannot insert clip without producer //////";
1780 Mlt::Producer parentProd(m_mltProducer->parent());
1781 if (parentProd.get_producer() == NULL) {
1782 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1786 Mlt::Service service(parentProd.get_service());
1787 if (service.type() != tractor_type) {
1788 kWarning() << "// TRACTOR PROBLEM";
1791 Mlt::Tractor tractor(service);
1792 if (info.track > tractor.count() - 1) {
1793 kDebug() << "ERROR TRYING TO INSERT CLIP ON TRACK " << info.track << ", at POS: " << info.startPos.frames(25);
1797 Mlt::Producer trackProducer(tractor.track(info.track));
1798 int trackDuration = trackProducer.get_playtime() - 1;
1799 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1800 //kDebug()<<"/// INSERT cLIP: "<<info.cropStart.frames(m_fps)<<", "<<info.startPos.frames(m_fps)<<"-"<<info.endPos.frames(m_fps);
1801 prod = checkSlowMotionProducer(prod, element);
1802 if (prod == NULL || !prod->is_valid()) {
1807 int cutPos = (int) info.cropStart.frames(m_fps);
1808 if (cutPos < 0) cutPos = 0;
1809 int insertPos = (int) info.startPos.frames(m_fps);
1810 int cutDuration = (int)(info.endPos - info.startPos).frames(m_fps) - 1;
1811 Mlt::Producer *clip = prod->cut(cutPos, cutDuration + cutPos);
1812 if (overwrite && (insertPos < trackDuration)) {
1813 // Replace zone with blanks
1814 //trackPlaylist.split_at(insertPos, true);
1815 trackPlaylist.remove_region(insertPos, cutDuration + 1);
1816 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1817 trackPlaylist.insert_blank(clipIndex, cutDuration);
1819 trackPlaylist.split_at(insertPos, true);
1820 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1821 trackPlaylist.insert_blank(clipIndex, cutDuration);
1823 int newIndex = trackPlaylist.insert_at(insertPos, clip, 1);
1825 /*if (QString(prod->get("transparency")).toInt() == 1)
1826 mltAddClipTransparency(info, info.track - 1, QString(prod->get("id")).toInt());*/
1828 if (info.track != 0 && (newIndex + 1 == trackPlaylist.count())) mltCheckLength(&tractor);
1830 /*tractor.multitrack()->refresh();
1831 tractor.refresh();*/
1836 void Render::mltCutClip(int track, GenTime position)
1838 Mlt::Service service(m_mltProducer->parent().get_service());
1839 if (service.type() != tractor_type) {
1840 kWarning() << "// TRACTOR PROBLEM";
1844 Mlt::Tractor tractor(service);
1845 Mlt::Producer trackProducer(tractor.track(track));
1846 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1849 /* // Display playlist info
1850 kDebug()<<"//////////// BEFORE";
1851 for (int i = 0; i < trackPlaylist.count(); i++) {
1852 int blankStart = trackPlaylist.clip_start(i);
1853 int blankDuration = trackPlaylist.clip_length(i) - 1;
1855 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1856 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1859 int cutPos = (int) position.frames(m_fps);
1861 int clipIndex = trackPlaylist.get_clip_index_at(cutPos);
1862 if (trackPlaylist.is_blank(clipIndex)) {
1863 kDebug() << "// WARNING, TRYING TO CUT A BLANK";
1867 int clipStart = trackPlaylist.clip_start(clipIndex);
1868 trackPlaylist.split(clipIndex, cutPos - clipStart - 1);
1871 // duplicate effects
1872 Mlt::Producer *original = trackPlaylist.get_clip_at(clipStart);
1873 Mlt::Producer *clip = trackPlaylist.get_clip_at(cutPos);
1875 if (original == NULL || clip == NULL) {
1876 kDebug() << "// ERROR GRABBING CLIP AFTER SPLIT";
1878 Mlt::Service clipService(original->get_service());
1879 Mlt::Service dupService(clip->get_service());
1883 Mlt::Filter *filter = clipService.filter(ct);
1885 // Only duplicate Kdenlive filters, and skip the fade in effects
1886 if (filter->is_valid() && strcmp(filter->get("kdenlive_id"), "") && strcmp(filter->get("kdenlive_id"), "fadein") && strcmp(filter->get("kdenlive_id"), "fade_from_black")) {
1887 // looks like there is no easy way to duplicate a filter,
1888 // so we will create a new one and duplicate its properties
1889 Mlt::Filter *dup = new Mlt::Filter(*m_mltProfile, filter->get("mlt_service"));
1890 if (dup && dup->is_valid()) {
1891 Mlt::Properties entries(filter->get_properties());
1892 for (int i = 0; i < entries.count(); i++) {
1893 dup->set(entries.get_name(i), entries.get(i));
1895 dupService.attach(*dup);
1899 filter = clipService.filter(ct);
1902 /* // Display playlist info
1903 kDebug()<<"//////////// AFTER";
1904 for (int i = 0; i < trackPlaylist.count(); i++) {
1905 int blankStart = trackPlaylist.clip_start(i);
1906 int blankDuration = trackPlaylist.clip_length(i) - 1;
1908 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1909 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1914 Mlt::Tractor *Render::lockService()
1916 // we are going to replace some clips, purge consumer
1917 QMutexLocker locker(&m_mutex);
1918 if (!m_mltProducer) return NULL;
1919 if (m_mltConsumer) {
1920 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
1921 m_mltConsumer->purge();
1923 Mlt::Service service(m_mltProducer->parent().get_service());
1924 if (service.type() != tractor_type) {
1925 kWarning() << "// TRACTOR PROBLEM";
1929 return new Mlt::Tractor(service);
1933 void Render::unlockService(Mlt::Tractor *tractor)
1938 if (!m_mltProducer) return;
1939 Mlt::Service service(m_mltProducer->parent().get_service());
1940 if (service.type() != tractor_type) {
1941 kWarning() << "// TRACTOR PROBLEM";
1947 bool Render::mltUpdateClip(Mlt::Tractor *tractor, ItemInfo info, QDomElement element, Mlt::Producer *prod)
1950 if (prod == NULL || tractor == NULL) {
1951 kDebug() << "Cannot update clip with null producer //////";
1955 Mlt::Producer trackProducer(tractor->track(tractor->count() - 1 - info.track));
1956 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1957 int startPos = info.startPos.frames(m_fps);
1958 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
1959 if (trackPlaylist.is_blank(clipIndex)) {
1960 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << startPos;
1963 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
1965 QList <Mlt::Filter *> filtersList;
1966 Mlt::Service sourceService(clip->get_service());
1968 Mlt::Filter *filter = sourceService.filter(ct);
1970 if (filter->get_int("kdenlive_ix") != 0) {
1971 filtersList.append(filter);
1974 filter = sourceService.filter(ct);
1977 clip = trackPlaylist.replace_with_blank(clipIndex);
1979 prod = checkSlowMotionProducer(prod, element);
1980 if (prod == NULL || !prod->is_valid()) {
1984 Mlt::Producer *clip2 = prod->cut(info.cropStart.frames(m_fps), (info.cropDuration + info.cropStart).frames(m_fps) - 1);
1985 trackPlaylist.insert_at(info.startPos.frames(m_fps), clip2, 1);
1986 Mlt::Service destService(clip2->get_service());
1989 if (!filtersList.isEmpty()) {
1990 for (int i = 0; i < filtersList.count(); i++)
1991 destService.attach(*(filtersList.at(i)));
1997 bool Render::mltRemoveClip(int track, GenTime position)
1999 Mlt::Service service(m_mltProducer->parent().get_service());
2000 if (service.type() != tractor_type) {
2001 kWarning() << "// TRACTOR PROBLEM";
2005 Mlt::Tractor tractor(service);
2006 Mlt::Producer trackProducer(tractor.track(track));
2007 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2008 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2010 if (trackPlaylist.is_blank(clipIndex)) {
2011 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << position.frames(m_fps);
2015 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2016 if (clip) delete clip;
2017 trackPlaylist.consolidate_blanks(0);
2019 /* // Display playlist info
2020 kDebug()<<"//// AFTER";
2021 for (int i = 0; i < trackPlaylist.count(); i++) {
2022 int blankStart = trackPlaylist.clip_start(i);
2023 int blankDuration = trackPlaylist.clip_length(i) - 1;
2025 if (trackPlaylist.is_blank(i)) blk = "(blank)";
2026 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
2029 if (track != 0 && trackPlaylist.count() <= clipIndex) mltCheckLength(&tractor);
2033 int Render::mltGetSpaceLength(const GenTime &pos, int track, bool fromBlankStart)
2035 if (!m_mltProducer) {
2036 kDebug() << "PLAYLIST NOT INITIALISED //////";
2039 Mlt::Producer parentProd(m_mltProducer->parent());
2040 if (parentProd.get_producer() == NULL) {
2041 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
2045 Mlt::Service service(parentProd.get_service());
2046 Mlt::Tractor tractor(service);
2047 int insertPos = pos.frames(m_fps);
2049 Mlt::Producer trackProducer(tractor.track(track));
2050 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2051 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
2052 if (clipIndex == trackPlaylist.count()) {
2053 // We are after the end of the playlist
2056 if (!trackPlaylist.is_blank(clipIndex)) return 0;
2057 if (fromBlankStart) return trackPlaylist.clip_length(clipIndex);
2058 return trackPlaylist.clip_length(clipIndex) + trackPlaylist.clip_start(clipIndex) - insertPos;
2061 int Render::mltTrackDuration(int track)
2063 if (!m_mltProducer) {
2064 kDebug() << "PLAYLIST NOT INITIALISED //////";
2067 Mlt::Producer parentProd(m_mltProducer->parent());
2068 if (parentProd.get_producer() == NULL) {
2069 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
2073 Mlt::Service service(parentProd.get_service());
2074 Mlt::Tractor tractor(service);
2076 Mlt::Producer trackProducer(tractor.track(track));
2077 return trackProducer.get_playtime() - 1;
2080 void Render::mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime &duration, const GenTime &timeOffset)
2082 if (!m_mltProducer) {
2083 kDebug() << "PLAYLIST NOT INITIALISED //////";
2086 Mlt::Producer parentProd(m_mltProducer->parent());
2087 if (parentProd.get_producer() == NULL) {
2088 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
2091 //kDebug()<<"// CLP STRT LST: "<<trackClipStartList;
2092 //kDebug()<<"// TRA STRT LST: "<<trackTransitionStartList;
2094 Mlt::Service service(parentProd.get_service());
2095 Mlt::Tractor tractor(service);
2097 int diff = duration.frames(m_fps);
2098 int offset = timeOffset.frames(m_fps);
2102 // insert space in one track only
2103 Mlt::Producer trackProducer(tractor.track(track));
2104 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2105 insertPos = trackClipStartList.value(track);
2106 if (insertPos != -1) {
2107 insertPos += offset;
2108 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
2110 trackPlaylist.insert_blank(clipIndex, diff - 1);
2112 if (!trackPlaylist.is_blank(clipIndex)) clipIndex --;
2113 if (!trackPlaylist.is_blank(clipIndex)) {
2114 kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
2116 int position = trackPlaylist.clip_start(clipIndex);
2117 int blankDuration = trackPlaylist.clip_length(clipIndex);
2118 if (blankDuration + diff == 0) {
2119 trackPlaylist.remove(clipIndex);
2120 } else trackPlaylist.remove_region(position, -diff);
2122 trackPlaylist.consolidate_blanks(0);
2124 // now move transitions
2125 mlt_service serv = m_mltProducer->parent().get_service();
2126 mlt_service nextservice = mlt_service_get_producer(serv);
2127 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2128 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2129 QString resource = mlt_properties_get(properties, "mlt_service");
2131 while (mlt_type == "transition") {
2132 mlt_transition tr = (mlt_transition) nextservice;
2133 int currentTrack = mlt_transition_get_b_track(tr);
2134 int currentIn = (int) mlt_transition_get_in(tr);
2135 int currentOut = (int) mlt_transition_get_out(tr);
2136 insertPos = trackTransitionStartList.value(track);
2137 if (insertPos != -1) {
2138 insertPos += offset;
2139 if (track == currentTrack && currentOut > insertPos && resource != "mix") {
2140 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
2143 nextservice = mlt_service_producer(nextservice);
2144 if (nextservice == NULL) break;
2145 properties = MLT_SERVICE_PROPERTIES(nextservice);
2146 mlt_type = mlt_properties_get(properties, "mlt_type");
2147 resource = mlt_properties_get(properties, "mlt_service");
2150 for (int trackNb = tractor.count() - 1; trackNb >= 1; --trackNb) {
2151 Mlt::Producer trackProducer(tractor.track(trackNb));
2152 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2154 //int clipNb = trackPlaylist.count();
2155 insertPos = trackClipStartList.value(trackNb);
2156 if (insertPos != -1) {
2157 insertPos += offset;
2159 /* kDebug()<<"-------------\nTRACK "<<trackNb<<" HAS "<<clipNb<<" CLPIS";
2160 kDebug() << "INSERT SPACE AT: "<<insertPos<<", DIFF: "<<diff<<", TK: "<<trackNb;
2161 for (int i = 0; i < clipNb; i++) {
2162 kDebug()<<"CLIP "<<i<<", START: "<<trackPlaylist.clip_start(i)<<", END: "<<trackPlaylist.clip_start(i) + trackPlaylist.clip_length(i);
2163 if (trackPlaylist.is_blank(i)) kDebug()<<"++ BLANK ++ ";
2164 kDebug()<<"-------------";
2166 kDebug()<<"END-------------";*/
2169 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
2171 trackPlaylist.insert_blank(clipIndex, diff - 1);
2173 if (!trackPlaylist.is_blank(clipIndex)) {
2176 if (!trackPlaylist.is_blank(clipIndex)) {
2177 kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
2179 int position = trackPlaylist.clip_start(clipIndex);
2180 int blankDuration = trackPlaylist.clip_length(clipIndex);
2181 if (diff + blankDuration == 0) {
2182 trackPlaylist.remove(clipIndex);
2183 } else trackPlaylist.remove_region(position, - diff);
2185 trackPlaylist.consolidate_blanks(0);
2188 // now move transitions
2189 mlt_service serv = m_mltProducer->parent().get_service();
2190 mlt_service nextservice = mlt_service_get_producer(serv);
2191 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2192 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2193 QString resource = mlt_properties_get(properties, "mlt_service");
2195 while (mlt_type == "transition") {
2196 mlt_transition tr = (mlt_transition) nextservice;
2197 int currentIn = (int) mlt_transition_get_in(tr);
2198 int currentOut = (int) mlt_transition_get_out(tr);
2199 int currentTrack = mlt_transition_get_b_track(tr);
2200 insertPos = trackTransitionStartList.value(currentTrack);
2201 if (insertPos != -1) {
2202 insertPos += offset;
2203 if (currentOut > insertPos && resource != "mix") {
2204 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
2207 nextservice = mlt_service_producer(nextservice);
2208 if (nextservice == NULL) break;
2209 properties = MLT_SERVICE_PROPERTIES(nextservice);
2210 mlt_type = mlt_properties_get(properties, "mlt_type");
2211 resource = mlt_properties_get(properties, "mlt_service");
2215 mltCheckLength(&tractor);
2216 m_mltConsumer->set("refresh", 1);
2220 void Render::mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest)
2222 if (source == dest) return;
2223 Mlt::Service sourceService(source->get_service());
2224 Mlt::Service destService(dest->get_service());
2226 // move all effects to the correct producer
2228 Mlt::Filter *filter = sourceService.filter(ct);
2230 if (filter->get_int("kdenlive_ix") != 0) {
2231 sourceService.detach(*filter);
2232 destService.attach(*filter);
2234 filter = sourceService.filter(ct);
2238 int Render::mltChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, double speed, double /*oldspeed*/, int strobe, Mlt::Producer *prod)
2241 Mlt::Service service(m_mltProducer->parent().get_service());
2242 if (service.type() != tractor_type) {
2243 kWarning() << "// TRACTOR PROBLEM";
2247 //kDebug() << "Changing clip speed, set in and out: " << info.cropStart.frames(m_fps) << " to " << (info.endPos - info.startPos).frames(m_fps) - 1;
2248 Mlt::Tractor tractor(service);
2249 Mlt::Producer trackProducer(tractor.track(info.track));
2250 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2251 int startPos = info.startPos.frames(m_fps);
2252 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
2253 int clipLength = trackPlaylist.clip_length(clipIndex);
2255 Mlt::Producer *original = trackPlaylist.get_clip(clipIndex);
2256 if (original == NULL) {
2259 if (!original->is_valid() || original->is_blank()) {
2264 Mlt::Producer clipparent = original->parent();
2265 if (!clipparent.is_valid() || clipparent.is_blank()) {
2271 QString serv = clipparent.get("mlt_service");
2272 QString id = clipparent.get("id");
2273 if (speed <= 0 && speed > -1) speed = 1.0;
2274 //kDebug() << "CLIP SERVICE: " << serv;
2275 if ((serv == "avformat" || serv == "avformat-novalidate") && (speed != 1.0 || strobe > 1)) {
2277 QString url = QString::fromUtf8(clipparent.get("resource"));
2278 url.append('?' + m_locale.toString(speed));
2279 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
2280 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
2281 if (!slowprod || slowprod->get_producer() == NULL) {
2282 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
2283 if (strobe > 1) slowprod->set("strobe", strobe);
2284 QString producerid = "slowmotion:" + id + ':' + m_locale.toString(speed);
2285 if (strobe > 1) producerid.append(':' + QString::number(strobe));
2286 slowprod->set("id", producerid.toUtf8().constData());
2287 // copy producer props
2288 double ar = original->parent().get_double("force_aspect_ratio");
2289 if (ar != 0.0) slowprod->set("force_aspect_ratio", ar);
2290 double fps = original->parent().get_double("force_fps");
2291 if (fps != 0.0) slowprod->set("force_fps", fps);
2292 int threads = original->parent().get_int("threads");
2293 if (threads != 0) slowprod->set("threads", threads);
2294 if (original->parent().get("force_progressive"))
2295 slowprod->set("force_progressive", original->parent().get_int("force_progressive"));
2296 if (original->parent().get("force_tff"))
2297 slowprod->set("force_tff", original->parent().get_int("force_tff"));
2298 int ix = original->parent().get_int("video_index");
2299 if (ix != 0) slowprod->set("video_index", ix);
2300 int colorspace = original->parent().get_int("force_colorspace");
2301 if (colorspace != 0) slowprod->set("force_colorspace", colorspace);
2302 int full_luma = original->parent().get_int("set.force_full_luma");
2303 if (full_luma != 0) slowprod->set("set.force_full_luma", full_luma);
2304 m_slowmotionProducers.insert(url, slowprod);
2306 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2307 trackPlaylist.consolidate_blanks(0);
2309 // Check that the blank space is long enough for our new duration
2310 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2311 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2313 if (clipIndex + 1 < trackPlaylist.count() && (startPos + clipLength / speed > blankEnd)) {
2314 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2315 cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)(info.cropStart.frames(m_fps) / speed + maxLength.frames(m_fps) - 1));
2316 } else cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)((info.cropStart.frames(m_fps) + clipLength) / speed - 1));
2318 // move all effects to the correct producer
2319 mltPasteEffects(clip, cut);
2320 trackPlaylist.insert_at(startPos, cut, 1);
2323 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2324 newLength = trackPlaylist.clip_length(clipIndex);
2326 } else if (speed == 1.0 && strobe < 2) {
2329 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2330 trackPlaylist.consolidate_blanks(0);
2332 // Check that the blank space is long enough for our new duration
2333 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2334 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2337 int originalStart = (int)(speedIndependantInfo.cropStart.frames(m_fps));
2338 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + speedIndependantInfo.cropDuration).frames(m_fps) > blankEnd) {
2339 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2340 cut = prod->cut(originalStart, (int)(originalStart + maxLength.frames(m_fps) - 1));
2341 } else cut = prod->cut(originalStart, (int)(originalStart + speedIndependantInfo.cropDuration.frames(m_fps)) - 1);
2343 // move all effects to the correct producer
2344 mltPasteEffects(clip, cut);
2346 trackPlaylist.insert_at(startPos, cut, 1);
2349 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2350 newLength = trackPlaylist.clip_length(clipIndex);
2353 } else if (serv == "framebuffer") {
2355 QString url = QString::fromUtf8(clipparent.get("resource"));
2356 url = url.section('?', 0, 0);
2357 url.append('?' + m_locale.toString(speed));
2358 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
2359 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
2360 if (!slowprod || slowprod->get_producer() == NULL) {
2361 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
2362 slowprod->set("strobe", strobe);
2363 QString producerid = "slowmotion:" + id.section(':', 1, 1) + ':' + m_locale.toString(speed);
2364 if (strobe > 1) producerid.append(':' + QString::number(strobe));
2365 slowprod->set("id", producerid.toUtf8().constData());
2366 // copy producer props
2367 double ar = original->parent().get_double("force_aspect_ratio");
2368 if (ar != 0.0) slowprod->set("force_aspect_ratio", ar);
2369 double fps = original->parent().get_double("force_fps");
2370 if (fps != 0.0) slowprod->set("force_fps", fps);
2371 if (original->parent().get("force_progressive"))
2372 slowprod->set("force_progressive", original->parent().get_int("force_progressive"));
2373 if (original->parent().get("force_tff"))
2374 slowprod->set("force_tff", original->parent().get_int("force_tff"));
2375 int threads = original->parent().get_int("threads");
2376 if (threads != 0) slowprod->set("threads", threads);
2377 int ix = original->parent().get_int("video_index");
2378 if (ix != 0) slowprod->set("video_index", ix);
2379 int colorspace = original->parent().get_int("force_colorspace");
2380 if (colorspace != 0) slowprod->set("force_colorspace", colorspace);
2381 int full_luma = original->parent().get_int("set.force_full_luma");
2382 if (full_luma != 0) slowprod->set("set.force_full_luma", full_luma);
2383 m_slowmotionProducers.insert(url, slowprod);
2385 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2386 trackPlaylist.consolidate_blanks(0);
2388 GenTime duration = speedIndependantInfo.cropDuration / speed;
2389 int originalStart = (int)(speedIndependantInfo.cropStart.frames(m_fps) / speed);
2391 // Check that the blank space is long enough for our new duration
2392 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2393 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2396 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + duration).frames(m_fps) > blankEnd) {
2397 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2398 cut = slowprod->cut(originalStart, (int)(originalStart + maxLength.frames(m_fps) - 1));
2399 } else cut = slowprod->cut(originalStart, (int)(originalStart + duration.frames(m_fps)) - 1);
2401 // move all effects to the correct producer
2402 mltPasteEffects(clip, cut);
2404 trackPlaylist.insert_at(startPos, cut, 1);
2407 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2408 newLength = trackPlaylist.clip_length(clipIndex);
2413 if (clipIndex + 1 == trackPlaylist.count()) mltCheckLength(&tractor);
2417 bool Render::mltRemoveTrackEffect(int track, int index, bool updateIndex)
2419 Mlt::Service service(m_mltProducer->parent().get_service());
2420 bool success = false;
2421 Mlt::Tractor tractor(service);
2422 Mlt::Producer trackProducer(tractor.track(track));
2423 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2424 Mlt::Service clipService(trackPlaylist.get_service());
2428 Mlt::Filter *filter = clipService.filter(ct);
2430 if ((index == -1 && strcmp(filter->get("kdenlive_id"), "")) || filter->get_int("kdenlive_ix") == index) {
2431 if (clipService.detach(*filter) == 0) success = true;
2432 } else if (updateIndex) {
2433 // Adjust the other effects index
2434 if (filter->get_int("kdenlive_ix") > index) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2437 filter = clipService.filter(ct);
2444 bool Render::mltRemoveEffect(int track, GenTime position, int index, bool updateIndex, bool doRefresh)
2446 if (position < GenTime()) {
2447 // Remove track effect
2448 return mltRemoveTrackEffect(track, index, updateIndex);
2450 Mlt::Service service(m_mltProducer->parent().get_service());
2451 bool success = false;
2452 Mlt::Tractor tractor(service);
2453 Mlt::Producer trackProducer(tractor.track(track));
2454 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2456 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2457 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2459 kDebug() << " / / / CANNOT FIND CLIP TO REMOVE EFFECT";
2463 Mlt::Service clipService(clip->get_service());
2464 int duration = clip->get_playtime();
2466 // Check if clip is visible in monitor
2467 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2468 if (diff < 0 || diff > duration) doRefresh = false;
2474 Mlt::Filter *filter = clipService.filter(ct);
2476 if ((index == -1 && strcmp(filter->get("kdenlive_id"), "")) || filter->get_int("kdenlive_ix") == index) {// && filter->get("kdenlive_id") == id) {
2477 if (clipService.detach(*filter) == 0) success = true;
2478 //kDebug()<<"Deleted filter id:"<<filter->get("kdenlive_id")<<", ix:"<<filter->get("kdenlive_ix")<<", SERVICE:"<<filter->get("mlt_service");
2479 } else if (updateIndex) {
2480 // Adjust the other effects index
2481 if (filter->get_int("kdenlive_ix") > index) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2484 filter = clipService.filter(ct);
2487 if (doRefresh) refresh();
2491 bool Render::mltAddTrackEffect(int track, EffectsParameterList params)
2493 Mlt::Service service(m_mltProducer->parent().get_service());
2494 Mlt::Tractor tractor(service);
2495 Mlt::Producer trackProducer(tractor.track(track));
2496 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2497 Mlt::Service trackService(trackProducer.get_service()); //trackPlaylist
2498 return mltAddEffect(trackService, params, trackProducer.get_playtime() - 1, true);
2502 bool Render::mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh)
2505 Mlt::Service service(m_mltProducer->parent().get_service());
2507 Mlt::Tractor tractor(service);
2508 Mlt::Producer trackProducer(tractor.track(track));
2509 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2511 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2512 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2517 Mlt::Service clipService(clip->get_service());
2518 int duration = clip->get_playtime();
2520 // Check if clip is visible in monitor
2521 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2522 if (diff < 0 || diff > duration) doRefresh = false;
2525 return mltAddEffect(clipService, params, duration, doRefresh);
2528 bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int duration, bool doRefresh)
2530 bool updateIndex = false;
2531 const int filter_ix = params.paramValue("kdenlive_ix").toInt();
2532 const QString region = params.paramValue("region");
2536 Mlt::Filter *filter = service.filter(ct);
2538 if (filter->get_int("kdenlive_ix") == filter_ix) {
2539 // A filter at that position already existed, so we will increase all indexes later
2544 filter = service.filter(ct);
2547 if (params.paramValue("id") == "speed") {
2548 // special case, speed effect is not really inserted, we just update the other effects index (kdenlive_ix)
2550 filter = service.filter(ct);
2552 if (filter->get_int("kdenlive_ix") >= filter_ix) {
2553 if (updateIndex) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") + 1);
2556 filter = service.filter(ct);
2559 if (doRefresh) refresh();
2564 // temporarily remove all effects after insert point
2565 QList <Mlt::Filter *> filtersList;
2567 filter = service.filter(ct);
2569 if (filter->get_int("kdenlive_ix") >= filter_ix) {
2570 filtersList.append(filter);
2571 service.detach(*filter);
2573 filter = service.filter(ct);
2577 QString tag = params.paramValue("tag");
2578 //kDebug() << " / / INSERTING EFFECT: " << tag << ", REGI: " << region;
2579 char *filterTag = qstrdup(tag.toUtf8().constData());
2580 char *filterId = qstrdup(params.paramValue("id").toUtf8().constData());
2581 QHash<QString, QString>::Iterator it;
2582 QString kfr = params.paramValue("keyframes");
2584 if (!kfr.isEmpty()) {
2585 QStringList keyFrames = kfr.split(';', QString::SkipEmptyParts);
2586 //kDebug() << "// ADDING KEYFRAME EFFECT: " << params.paramValue("keyframes");
2587 char *starttag = qstrdup(params.paramValue("starttag", "start").toUtf8().constData());
2588 char *endtag = qstrdup(params.paramValue("endtag", "end").toUtf8().constData());
2589 //kDebug() << "// ADDING KEYFRAME TAGS: " << starttag << ", " << endtag;
2590 //double max = params.paramValue("max").toDouble();
2591 double min = params.paramValue("min").toDouble();
2592 double factor = params.paramValue("factor", "1").toDouble();
2593 double paramOffset = params.paramValue("offset", "0").toDouble();
2594 params.removeParam("starttag");
2595 params.removeParam("endtag");
2596 params.removeParam("keyframes");
2597 params.removeParam("min");
2598 params.removeParam("max");
2599 params.removeParam("factor");
2600 params.removeParam("offset");
2602 // Special case, only one keyframe, means we want a constant value
2603 if (keyFrames.count() == 1) {
2604 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2605 if (filter && filter->is_valid()) {
2606 filter->set("kdenlive_id", filterId);
2607 int x1 = keyFrames.at(0).section(':', 0, 0).toInt();
2608 double y1 = keyFrames.at(0).section(':', 1, 1).toDouble();
2609 for (int j = 0; j < params.count(); j++) {
2610 filter->set(params.at(j).name().toUtf8().constData(), params.at(j).value().toUtf8().constData());
2612 filter->set("in", x1);
2613 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2614 filter->set(starttag, m_locale.toString(((min + y1) - paramOffset) / factor).toUtf8().data());
2615 service.attach(*filter);
2617 } else for (int i = 0; i < keyFrames.size() - 1; ++i) {
2618 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2619 if (filter && filter->is_valid()) {
2620 filter->set("kdenlive_id", filterId);
2621 int x1 = keyFrames.at(i).section(':', 0, 0).toInt() + offset;
2622 double y1 = keyFrames.at(i).section(':', 1, 1).toDouble();
2623 int x2 = keyFrames.at(i + 1).section(':', 0, 0).toInt();
2624 double y2 = keyFrames.at(i + 1).section(':', 1, 1).toDouble();
2625 if (x2 == -1) x2 = duration;
2627 for (int j = 0; j < params.count(); j++) {
2628 filter->set(params.at(j).name().toUtf8().constData(), params.at(j).value().toUtf8().constData());
2631 filter->set("in", x1);
2632 filter->set("out", x2);
2633 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2634 filter->set(starttag, m_locale.toString(((min + y1) - paramOffset) / factor).toUtf8().data());
2635 filter->set(endtag, m_locale.toString(((min + y2) - paramOffset) / factor).toUtf8().data());
2636 service.attach(*filter);
2643 Mlt::Filter *filter;
2645 if (!region.isEmpty()) {
2646 filter = new Mlt::Filter(*m_mltProfile, "region");
2647 } else filter = new Mlt::Filter(*m_mltProfile, filterTag);
2648 if (filter && filter->is_valid()) {
2649 filter->set("kdenlive_id", filterId);
2650 if (!region.isEmpty()) {
2651 filter->set("resource", region.toUtf8().constData());
2652 filter->set("kdenlive_ix", params.paramValue("kdenlive_ix").toUtf8().constData());
2653 filter->set("filter0", filterTag);
2654 prefix = "filter0.";
2655 params.removeParam("id");
2656 params.removeParam("region");
2657 params.removeParam("kdenlive_ix");
2660 kDebug() << "filter is NULL";
2664 params.removeParam("kdenlive_id");
2665 if (params.hasParam("_sync_in_out")) {
2666 // This effect must sync in / out with parent clip
2667 params.removeParam("_sync_in_out");
2668 filter->set_in_and_out(service.get_int("in"), service.get_int("out"));
2671 for (int j = 0; j < params.count(); j++) {
2672 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2676 QString effectArgs = params.paramValue("id").section('_', 1);
2678 params.removeParam("id");
2679 params.removeParam("kdenlive_ix");
2680 params.removeParam("tag");
2681 params.removeParam("disable");
2682 params.removeParam("region");
2684 for (int j = 0; j < params.count(); j++) {
2685 effectArgs.append(' ' + params.at(j).value());
2687 //kDebug() << "SOX EFFECTS: " << effectArgs.simplified();
2688 filter->set("effect", effectArgs.simplified().toUtf8().constData());
2691 // attach filter to the clip
2692 service.attach(*filter);
2697 // re-add following filters
2698 for (int i = 0; i < filtersList.count(); i++) {
2699 Mlt::Filter *filter = filtersList.at(i);
2701 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") + 1);
2702 service.attach(*filter);
2705 if (doRefresh) refresh();
2709 bool Render::mltEditTrackEffect(int track, EffectsParameterList params)
2711 Mlt::Service service(m_mltProducer->parent().get_service());
2712 Mlt::Tractor tractor(service);
2713 Mlt::Producer trackProducer(tractor.track(track));
2714 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2715 Mlt::Service clipService(trackPlaylist.get_service());
2717 QString index = params.paramValue("kdenlive_ix");
2718 QString tag = params.paramValue("tag");
2720 Mlt::Filter *filter = clipService.filter(ct);
2722 if (filter->get_int("kdenlive_ix") == index.toInt()) {
2726 filter = clipService.filter(ct);
2730 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT! " << index << ", " << tag;
2731 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2733 bool success = false;//mltAddTrackEffect(track, params);
2737 QString ser = filter->get("mlt_service");
2738 if (ser == "region") prefix = "filter0.";
2740 for (int j = 0; j < params.count(); j++) {
2741 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2750 bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList params)
2752 int index = params.paramValue("kdenlive_ix").toInt();
2753 QString tag = params.paramValue("tag");
2755 if (!params.paramValue("keyframes").isEmpty() || /*it.key().startsWith("#") || */tag.startsWith("ladspa") || tag == "sox" || tag == "autotrack_rectangle" || params.hasParam("region")) {
2756 // This is a keyframe effect, to edit it, we remove it and re-add it.
2757 bool success = mltRemoveEffect(track, position, index, false);
2758 // if (!success) kDebug() << "// ERROR Removing effect : " << index;
2759 if (position < GenTime())
2760 success = mltAddTrackEffect(track, params);
2762 success = mltAddEffect(track, position, params);
2763 // if (!success) kDebug() << "// ERROR Adding effect : " << index;
2766 if (position < GenTime()) {
2767 return mltEditTrackEffect(track, params);
2770 Mlt::Service service(m_mltProducer->parent().get_service());
2771 Mlt::Tractor tractor(service);
2772 Mlt::Producer trackProducer(tractor.track(track));
2773 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2775 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2776 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2778 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2782 int duration = clip->get_playtime();
2783 bool doRefresh = true;
2784 // Check if clip is visible in monitor
2785 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2786 if (diff < 0 || diff > duration)
2790 Mlt::Filter *filter = clip->filter(ct);
2792 if (filter->get_int("kdenlive_ix") == index) {
2796 filter = clip->filter(ct);
2800 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT! " << index << ", " << tag;
2801 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2803 bool success = mltAddEffect(track, position, params);
2807 QString ser = filter->get("mlt_service");
2808 if (ser == "region") prefix = "filter0.";
2809 if (params.hasParam("_sync_in_out")) {
2810 // This effect must sync in / out with parent clip
2811 params.removeParam("_sync_in_out");
2812 filter->set_in_and_out(clip->get_in(), clip->get_out());
2815 for (int j = 0; j < params.count(); j++) {
2816 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2822 if (doRefresh) refresh();
2826 void Render::mltUpdateEffectPosition(int track, GenTime position, int oldPos, int newPos)
2828 Mlt::Service service(m_mltProducer->parent().get_service());
2829 Mlt::Tractor tractor(service);
2830 Mlt::Producer trackProducer(tractor.track(track));
2831 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2833 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2834 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2836 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2840 Mlt::Service clipService(clip->get_service());
2841 int duration = clip->get_playtime();
2842 bool doRefresh = true;
2843 // Check if clip is visible in monitor
2844 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2845 if (diff < 0 || diff > duration) doRefresh = false;
2849 Mlt::Filter *filter = clipService.filter(ct);
2851 int pos = filter->get_int("kdenlive_ix");
2852 if (pos == oldPos) {
2853 filter->set("kdenlive_ix", newPos);
2855 filter = clipService.filter(ct);
2857 if (doRefresh) refresh();
2860 void Render::mltMoveEffect(int track, GenTime position, int oldPos, int newPos)
2862 if (position < GenTime()) {
2863 mltMoveTrackEffect(track, oldPos, newPos);
2866 Mlt::Service service(m_mltProducer->parent().get_service());
2867 Mlt::Tractor tractor(service);
2868 Mlt::Producer trackProducer(tractor.track(track));
2869 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2871 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2872 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2874 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2878 Mlt::Service clipService(clip->get_service());
2879 int duration = clip->get_playtime();
2880 bool doRefresh = true;
2881 // Check if clip is visible in monitor
2882 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2883 if (diff < 0 || diff > duration) doRefresh = false;
2887 QList <Mlt::Filter *> filtersList;
2888 Mlt::Filter *filter = clipService.filter(ct);
2890 if (newPos > oldPos) {
2892 if (!found && filter->get_int("kdenlive_ix") == oldPos) {
2893 filter->set("kdenlive_ix", newPos);
2894 filtersList.append(filter);
2895 clipService.detach(*filter);
2896 filter = clipService.filter(ct);
2897 while (filter && filter->get_int("kdenlive_ix") <= newPos) {
2898 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2900 filter = clipService.filter(ct);
2904 if (filter && filter->get_int("kdenlive_ix") > newPos) {
2905 filtersList.append(filter);
2906 clipService.detach(*filter);
2908 filter = clipService.filter(ct);
2912 if (filter->get_int("kdenlive_ix") == oldPos) {
2913 filter->set("kdenlive_ix", newPos);
2914 filtersList.append(filter);
2915 clipService.detach(*filter);
2917 filter = clipService.filter(ct);
2921 filter = clipService.filter(ct);
2923 int pos = filter->get_int("kdenlive_ix");
2924 if (pos >= newPos) {
2925 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
2926 filtersList.append(filter);
2927 clipService.detach(*filter);
2929 filter = clipService.filter(ct);
2933 for (int i = 0; i < filtersList.count(); i++) {
2934 clipService.attach(*(filtersList.at(i)));
2937 if (doRefresh) refresh();
2940 void Render::mltMoveTrackEffect(int track, int oldPos, int newPos)
2942 Mlt::Service service(m_mltProducer->parent().get_service());
2943 Mlt::Tractor tractor(service);
2944 Mlt::Producer trackProducer(tractor.track(track));
2945 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2946 Mlt::Service clipService(trackPlaylist.get_service());
2948 QList <Mlt::Filter *> filtersList;
2949 Mlt::Filter *filter = clipService.filter(ct);
2951 if (newPos > oldPos) {
2953 if (!found && filter->get_int("kdenlive_ix") == oldPos) {
2954 filter->set("kdenlive_ix", newPos);
2955 filtersList.append(filter);
2956 clipService.detach(*filter);
2957 filter = clipService.filter(ct);
2958 while (filter && filter->get_int("kdenlive_ix") <= newPos) {
2959 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2961 filter = clipService.filter(ct);
2965 if (filter && filter->get_int("kdenlive_ix") > newPos) {
2966 filtersList.append(filter);
2967 clipService.detach(*filter);
2969 filter = clipService.filter(ct);
2973 if (filter->get_int("kdenlive_ix") == oldPos) {
2974 filter->set("kdenlive_ix", newPos);
2975 filtersList.append(filter);
2976 clipService.detach(*filter);
2978 filter = clipService.filter(ct);
2982 filter = clipService.filter(ct);
2984 int pos = filter->get_int("kdenlive_ix");
2985 if (pos >= newPos) {
2986 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
2987 filtersList.append(filter);
2988 clipService.detach(*filter);
2990 filter = clipService.filter(ct);
2994 for (int i = 0; i < filtersList.count(); i++) {
2995 clipService.attach(*(filtersList.at(i)));
3000 bool Render::mltResizeClipEnd(ItemInfo info, GenTime clipDuration)
3002 Mlt::Service service(m_mltProducer->parent().get_service());
3003 Mlt::Tractor tractor(service);
3004 Mlt::Producer trackProducer(tractor.track(info.track));
3005 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3007 /* // Display playlist info
3008 kDebug()<<"//////////// BEFORE RESIZE";
3009 for (int i = 0; i < trackPlaylist.count(); i++) {
3010 int blankStart = trackPlaylist.clip_start(i);
3011 int blankDuration = trackPlaylist.clip_length(i) - 1;
3013 if (trackPlaylist.is_blank(i)) blk = "(blank)";
3014 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
3017 if (trackPlaylist.is_blank_at((int) info.startPos.frames(m_fps))) {
3018 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
3022 int clipIndex = trackPlaylist.get_clip_index_at((int) info.startPos.frames(m_fps));
3023 //kDebug() << "// SELECTED CLIP START: " << trackPlaylist.clip_start(clipIndex);
3024 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
3026 int previousStart = clip->get_in();
3027 int newDuration = (int) clipDuration.frames(m_fps) - 1;
3028 int diff = newDuration - (trackPlaylist.clip_length(clipIndex) - 1);
3030 int currentOut = newDuration + previousStart;
3031 if (currentOut > clip->get_length()) {
3032 clip->parent().set("length", currentOut + 1);
3033 clip->parent().set("out", currentOut);
3034 clip->set("length", currentOut + 1);
3037 /*if (newDuration > clip->get_out()) {
3038 clip->parent().set_in_and_out(0, newDuration + 1);
3039 clip->set_in_and_out(0, newDuration + 1);
3042 trackPlaylist.resize_clip(clipIndex, previousStart, newDuration + previousStart);
3043 trackPlaylist.consolidate_blanks(0);
3044 // skip to next clip
3046 //kDebug() << "//////// RESIZE CLIP: " << clipIndex << "( pos: " << info.startPos.frames(25) << "), DIFF: " << diff << ", CURRENT DUR: " << previousDuration << ", NEW DUR: " << newDuration << ", IX: " << clipIndex << ", MAX: " << trackPlaylist.count();
3048 // clip was made longer, trim next blank if there is one.
3049 if (clipIndex < trackPlaylist.count()) {
3050 // If this is not the last clip in playlist
3051 if (trackPlaylist.is_blank(clipIndex)) {
3052 int blankStart = trackPlaylist.clip_start(clipIndex);
3053 int blankDuration = trackPlaylist.clip_length(clipIndex);
3054 if (diff > blankDuration) {
3055 kDebug() << "// ERROR blank clip is not large enough to get back required space!!!";
3057 if (diff - blankDuration == 0) {
3058 trackPlaylist.remove(clipIndex);
3059 } else trackPlaylist.remove_region(blankStart, diff);
3061 kDebug() << "/// RESIZE ERROR, NXT CLIP IS NOT BLK: " << clipIndex;
3064 } else if (clipIndex != trackPlaylist.count()) trackPlaylist.insert_blank(clipIndex, 0 - diff - 1);
3065 trackPlaylist.consolidate_blanks(0);
3068 if (info.track != 0 && clipIndex == trackPlaylist.count()) mltCheckLength(&tractor);
3069 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
3070 //mltResizeTransparency(previousStart, previousStart, previousStart + newDuration, track, QString(clip->parent().get("id")).toInt());
3071 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
3072 ItemInfo transpinfo;
3073 transpinfo.startPos = info.startPos;
3074 transpinfo.endPos = info.startPos + clipDuration;
3075 transpinfo.track = info.track;
3076 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
3078 m_mltConsumer->set("refresh", 1);
3082 void Render::mltChangeTrackState(int track, bool mute, bool blind)
3084 Mlt::Service service(m_mltProducer->parent().get_service());
3085 Mlt::Tractor tractor(service);
3086 Mlt::Producer trackProducer(tractor.track(track));
3088 // Make sure muting will not produce problems with our audio mixing transition,
3089 // because audio mixing is done between each track and the lowest one
3090 bool audioMixingBroken = false;
3091 if (mute && trackProducer.get_int("hide") < 2 ) {
3092 // We mute a track with sound
3093 if (track == getLowestNonMutedAudioTrack(tractor)) audioMixingBroken = true;
3094 kDebug()<<"Muting track: "<<track <<" / "<<getLowestNonMutedAudioTrack(tractor);
3096 else if (!mute && trackProducer.get_int("hide") > 1 ) {
3097 // We un-mute a previously muted track
3098 if (track < getLowestNonMutedAudioTrack(tractor)) audioMixingBroken = true;
3102 if (blind) trackProducer.set("hide", 3);
3103 else trackProducer.set("hide", 2);
3105 trackProducer.set("hide", 1);
3107 trackProducer.set("hide", 0);
3109 if (audioMixingBroken) fixAudioMixing(tractor);
3111 tractor.multitrack()->refresh();
3116 int Render::getLowestNonMutedAudioTrack(Mlt::Tractor tractor)
3118 for (int i = 1; i < tractor.count(); i++) {
3119 Mlt::Producer trackProducer(tractor.track(i));
3120 if (trackProducer.get_int("hide") < 2) return i;
3122 return tractor.count() - 1;
3125 void Render::fixAudioMixing(Mlt::Tractor tractor)
3127 // Make sure the audio mixing transitions are applied to the lowest audible (non muted) track
3128 int lowestTrack = getLowestNonMutedAudioTrack(tractor);
3130 mlt_service serv = m_mltProducer->parent().get_service();
3131 Mlt::Field *field = tractor.field();
3132 mlt_service_lock(serv);
3134 mlt_service nextservice = mlt_service_get_producer(serv);
3135 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3136 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3137 QString resource = mlt_properties_get(properties, "mlt_service");
3139 mlt_service nextservicetodisconnect;
3140 // Delete all audio mixing transitions
3141 while (mlt_type == "transition") {
3142 if (resource == "mix") {
3143 nextservicetodisconnect = nextservice;
3144 nextservice = mlt_service_producer(nextservice);
3145 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
3147 else nextservice = mlt_service_producer(nextservice);
3148 if (nextservice == NULL) break;
3149 properties = MLT_SERVICE_PROPERTIES(nextservice);
3150 mlt_type = mlt_properties_get(properties, "mlt_type");
3151 resource = mlt_properties_get(properties, "mlt_service");
3154 // Re-add correct audio transitions
3155 for (int i = lowestTrack + 1; i < tractor.count(); i++) {
3156 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "mix");
3157 transition->set("always_active", 1);
3158 transition->set("combine", 1);
3159 transition->set("internal_added", 237);
3160 field->plant_transition(*transition, lowestTrack, i);
3162 mlt_service_unlock(serv);
3165 bool Render::mltResizeClipCrop(ItemInfo info, GenTime diff)
3167 Mlt::Service service(m_mltProducer->parent().get_service());
3168 int frameOffset = (int) diff.frames(m_fps);
3169 Mlt::Tractor tractor(service);
3170 Mlt::Producer trackProducer(tractor.track(info.track));
3171 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3172 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
3173 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
3177 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
3178 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
3180 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
3184 int previousStart = clip->get_in();
3185 int previousOut = clip->get_out();
3187 trackPlaylist.resize_clip(clipIndex, previousStart + frameOffset, previousOut + frameOffset);
3189 m_mltConsumer->set("refresh", 1);
3193 bool Render::mltResizeClipStart(ItemInfo info, GenTime diff)
3195 //kDebug() << "//////// RSIZING CLIP from: "<<info.startPos.frames(25)<<" to "<<diff.frames(25);
3196 Mlt::Service service(m_mltProducer->parent().get_service());
3197 int moveFrame = (int) diff.frames(m_fps);
3198 Mlt::Tractor tractor(service);
3199 Mlt::Producer trackProducer(tractor.track(info.track));
3200 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3201 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
3202 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
3206 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
3207 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
3208 if (clip == NULL || clip->is_blank()) {
3209 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
3213 int previousStart = clip->get_in();
3214 int previousOut = clip->get_out();
3216 previousStart += moveFrame;
3218 if (previousStart < 0) {
3219 // this is possible for images and color clips
3220 previousOut -= previousStart;
3224 int length = previousOut + 1;
3225 if (length > clip->get_length()) {
3226 clip->parent().set("length", length + 1);
3227 clip->parent().set("out", length);
3228 clip->set("length", length + 1);
3232 // kDebug() << "RESIZE, new start: " << previousStart << ", " << previousOut;
3233 trackPlaylist.resize_clip(clipIndex, previousStart, previousOut);
3234 if (moveFrame > 0) {
3235 trackPlaylist.insert_blank(clipIndex, moveFrame - 1);
3237 //int midpos = info.startPos.frames(m_fps) + moveFrame - 1;
3238 int blankIndex = clipIndex - 1;
3239 int blankLength = trackPlaylist.clip_length(blankIndex);
3240 // kDebug() << " + resizing blank length " << blankLength << ", SIZE DIFF: " << moveFrame;
3241 if (! trackPlaylist.is_blank(blankIndex)) {
3242 kDebug() << "WARNING, CLIP TO RESIZE IS NOT BLANK";
3244 if (blankLength + moveFrame == 0)
3245 trackPlaylist.remove(blankIndex);
3247 trackPlaylist.resize_clip(blankIndex, 0, blankLength + moveFrame - 1);
3249 trackPlaylist.consolidate_blanks(0);
3250 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
3251 //mltResizeTransparency(previousStart, (int) moveEnd.frames(m_fps), (int) (moveEnd + out - in).frames(m_fps), track, QString(clip->parent().get("id")).toInt());
3252 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
3253 ItemInfo transpinfo;
3254 transpinfo.startPos = info.startPos + diff;
3255 transpinfo.endPos = info.startPos + diff + (info.endPos - info.startPos);
3256 transpinfo.track = info.track;
3257 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
3259 //m_mltConsumer->set("refresh", 1);
3261 m_mltConsumer->set("refresh", 1);
3265 bool Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd, Mlt::Producer *prod, bool overwrite, bool insert)
3267 return mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps), prod, overwrite, insert);
3271 bool Render::mltUpdateClipProducer(Mlt::Tractor *tractor, int track, int pos, Mlt::Producer *prod)
3273 if (prod == NULL || !prod->is_valid() || tractor == NULL || !tractor->is_valid()) {
3274 kDebug() << "// Warning, CLIP on track " << track << ", at: " << pos << " is invalid, cannot update it!!!";
3278 Mlt::Producer trackProducer(tractor->track(track));
3279 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3280 int clipIndex = trackPlaylist.get_clip_index_at(pos);
3281 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3282 if (clipProducer == NULL || clipProducer->is_blank()) {
3283 kDebug() << "// ERROR UPDATING CLIP PROD";
3284 delete clipProducer;
3287 Mlt::Producer *clip = prod->cut(clipProducer->get_in(), clipProducer->get_out());
3288 if (!clip || !clip->is_valid()) {
3289 if (clip) delete clip;
3290 delete clipProducer;
3293 // move all effects to the correct producer
3294 mltPasteEffects(clipProducer, clip);
3295 trackPlaylist.insert_at(pos, clip, 1);
3297 delete clipProducer;
3301 bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd, Mlt::Producer *prod, bool overwrite, bool /*insert*/)
3303 Mlt::Service service(m_mltProducer->parent().get_service());
3304 if (service.type() != tractor_type) {
3305 kWarning() << "// TRACTOR PROBLEM";
3309 Mlt::Tractor tractor(service);
3311 Mlt::Producer trackProducer(tractor.track(startTrack));
3312 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3313 int clipIndex = trackPlaylist.get_clip_index_at(moveStart);
3314 //kDebug() << "////// LOOKING FOR CLIP TO MOVE, INDEX: " << clipIndex;
3315 bool checkLength = false;
3316 if (endTrack == startTrack) {
3317 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3318 if ((!overwrite && !trackPlaylist.is_blank_at(moveEnd)) || !clipProducer || !clipProducer->is_valid() || clipProducer->is_blank()) {
3319 // error, destination is not empty
3321 if (!trackPlaylist.is_blank_at(moveEnd) && clipProducer->is_valid()) trackPlaylist.insert_at(moveStart, clipProducer, 1);
3322 delete clipProducer;
3324 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
3325 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
3329 trackPlaylist.consolidate_blanks(0);
3331 trackPlaylist.remove_region(moveEnd, clipProducer->get_playtime());
3332 int clipIndex = trackPlaylist.get_clip_index_at(moveEnd);
3333 trackPlaylist.insert_blank(clipIndex, clipProducer->get_playtime() - 1);
3335 int newIndex = trackPlaylist.insert_at(moveEnd, clipProducer, 1);
3336 trackPlaylist.consolidate_blanks(1);
3337 delete clipProducer;
3338 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
3339 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
3341 if (newIndex + 1 == trackPlaylist.count()) checkLength = true;
3345 Mlt::Producer destTrackProducer(tractor.track(endTrack));
3346 Mlt::Playlist destTrackPlaylist((mlt_playlist) destTrackProducer.get_service());
3347 if (!overwrite && !destTrackPlaylist.is_blank_at(moveEnd)) {
3348 // error, destination is not empty
3352 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3353 if (!clipProducer || clipProducer->is_blank()) {
3354 // error, destination is not empty
3355 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
3356 if (clipProducer) delete clipProducer;
3357 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
3361 trackPlaylist.consolidate_blanks(0);
3362 destTrackPlaylist.consolidate_blanks(1);
3363 Mlt::Producer *clip;
3364 // check if we are moving a slowmotion producer
3365 QString serv = clipProducer->parent().get("mlt_service");
3366 QString currentid = clipProducer->parent().get("id");
3367 if (serv == "framebuffer" || currentid.endsWith("_video")) {
3368 clip = clipProducer;
3371 // Special case: prod is null when using placeholder clips.
3372 // in that case, use the producer existing in playlist. Note that
3373 // it will bypass the one producer per track logic and might cause
3374 // Sound cracks if clip is moved so that it overlaps another copy of itself
3375 clip = clipProducer->cut(clipProducer->get_in(), clipProducer->get_out());
3376 } else clip = prod->cut(clipProducer->get_in(), clipProducer->get_out());
3379 // move all effects to the correct producer
3380 mltPasteEffects(clipProducer, clip);
3383 destTrackPlaylist.remove_region(moveEnd, clip->get_playtime());
3384 int clipIndex = destTrackPlaylist.get_clip_index_at(moveEnd);
3385 destTrackPlaylist.insert_blank(clipIndex, clip->get_playtime() - 1);
3388 int newIndex = destTrackPlaylist.insert_at(moveEnd, clip, 1);
3390 if (clip == clipProducer) {
3395 delete clipProducer;
3397 destTrackPlaylist.consolidate_blanks(0);
3398 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
3399 kDebug() << "//////// moving clip transparency";
3400 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
3402 if (clipIndex > trackPlaylist.count()) checkLength = true;
3403 else if (newIndex + 1 == destTrackPlaylist.count()) checkLength = true;
3407 if (checkLength) mltCheckLength(&tractor);
3409 //m_mltConsumer->set("refresh", 1);
3414 QList <int> Render::checkTrackSequence(int track)
3417 Mlt::Service service(m_mltProducer->parent().get_service());
3418 if (service.type() != tractor_type) {
3419 kWarning() << "// TRACTOR PROBLEM";
3422 Mlt::Tractor tractor(service);
3424 Mlt::Producer trackProducer(tractor.track(track));
3425 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3426 int clipNb = trackPlaylist.count();
3427 //kDebug() << "// PARSING SCENE TRACK: " << t << ", CLIPS: " << clipNb;
3428 for (int i = 0; i < clipNb; i++) {
3429 Mlt::Producer *c = trackPlaylist.get_clip(i);
3430 int pos = trackPlaylist.clip_start(i);
3431 if (!list.contains(pos)) list.append(pos);
3432 pos += c->get_playtime();
3433 if (!list.contains(pos)) list.append(pos);
3439 bool Render::mltMoveTransition(QString type, int startTrack, int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut)
3441 int new_in = (int)newIn.frames(m_fps);
3442 int new_out = (int)newOut.frames(m_fps) - 1;
3443 if (new_in >= new_out) return false;
3444 int old_in = (int)oldIn.frames(m_fps);
3445 int old_out = (int)oldOut.frames(m_fps) - 1;
3447 Mlt::Service service(m_mltProducer->parent().get_service());
3448 Mlt::Tractor tractor(service);
3449 Mlt::Field *field = tractor.field();
3451 bool doRefresh = true;
3452 // Check if clip is visible in monitor
3453 int diff = old_out - m_mltProducer->position();
3454 if (diff < 0 || diff > old_out - old_in) doRefresh = false;
3456 diff = new_out - m_mltProducer->position();
3457 if (diff < 0 || diff > new_out - new_in) doRefresh = false;
3461 mlt_service nextservice = mlt_service_get_producer(service.get_service());
3462 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3463 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3464 QString resource = mlt_properties_get(properties, "mlt_service");
3465 int old_pos = (int)(old_in + old_out) / 2;
3468 while (mlt_type == "transition") {
3469 Mlt::Transition transition((mlt_transition) nextservice);
3470 int currentTrack = transition.get_b_track();
3471 int currentIn = (int) transition.get_in();
3472 int currentOut = (int) transition.get_out();
3474 if (resource == type && startTrack == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
3476 if (newTrack - startTrack != 0) {
3477 Mlt::Properties trans_props(transition.get_properties());
3478 Mlt::Transition new_transition(*m_mltProfile, transition.get("mlt_service"));
3479 Mlt::Properties new_trans_props(new_transition.get_properties());
3480 new_trans_props.inherit(trans_props);
3481 new_transition.set_in_and_out(new_in, new_out);
3482 field->disconnect_service(transition);
3483 mltPlantTransition(field, new_transition, newTransitionTrack, newTrack);
3484 //field->plant_transition(new_transition, newTransitionTrack, newTrack);
3485 } else transition.set_in_and_out(new_in, new_out);
3488 nextservice = mlt_service_producer(nextservice);
3489 if (nextservice == NULL) break;
3490 properties = MLT_SERVICE_PROPERTIES(nextservice);
3491 mlt_type = mlt_properties_get(properties, "mlt_type");
3492 resource = mlt_properties_get(properties, "mlt_service");
3495 if (doRefresh) refresh();
3496 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3501 void Render::mltPlantTransition(Mlt::Field *field, Mlt::Transition &tr, int a_track, int b_track)
3503 mlt_service nextservice = mlt_service_get_producer(field->get_service());
3504 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3505 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3506 QString resource = mlt_properties_get(properties, "mlt_service");
3507 QList <Mlt::Transition *> trList;
3509 while (mlt_type == "transition") {
3510 Mlt::Transition transition((mlt_transition) nextservice);
3511 int aTrack = transition.get_a_track();
3512 int bTrack = transition.get_b_track();
3513 if (resource != "mix" && (aTrack < a_track || (aTrack == a_track && bTrack > b_track))) {
3514 Mlt::Properties trans_props(transition.get_properties());
3515 Mlt::Transition *cp = new Mlt::Transition(*m_mltProfile, transition.get("mlt_service"));
3516 Mlt::Properties new_trans_props(cp->get_properties());
3517 new_trans_props.inherit(trans_props);
3519 field->disconnect_service(transition);
3521 //else kDebug() << "// FOUND TRANS OK, "<<resource<< ", A_: " << aTrack << ", B_ "<<bTrack;
3523 nextservice = mlt_service_producer(nextservice);
3524 if (nextservice == NULL) break;
3525 properties = MLT_SERVICE_PROPERTIES(nextservice);
3526 mlt_type = mlt_properties_get(properties, "mlt_type");
3527 resource = mlt_properties_get(properties, "mlt_service");
3529 field->plant_transition(tr, a_track, b_track);
3531 // re-add upper transitions
3532 for (int i = trList.count() - 1; i >= 0; i--) {
3533 //kDebug()<< "REPLANT ON TK: "<<trList.at(i)->get_a_track()<<", "<<trList.at(i)->get_b_track();
3534 field->plant_transition(*trList.at(i), trList.at(i)->get_a_track(), trList.at(i)->get_b_track());
3538 void Render::mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool force)
3540 if (oldTag == tag && !force) mltUpdateTransitionParams(tag, a_track, b_track, in, out, xml);
3542 //kDebug()<<"// DELETING TRANS: "<<a_track<<"-"<<b_track;
3543 mltDeleteTransition(oldTag, a_track, b_track, in, out, xml, false);
3544 mltAddTransition(tag, a_track, b_track, in, out, xml, false);
3547 if (m_mltProducer->position() >= in.frames(m_fps) && m_mltProducer->position() <= out.frames(m_fps)) refresh();
3550 void Render::mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml)
3552 mlt_service serv = m_mltProducer->parent().get_service();
3553 mlt_service_lock(serv);
3555 mlt_service nextservice = mlt_service_get_producer(serv);
3556 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3557 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3558 QString resource = mlt_properties_get(properties, "mlt_service");
3559 int in_pos = (int) in.frames(m_fps);
3560 int out_pos = (int) out.frames(m_fps) - 1;
3562 while (mlt_type == "transition") {
3563 mlt_transition tr = (mlt_transition) nextservice;
3564 int currentTrack = mlt_transition_get_b_track(tr);
3565 int currentBTrack = mlt_transition_get_a_track(tr);
3566 int currentIn = (int) mlt_transition_get_in(tr);
3567 int currentOut = (int) mlt_transition_get_out(tr);
3569 // kDebug()<<"Looking for transition : " << currentIn <<'x'<<currentOut<< ", OLD oNE: "<<in_pos<<'x'<<out_pos;
3570 if (resource == type && b_track == currentTrack && currentIn == in_pos && currentOut == out_pos) {
3571 QMap<QString, QString> map = mltGetTransitionParamsFromXml(xml);
3572 QMap<QString, QString>::Iterator it;
3574 mlt_properties transproperties = MLT_TRANSITION_PROPERTIES(tr);
3576 QString currentId = mlt_properties_get(transproperties, "kdenlive_id");
3577 if (currentId != xml.attribute("id")) {
3578 // The transition ID is not the same, so reset all properties
3579 mlt_properties_set(transproperties, "kdenlive_id", xml.attribute("id").toUtf8().constData());
3580 // Cleanup previous properties
3581 QStringList permanentProps;
3582 permanentProps << "factory" << "kdenlive_id" << "mlt_service" << "mlt_type" << "in";
3583 permanentProps << "out" << "a_track" << "b_track";
3584 for (int i = 0; i < mlt_properties_count(transproperties); i++) {
3585 QString propName = mlt_properties_get_name(transproperties, i);
3586 if (!propName.startsWith('_') && ! permanentProps.contains(propName)) {
3587 mlt_properties_set(transproperties, propName.toUtf8().constData(), "");
3592 mlt_properties_set_int(transproperties, "force_track", xml.attribute("force_track").toInt());
3593 mlt_properties_set_int(transproperties, "automatic", xml.attribute("automatic", "0").toInt());
3595 if (currentBTrack != a_track) {
3596 mlt_properties_set_int(transproperties, "a_track", a_track);
3598 for (it = map.begin(); it != map.end(); ++it) {
3600 mlt_properties_set(transproperties, key.toUtf8().constData(), it.value().toUtf8().constData());
3601 //kDebug() << " ------ UPDATING TRANS PARAM: " << key.toUtf8().constData() << ": " << it.value().toUtf8().constData();
3602 //filter->set("kdenlive_id", id);
3606 nextservice = mlt_service_producer(nextservice);
3607 if (nextservice == NULL) break;
3608 properties = MLT_SERVICE_PROPERTIES(nextservice);
3609 mlt_type = mlt_properties_get(properties, "mlt_type");
3610 resource = mlt_properties_get(properties, "mlt_service");
3612 mlt_service_unlock(serv);
3614 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3617 void Render::mltDeleteTransition(QString tag, int /*a_track*/, int b_track, GenTime in, GenTime out, QDomElement /*xml*/, bool /*do_refresh*/)
3619 mlt_service serv = m_mltProducer->parent().get_service();
3620 mlt_service_lock(serv);
3622 Mlt::Service service(serv);
3623 Mlt::Tractor tractor(service);
3624 Mlt::Field *field = tractor.field();
3626 //if (do_refresh) m_mltConsumer->set("refresh", 0);
3628 mlt_service nextservice = mlt_service_get_producer(serv);
3629 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3630 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3631 QString resource = mlt_properties_get(properties, "mlt_service");
3633 const int old_pos = (int)((in + out).frames(m_fps) / 2);
3634 //kDebug() << " del trans pos: " << in.frames(25) << "-" << out.frames(25);
3636 while (mlt_type == "transition") {
3637 mlt_transition tr = (mlt_transition) nextservice;
3638 int currentTrack = mlt_transition_get_b_track(tr);
3639 int currentIn = (int) mlt_transition_get_in(tr);
3640 int currentOut = (int) mlt_transition_get_out(tr);
3641 //kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
3643 if (resource == tag && b_track == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
3644 mlt_field_disconnect_service(field->get_field(), nextservice);
3647 nextservice = mlt_service_producer(nextservice);
3648 if (nextservice == NULL) break;
3649 properties = MLT_SERVICE_PROPERTIES(nextservice);
3650 mlt_type = mlt_properties_get(properties, "mlt_type");
3651 resource = mlt_properties_get(properties, "mlt_service");
3653 mlt_service_unlock(serv);
3655 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3658 QMap<QString, QString> Render::mltGetTransitionParamsFromXml(QDomElement xml)
3660 QDomNodeList attribs = xml.elementsByTagName("parameter");
3661 QMap<QString, QString> map;
3662 for (int i = 0; i < attribs.count(); i++) {
3663 QDomElement e = attribs.item(i).toElement();
3664 QString name = e.attribute("name");
3665 //kDebug()<<"-- TRANSITION PARAM: "<<name<<" = "<< e.attribute("name")<<" / " << e.attribute("value");
3666 map[name] = e.attribute("default");
3667 if (!e.attribute("value").isEmpty()) {
3668 map[name] = e.attribute("value");
3670 if (e.attribute("type") != "addedgeometry" && (e.attribute("factor", "1") != "1" || e.attribute("offset", "0") != "0")) {
3671 map[name] = m_locale.toString((map.value(name).toDouble() - e.attribute("offset", "0").toDouble()) / e.attribute("factor", "1").toDouble());
3672 //map[name]=map[name].replace(".",","); //FIXME how to solve locale conversion of . ,
3675 if (e.attribute("namedesc").contains(';')) {
3676 QString format = e.attribute("format");
3677 QStringList separators = format.split("%d", QString::SkipEmptyParts);
3678 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
3680 QTextStream txtNeu(&neu);
3681 if (values.size() > 0)
3682 txtNeu << (int)values[0].toDouble();
3684 for (i = 0; i < separators.size() && i + 1 < values.size(); i++) {
3685 txtNeu << separators[i];
3686 txtNeu << (int)(values[i+1].toDouble());
3688 if (i < separators.size())
3689 txtNeu << separators[i];
3690 map[e.attribute("name")] = neu;
3697 void Render::mltAddClipTransparency(ItemInfo info, int transitiontrack, int id)
3699 kDebug() << "///////// ADDING CLIP TRANSPARENCY AT: " << info.startPos.frames(25);
3700 Mlt::Service service(m_mltProducer->parent().get_service());
3701 Mlt::Tractor tractor(service);
3702 Mlt::Field *field = tractor.field();
3704 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
3705 transition->set_in_and_out((int) info.startPos.frames(m_fps), (int) info.endPos.frames(m_fps) - 1);
3706 transition->set("transparency", id);
3707 transition->set("fill", 1);
3708 transition->set("internal_added", 237);
3709 field->plant_transition(*transition, transitiontrack, info.track);
3713 void Render::mltDeleteTransparency(int pos, int track, int id)
3715 Mlt::Service service(m_mltProducer->parent().get_service());
3716 Mlt::Tractor tractor(service);
3717 Mlt::Field *field = tractor.field();
3719 //if (do_refresh) m_mltConsumer->set("refresh", 0);
3720 mlt_service serv = m_mltProducer->parent().get_service();
3722 mlt_service nextservice = mlt_service_get_producer(serv);
3723 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3724 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3725 QString resource = mlt_properties_get(properties, "mlt_service");
3727 while (mlt_type == "transition") {
3728 mlt_transition tr = (mlt_transition) nextservice;
3729 int currentTrack = mlt_transition_get_b_track(tr);
3730 int currentIn = (int) mlt_transition_get_in(tr);
3731 int currentOut = (int) mlt_transition_get_out(tr);
3732 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3733 kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
3735 if (resource == "composite" && track == currentTrack && currentIn == pos && transitionId == id) {
3736 //kDebug() << " / / / / /DELETE TRANS DOOOMNE";
3737 mlt_field_disconnect_service(field->get_field(), nextservice);
3740 nextservice = mlt_service_producer(nextservice);
3741 if (nextservice == NULL) break;
3742 properties = MLT_SERVICE_PROPERTIES(nextservice);
3743 mlt_type = mlt_properties_get(properties, "mlt_type");
3744 resource = mlt_properties_get(properties, "mlt_service");
3746 //if (do_refresh) m_mltConsumer->set("refresh", 1);
3749 void Render::mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id)
3751 Mlt::Service service(m_mltProducer->parent().get_service());
3752 Mlt::Tractor tractor(service);
3755 m_mltConsumer->set("refresh", 0);
3757 mlt_service serv = m_mltProducer->parent().get_service();
3758 mlt_service nextservice = mlt_service_get_producer(serv);
3759 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3760 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3761 QString resource = mlt_properties_get(properties, "mlt_service");
3762 kDebug() << "// resize transpar from: " << oldStart << ", TO: " << newStart << 'x' << newEnd << ", " << track << ", " << id;
3763 while (mlt_type == "transition") {
3764 mlt_transition tr = (mlt_transition) nextservice;
3765 int currentTrack = mlt_transition_get_b_track(tr);
3766 int currentIn = (int) mlt_transition_get_in(tr);
3767 //mlt_properties props = MLT_TRANSITION_PROPERTIES(tr);
3768 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3769 kDebug() << "// resize transpar current in: " << currentIn << ", Track: " << currentTrack << ", id: " << id << 'x' << transitionId ;
3770 if (resource == "composite" && track == currentTrack && currentIn == oldStart && transitionId == id) {
3771 kDebug() << " / / / / /RESIZE TRANS TO: " << newStart << 'x' << newEnd;
3772 mlt_transition_set_in_and_out(tr, newStart, newEnd);
3775 nextservice = mlt_service_producer(nextservice);
3776 if (nextservice == NULL) break;
3777 properties = MLT_SERVICE_PROPERTIES(nextservice);
3778 mlt_type = mlt_properties_get(properties, "mlt_type");
3779 resource = mlt_properties_get(properties, "mlt_service");
3782 m_mltConsumer->set("refresh", 1);
3786 void Render::mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id)
3788 Mlt::Service service(m_mltProducer->parent().get_service());
3789 Mlt::Tractor tractor(service);
3792 m_mltConsumer->set("refresh", 0);
3794 mlt_service serv = m_mltProducer->parent().get_service();
3795 mlt_service nextservice = mlt_service_get_producer(serv);
3796 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3797 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3798 QString resource = mlt_properties_get(properties, "mlt_service");
3800 while (mlt_type == "transition") {
3801 mlt_transition tr = (mlt_transition) nextservice;
3802 int currentTrack = mlt_transition_get_b_track(tr);
3803 int currentaTrack = mlt_transition_get_a_track(tr);
3804 int currentIn = (int) mlt_transition_get_in(tr);
3805 int currentOut = (int) mlt_transition_get_out(tr);
3806 //mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3807 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3808 //kDebug()<<" + TRANSITION "<<id<<" == "<<transitionId<<", START TMIE: "<<currentIn<<", LOOK FR: "<<startTime<<", TRACK: "<<currentTrack<<'x'<<startTrack;
3809 if (resource == "composite" && transitionId == id && startTime == currentIn && startTrack == currentTrack) {
3810 kDebug() << "//////MOVING";
3811 mlt_transition_set_in_and_out(tr, endTime, endTime + currentOut - currentIn);
3812 if (endTrack != startTrack) {
3813 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3814 mlt_properties_set_int(properties, "a_track", currentaTrack + endTrack - currentTrack);
3815 mlt_properties_set_int(properties, "b_track", endTrack);
3819 nextservice = mlt_service_producer(nextservice);
3820 if (nextservice == NULL) break;
3821 properties = MLT_SERVICE_PROPERTIES(nextservice);
3822 mlt_type = mlt_properties_get(properties, "mlt_type");
3823 resource = mlt_properties_get(properties, "mlt_service");
3826 m_mltConsumer->set("refresh", 1);
3830 bool Render::mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool do_refresh)
3832 if (in >= out) return false;
3833 QMap<QString, QString> args = mltGetTransitionParamsFromXml(xml);
3834 Mlt::Service service(m_mltProducer->parent().get_service());
3836 Mlt::Tractor tractor(service);
3837 Mlt::Field *field = tractor.field();
3839 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, tag.toUtf8().constData());
3840 if (out != GenTime())
3841 transition->set_in_and_out((int) in.frames(m_fps), (int) out.frames(m_fps) - 1);
3843 if (do_refresh && (m_mltProducer->position() < in.frames(m_fps) || m_mltProducer->position() > out.frames(m_fps))) do_refresh = false;
3844 QMap<QString, QString>::Iterator it;
3846 if (xml.attribute("automatic") == "1") transition->set("automatic", 1);
3847 //kDebug() << " ------ ADDING TRANSITION PARAMs: " << args.count();
3848 if (xml.hasAttribute("id"))
3849 transition->set("kdenlive_id", xml.attribute("id").toUtf8().constData());
3850 if (xml.hasAttribute("force_track"))
3851 transition->set("force_track", xml.attribute("force_track").toInt());
3853 for (it = args.begin(); it != args.end(); ++it) {
3855 if (!it.value().isEmpty())
3856 transition->set(key.toUtf8().constData(), it.value().toUtf8().constData());
3857 //kDebug() << " ------ ADDING TRANS PARAM: " << key << ": " << it.value();
3859 // attach transition
3861 mltPlantTransition(field, *transition, a_track, b_track);
3862 // field->plant_transition(*transition, a_track, b_track);
3864 if (do_refresh) refresh();
3868 void Render::mltSavePlaylist()
3870 kWarning() << "// UPDATING PLAYLIST TO DISK++++++++++++++++";
3871 Mlt::Consumer fileConsumer(*m_mltProfile, "xml");
3872 fileConsumer.set("resource", "/tmp/playlist.mlt");
3874 Mlt::Service service(m_mltProducer->get_service());
3876 fileConsumer.connect(service);
3877 fileConsumer.start();
3880 const QList <Mlt::Producer *> Render::producersList()
3882 QList <Mlt::Producer *> prods;
3883 if (m_mltProducer == NULL) return prods;
3884 Mlt::Service service(m_mltProducer->parent().get_service());
3885 if (service.type() != tractor_type) return prods;
3886 Mlt::Tractor tractor(service);
3889 int trackNb = tractor.count();
3890 for (int t = 1; t < trackNb; t++) {
3891 Mlt::Producer *tt = tractor.track(t);
3892 Mlt::Producer trackProducer(tt);
3894 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3895 int clipNb = trackPlaylist.count();
3896 for (int i = 0; i < clipNb; i++) {
3897 Mlt::Producer *c = trackPlaylist.get_clip(i);
3898 if (c == NULL) continue;
3899 QString prodId = c->parent().get("id");
3900 if (!c->is_blank() && !ids.contains(prodId) && !prodId.startsWith("slowmotion") && !prodId.isEmpty()) {
3901 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
3904 prods.append(nprod);
3913 void Render::fillSlowMotionProducers()
3915 if (m_mltProducer == NULL) return;
3916 Mlt::Service service(m_mltProducer->parent().get_service());
3917 if (service.type() != tractor_type) return;
3919 Mlt::Tractor tractor(service);
3921 int trackNb = tractor.count();
3922 for (int t = 1; t < trackNb; t++) {
3923 Mlt::Producer *tt = tractor.track(t);
3924 Mlt::Producer trackProducer(tt);
3926 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3927 int clipNb = trackPlaylist.count();
3928 for (int i = 0; i < clipNb; i++) {
3929 Mlt::Producer *c = trackPlaylist.get_clip(i);
3930 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
3932 QString id = nprod->parent().get("id");
3933 if (id.startsWith("slowmotion:") && !nprod->is_blank()) {
3934 // this is a slowmotion producer, add it to the list
3935 QString url = QString::fromUtf8(nprod->get("resource"));
3936 int strobe = nprod->get_int("strobe");
3937 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
3938 if (!m_slowmotionProducers.contains(url)) {
3939 m_slowmotionProducers.insert(url, nprod);
3941 } else delete nprod;
3948 void Render::mltInsertTrack(int ix, bool videoTrack)
3952 Mlt::Service service(m_mltProducer->parent().get_service());
3954 if (service.type() != tractor_type) {
3955 kWarning() << "// TRACTOR PROBLEM";
3959 Mlt::Tractor tractor(service);
3961 Mlt::Playlist playlist;
3962 int ct = tractor.count();
3964 kDebug() << "// ERROR, TRYING TO insert TRACK " << ix << ", max: " << ct;
3970 Mlt::Producer *prodToMove = new Mlt::Producer(tractor.track(pos));
3971 tractor.set_track(playlist, pos);
3972 Mlt::Producer newProd(tractor.track(pos));
3973 if (!videoTrack) newProd.set("hide", 1);
3975 for (; pos <= ct; pos++) {
3976 Mlt::Producer *prodToMove2 = new Mlt::Producer(tractor.track(pos));
3977 tractor.set_track(*prodToMove, pos);
3978 prodToMove = prodToMove2;
3981 tractor.set_track(playlist, ix);
3982 Mlt::Producer newProd(tractor.track(ix));
3983 if (!videoTrack) newProd.set("hide", 1);
3988 mlt_service serv = m_mltProducer->parent().get_service();
3989 mlt_service nextservice = mlt_service_get_producer(serv);
3990 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3991 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3992 QString resource = mlt_properties_get(properties, "mlt_service");
3994 while (mlt_type == "transition") {
3995 if (resource != "mix") {
3996 mlt_transition tr = (mlt_transition) nextservice;
3997 int currentTrack = mlt_transition_get_b_track(tr);
3998 int currentaTrack = mlt_transition_get_a_track(tr);
3999 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
4001 if (currentTrack >= ix) {
4002 mlt_properties_set_int(properties, "b_track", currentTrack + 1);
4003 mlt_properties_set_int(properties, "a_track", currentaTrack + 1);
4006 nextservice = mlt_service_producer(nextservice);
4007 if (nextservice == NULL) break;
4008 properties = MLT_SERVICE_PROPERTIES(nextservice);
4009 mlt_type = mlt_properties_get(properties, "mlt_type");
4010 resource = mlt_properties_get(properties, "mlt_service");
4013 // Add audio mix transition to last track
4014 Mlt::Field *field = tractor.field();
4015 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "mix");
4016 transition->set("a_track", 1);
4017 transition->set("b_track", ct);
4018 transition->set("always_active", 1);
4019 transition->set("internal_added", 237);
4020 transition->set("combine", 1);
4021 field->plant_transition(*transition, 1, ct);
4022 //mlt_service_unlock(m_mltConsumer->get_service());
4024 //tractor.multitrack()->refresh();
4025 //tractor.refresh();
4026 blockSignals(false);
4030 void Render::mltDeleteTrack(int ix)
4033 doc.setContent(sceneList(), false);
4034 int tracksCount = doc.elementsByTagName("track").count() - 1;
4035 QDomNode track = doc.elementsByTagName("track").at(ix);
4036 QDomNode tractor = doc.elementsByTagName("tractor").at(0);
4037 QDomNodeList transitions = doc.elementsByTagName("transition");
4038 for (int i = 0; i < transitions.count(); i++) {
4039 QDomElement e = transitions.at(i).toElement();
4040 QDomNodeList props = e.elementsByTagName("property");
4041 QMap <QString, QString> mappedProps;
4042 for (int j = 0; j < props.count(); j++) {
4043 QDomElement f = props.at(j).toElement();
4044 mappedProps.insert(f.attribute("name"), f.firstChild().nodeValue());
4046 if (mappedProps.value("mlt_service") == "mix" && mappedProps.value("b_track").toInt() == tracksCount) {
4047 tractor.removeChild(transitions.at(i));
4049 } else if (mappedProps.value("mlt_service") != "mix" && (mappedProps.value("b_track").toInt() >= ix || mappedProps.value("a_track").toInt() >= ix)) {
4050 // Transition needs to be moved
4051 int a_track = mappedProps.value("a_track").toInt();
4052 int b_track = mappedProps.value("b_track").toInt();
4053 if (a_track > 0 && a_track >= ix) a_track --;
4054 if (b_track == ix) {
4055 // transition was on the deleted track, so remove it
4056 tractor.removeChild(transitions.at(i));
4060 if (b_track > 0 && b_track > ix) b_track --;
4061 for (int j = 0; j < props.count(); j++) {
4062 QDomElement f = props.at(j).toElement();
4063 if (f.attribute("name") == "a_track") f.firstChild().setNodeValue(QString::number(a_track));
4064 else if (f.attribute("name") == "b_track") f.firstChild().setNodeValue(QString::number(b_track));
4069 tractor.removeChild(track);
4070 //kDebug() << "/////////// RESULT SCENE: \n" << doc.toString();
4071 setSceneList(doc.toString(), m_mltConsumer->position());
4072 emit refreshDocumentProducers(false, false);
4076 void Render::updatePreviewSettings()
4078 kDebug() << "////// RESTARTING CONSUMER";
4079 if (!m_mltConsumer || !m_mltProducer) return;
4080 if (m_mltProducer->get_playtime() == 0) return;
4081 QMutexLocker locker(&m_mutex);
4082 Mlt::Service service(m_mltProducer->parent().get_service());
4083 if (service.type() != tractor_type) return;
4085 //m_mltConsumer->set("refresh", 0);
4086 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
4087 m_mltConsumer->purge();
4088 QString scene = sceneList();
4090 if (m_mltProducer) {
4091 pos = m_mltProducer->position();
4094 setSceneList(scene, pos);
4098 QString Render::updateSceneListFps(double current_fps, double new_fps, QString scene)
4100 // Update all frame positions to the new fps value
4101 //WARNING: there are probably some effects or other that hold a frame value
4102 // as parameter and will also need to be updated here!
4104 doc.setContent(scene);
4106 double factor = new_fps / current_fps;
4107 QDomNodeList producers = doc.elementsByTagName("producer");
4108 for (int i = 0; i < producers.count(); i++) {
4109 QDomElement prod = producers.at(i).toElement();
4110 prod.removeAttribute("in");
4111 prod.removeAttribute("out");
4113 QDomNodeList props = prod.childNodes();
4114 for (int j = 0; j < props.count(); j++) {
4115 QDomElement param = props.at(j).toElement();
4116 QString paramName = param.attribute("name");
4117 if (paramName.startsWith("meta.") || paramName == "length") {
4118 prod.removeChild(props.at(j));
4124 QDomNodeList entries = doc.elementsByTagName("entry");
4125 for (int i = 0; i < entries.count(); i++) {
4126 QDomElement entry = entries.at(i).toElement();
4127 int in = entry.attribute("in").toInt();
4128 int out = entry.attribute("out").toInt();
4129 in = factor * in + 0.5;
4130 out = factor * out + 0.5;
4131 entry.setAttribute("in", in);
4132 entry.setAttribute("out", out);
4135 QDomNodeList blanks = doc.elementsByTagName("blank");
4136 for (int i = 0; i < blanks.count(); i++) {
4137 QDomElement blank = blanks.at(i).toElement();
4138 int length = blank.attribute("length").toInt();
4139 length = factor * length + 0.5;
4140 blank.setAttribute("length", QString::number(length));
4143 QDomNodeList filters = doc.elementsByTagName("filter");
4144 for (int i = 0; i < filters.count(); i++) {
4145 QDomElement filter = filters.at(i).toElement();
4146 int in = filter.attribute("in").toInt();
4147 int out = filter.attribute("out").toInt();
4148 in = factor * in + 0.5;
4149 out = factor * out + 0.5;
4150 filter.setAttribute("in", in);
4151 filter.setAttribute("out", out);
4154 QDomNodeList transitions = doc.elementsByTagName("transition");
4155 for (int i = 0; i < transitions.count(); i++) {
4156 QDomElement transition = transitions.at(i).toElement();
4157 int in = transition.attribute("in").toInt();
4158 int out = transition.attribute("out").toInt();
4159 in = factor * in + 0.5;
4160 out = factor * out + 0.5;
4161 transition.setAttribute("in", in);
4162 transition.setAttribute("out", out);
4163 QDomNodeList props = transition.childNodes();
4164 for (int j = 0; j < props.count(); j++) {
4165 QDomElement param = props.at(j).toElement();
4166 QString paramName = param.attribute("name");
4167 if (paramName == "geometry") {
4168 QString geom = param.firstChild().nodeValue();
4169 QStringList keys = geom.split(';');
4170 QStringList newKeys;
4171 for (int k = 0; k < keys.size(); ++k) {
4172 if (keys.at(k).contains('=')) {
4173 int pos = keys.at(k).section('=', 0, 0).toInt();
4174 pos = factor * pos + 0.5;
4175 newKeys.append(QString::number(pos) + '=' + keys.at(k).section('=', 1));
4176 } else newKeys.append(keys.at(k));
4178 param.firstChild().setNodeValue(newKeys.join(";"));
4182 QDomElement root = doc.documentElement();
4183 if (!root.isNull()) {
4184 QDomElement tractor = root.firstChildElement("tractor");
4185 int out = tractor.attribute("out").toInt();
4186 out = factor * out + 0.5;
4187 tractor.setAttribute("out", out);
4188 emit durationChanged(out);
4191 //kDebug() << "///////////////////////////// " << out << " \n" << doc.toString() << "\n-------------------------";
4192 return doc.toString();
4196 void Render::sendFrameUpdate()
4198 if (m_mltProducer) {
4199 Mlt::Frame * frame = m_mltProducer->get_frame();
4200 emitFrameUpdated(*frame);
4205 Mlt::Producer* Render::getProducer()
4207 return m_mltProducer;
4210 const QString Render::activeClipId()
4212 if (m_mltProducer) return m_mltProducer->get("id");
4216 #include "renderer.moc"