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"
32 #include "blackmagic/devices.h"
34 #include <mlt++/Mlt.h>
37 #include <KStandardDirs>
38 #include <KMessageBox>
40 #include <KTemporaryFile>
45 #include <QApplication>
46 #include <QtConcurrentRun>
54 static void kdenlive_callback(void* /*ptr*/, int level, const char* fmt, va_list vl)
56 // kDebug() << "log level" << level << QString().vsprintf(fmt, vl).simplified();
57 if (level > MLT_LOG_ERROR) return;
59 QApplication::postEvent(qApp->activeWindow(), new MltErrorEvent(error.vsprintf(fmt, vl).simplified()));
64 static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr)
66 // detect if the producer has finished playing. Is there a better way to do it?
67 Mlt::Frame frame(frame_ptr);
68 if (!frame.is_valid()) return;
69 self->emitFrameNumber(mlt_frame_get_position(frame_ptr));
70 if (self->sendFrameForAnalysis && frame_ptr->convert_image) {
71 self->emitFrameUpdated(frame);
73 if (self->analyseAudio) {
74 self->showAudio(frame);
76 if (frame.get_double("_speed") < 0.0 && mlt_frame_get_position(frame_ptr) <= 0) {
78 self->emitConsumerStopped();
83 static void consumer_paused(mlt_consumer, Render * self, mlt_frame /*frame_ptr*/)
85 // detect if the producer has finished playing. Is there a better way to do it?
86 self->emitConsumerStopped();
90 static void consumer_gl_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr)
92 // detect if the producer has finished playing. Is there a better way to do it?
93 Mlt::Frame frame(frame_ptr);
94 self->showFrame(frame);
95 if (frame.get_double("_speed") == 0.0) {
96 self->emitConsumerStopped();
97 } else if (frame.get_double("_speed") < 0.0 && mlt_frame_get_position(frame_ptr) <= 0) {
99 self->emitConsumerStopped();
103 Render::Render(const QString & rendererName, int winid, QString profile, QWidget *parent) :
104 AbstractRender(rendererName, parent),
105 analyseAudio(KdenliveSettings::monitor_audio()),
106 m_name(rendererName),
111 m_externalConsumer(false),
114 m_isSplitView(false),
118 if (profile.isEmpty()) profile = KdenliveSettings::current_profile();
119 buildConsumer(profile);
120 m_mltProducer = m_blackClip->cut(0, 50);
121 m_mltConsumer->connect(*m_mltProducer);
122 m_mltProducer->set_speed(0.0);
132 void Render::closeMlt()
135 m_requestList.clear();
136 m_infoThread.waitForFinished();
137 if (m_mltConsumer) delete m_mltConsumer;
138 if (m_mltProducer) delete m_mltProducer;
139 /*if (m_mltProducer) {
140 Mlt::Service service(m_mltProducer->parent().get_service());
141 mlt_service_lock(service.get_service());
143 if (service.type() == tractor_type) {
144 Mlt::Tractor tractor(service);
145 Mlt::Field *field = tractor.field();
146 mlt_service nextservice = mlt_service_get_producer(service.get_service());
147 mlt_service nextservicetodisconnect;
148 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
149 QString mlt_type = mlt_properties_get(properties, "mlt_type");
150 QString resource = mlt_properties_get(properties, "mlt_service");
151 // Delete all transitions
152 while (mlt_type == "transition") {
153 nextservicetodisconnect = nextservice;
154 nextservice = mlt_service_producer(nextservice);
155 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
156 if (nextservice == NULL) break;
157 properties = MLT_SERVICE_PROPERTIES(nextservice);
158 mlt_type = mlt_properties_get(properties, "mlt_type");
159 resource = mlt_properties_get(properties, "mlt_service");
165 mlt_service_unlock(service.get_service());
168 kDebug() << "// // // CLOSE RENDERER " << m_name;
169 if (m_blackClip) delete m_blackClip;
173 void Render::slotSwitchFullscreen()
175 if (m_mltConsumer) m_mltConsumer->set("full_screen", 1);
178 void Render::buildConsumer(const QString &profileName)
183 //TODO: uncomment following line when everything is clean
184 // uncommented Feb 2011 --Granjow
185 if (m_mltProfile) delete m_mltProfile;
186 m_activeProfile = profileName;
187 char *tmp = qstrdup(m_activeProfile.toUtf8().constData());
188 setenv("MLT_PROFILE", tmp, 1);
189 m_mltProfile = new Mlt::Profile(tmp);
190 m_mltProfile->set_explicit(true);
191 kDebug()<<"// ********* PROFILE AR: "<<m_mltProfile->dar();
194 m_blackClip = new Mlt::Producer(*m_mltProfile, "colour", "black");
195 m_blackClip->set("id", "black");
196 m_blackClip->set("mlt_type", "producer");
198 if (KdenliveSettings::external_display() && m_name != "clip") {
199 // Use blackmagic card for video output
200 QMap< QString, QString > profileProperties = ProfilesDialog::getSettingsFromFile(profileName);
201 int device = KdenliveSettings::blackmagic_output_device();
203 if (BMInterface::isSupportedProfile(device, profileProperties)) {
204 QString decklink = "decklink:" + QString::number(KdenliveSettings::blackmagic_output_device());
205 tmp = qstrdup(decklink.toUtf8().constData());
206 m_mltConsumer = new Mlt::Consumer(*m_mltProfile, tmp);
208 if (m_mltConsumer->is_valid()) {
209 m_externalConsumer = true;
210 m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_show);
211 m_mltConsumer->set("terminate_on_pause", 0);
212 m_mltConsumer->set("buffer", 12);
213 m_mltConsumer->set("deinterlace_method", "onefield");
214 m_mltConsumer->set("real_time", KdenliveSettings::mltthreads());
215 mlt_log_set_callback(kdenlive_callback);
217 if (m_mltConsumer && m_mltConsumer->is_valid()) return;
218 } 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()));
221 m_externalConsumer = false;
222 QString videoDriver = KdenliveSettings::videodrivername();
223 if (!videoDriver.isEmpty()) {
224 if (videoDriver == "x11_noaccel") {
225 setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
228 unsetenv("SDL_VIDEO_YUV_HWACCEL");
231 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
233 //m_mltConsumer->set("fullscreen", 1);
236 m_mltConsumer = new Mlt::Consumer(*m_mltProfile, "sdl_audio");
237 m_mltConsumer->set("preview_off", 1);
238 m_mltConsumer->set("preview_format", mlt_image_rgb24a);
239 m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_gl_frame_show);
241 m_mltConsumer = new Mlt::Consumer(*m_mltProfile, "sdl_preview");
242 // FIXME: the event object returned by the listen gets leaked...
243 m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_show);
244 m_mltConsumer->listen("consumer-sdl-paused", this, (mlt_listener) consumer_paused);
245 m_mltConsumer->set("window_id", m_winid);
247 m_mltConsumer->set("resize", 1);
248 m_mltConsumer->set("terminate_on_pause", 1);
249 m_mltConsumer->set("window_background", KdenliveSettings::window_background().name().toUtf8().constData());
250 m_mltConsumer->set("rescale", "nearest");
251 mlt_log_set_callback(kdenlive_callback);
253 QString audioDevice = KdenliveSettings::audiodevicename();
254 if (!audioDevice.isEmpty())
255 m_mltConsumer->set("audio_device", audioDevice.toUtf8().constData());
257 if (!videoDriver.isEmpty())
258 m_mltConsumer->set("video_driver", videoDriver.toUtf8().constData());
260 QString audioDriver = KdenliveSettings::audiodrivername();
263 // Disabled because the "auto" detected driver was sometimes wrong
264 if (audioDriver.isEmpty())
265 audioDriver = KdenliveSettings::autoaudiodrivername();
268 if (!audioDriver.isEmpty())
269 m_mltConsumer->set("audio_driver", audioDriver.toUtf8().constData());
271 m_mltConsumer->set("progressive", 1);
272 m_mltConsumer->set("audio_buffer", 1024);
273 m_mltConsumer->set("frequency", 48000);
274 m_mltConsumer->set("real_time", KdenliveSettings::mltthreads());
277 Mlt::Producer *Render::invalidProducer(const QString &id)
280 QString txt = "+" + i18n("Missing clip") + ".txt";
281 char *tmp = qstrdup(txt.toUtf8().constData());
282 clip = new Mlt::Producer(*m_mltProfile, tmp);
284 if (clip == NULL) clip = new Mlt::Producer(*m_mltProfile, "colour", "red");
286 clip->set("bgcolour", "0xff0000ff");
287 clip->set("pad", "10");
289 clip->set("id", id.toUtf8().constData());
290 clip->set("mlt_type", "producer");
294 int Render::resetProfile(const QString &profileName, bool dropSceneList)
297 if (!dropSceneList) scene = sceneList();
299 if (m_externalConsumer == KdenliveSettings::external_display()) {
300 if (KdenliveSettings::external_display() && m_activeProfile == profileName) return 1;
301 QString videoDriver = KdenliveSettings::videodrivername();
302 QString currentDriver = m_mltConsumer->get("video_driver");
303 if (getenv("SDL_VIDEO_YUV_HWACCEL") != NULL && currentDriver == "x11") currentDriver = "x11_noaccel";
304 QString background = KdenliveSettings::window_background().name();
305 QString currentBackground = m_mltConsumer->get("window_background");
306 if (m_activeProfile == profileName && currentDriver == videoDriver && background == currentBackground) {
307 kDebug() << "reset to same profile, nothing to do";
312 if (m_isSplitView) slotSplitView(false);
313 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
314 m_mltConsumer->purge();
315 delete m_mltConsumer;
316 m_mltConsumer = NULL;
319 double current_fps = m_mltProfile->fps();
320 double current_dar = m_mltProfile->dar();
323 m_requestList.clear();
324 m_infoThread.waitForFinished();
327 pos = m_mltProducer->position();
329 Mlt::Service service(m_mltProducer->get_service());
330 if (service.type() == tractor_type) {
331 Mlt::Tractor tractor(service);
332 for (int trackNb = tractor.count() - 1; trackNb >= 0; --trackNb) {
333 Mlt::Producer trackProducer(tractor.track(trackNb));
334 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
335 trackPlaylist.clear();
339 delete m_mltProducer;
341 m_mltProducer = NULL;
342 buildConsumer(profileName);
343 double new_fps = m_mltProfile->fps();
344 double new_dar = m_mltProfile->dar();
346 if (!dropSceneList) {
347 // We need to recover our playlist
348 if (current_fps != new_fps) {
349 // fps changed, we must update the scenelist positions
350 scene = updateSceneListFps(current_fps, new_fps, scene);
352 setSceneList(scene, pos);
353 // producers have changed (different profile), so reset them...
354 emit refreshDocumentProducers(new_dar != current_dar, current_fps != new_fps);
359 void Render::seek(GenTime time)
363 m_mltProducer->seek((int)(time.frames(m_fps)));
367 void Render::seek(int time)
371 m_mltProducer->seek(time);
376 /*QPixmap Render::frameThumbnail(Mlt::Frame *frame, int width, int height, bool border) {
377 QPixmap pix(width, height);
379 mlt_image_format format = mlt_image_rgb24a;
380 uint8_t *thumb = frame->get_image(format, width, height);
381 QImage image(thumb, width, height, QImage::Format_ARGB32);
383 if (!image.isNull()) {
384 pix = pix.fromImage(image);
386 QPainter painter(&pix);
387 painter.drawRect(0, 0, width - 1, height - 1);
389 } else pix.fill(Qt::black);
393 int Render::frameRenderWidth() const
395 return m_mltProfile->width();
398 int Render::renderWidth() const
400 return (int)(m_mltProfile->height() * m_mltProfile->dar() + 0.5);
403 int Render::renderHeight() const
405 return m_mltProfile->height();
408 QImage Render::extractFrame(int frame_position, QString path, int width, int height)
411 width = frameRenderWidth();
412 height = renderHeight();
413 } else if (width % 2 == 1) width++;
414 int dwidth = height * frameRenderWidth() / renderHeight();
415 if (!path.isEmpty()) {
416 Mlt::Producer *producer = new Mlt::Producer(*m_mltProfile, path.toUtf8().constData());
418 if (producer->is_valid()) {
419 QImage img = KThumb::getFrame(producer, frame_position, dwidth, width, height);
423 else delete producer;
427 if (!m_mltProducer || !path.isEmpty()) {
428 QImage pix(width, height, QImage::Format_RGB32);
432 return KThumb::getFrame(m_mltProducer, frame_position, dwidth, width, height);
435 QPixmap Render::getImageThumbnail(KUrl url, int /*width*/, int /*height*/)
439 if (url.fileName().startsWith(".all.")) { // check for slideshow
440 QString fileType = url.fileName().right(3);
442 QStringList::Iterator it;
444 QDir dir(url.directory());
446 filter << "*." + fileType;
447 filter << "*." + fileType.toUpper();
448 more = dir.entryList(filter, QDir::Files);
449 im.load(url.directory() + '/' + more.at(0));
450 } else im.load(url.path());
451 //pixmap = im.scaled(width, height);
455 double Render::consumerRatio() const
457 if (!m_mltConsumer) return 1.0;
458 return (m_mltConsumer->get_double("aspect_ratio_num") / m_mltConsumer->get_double("aspect_ratio_den"));
462 int Render::getLength()
466 // kDebug()<<"////// LENGTH: "<<mlt_producer_get_playtime(m_mltProducer->get_producer());
467 return mlt_producer_get_playtime(m_mltProducer->get_producer());
472 bool Render::isValid(KUrl url)
474 Mlt::Producer producer(*m_mltProfile, url.path().toUtf8().constData());
475 if (producer.is_blank())
481 double Render::dar() const
483 return m_mltProfile->dar();
486 double Render::sar() const
488 return m_mltProfile->sar();
491 void Render::slotSplitView(bool doit)
493 m_isSplitView = doit;
494 Mlt::Service service(m_mltProducer->parent().get_service());
495 Mlt::Tractor tractor(service);
496 if (service.type() != tractor_type || tractor.count() < 2) return;
497 Mlt::Field *field = tractor.field();
499 for (int i = 1, screen = 0; i < tractor.count() && screen < 4; i++) {
500 Mlt::Producer trackProducer(tractor.track(i));
501 kDebug() << "// TRACK: " << i << ", HIDE: " << trackProducer.get("hide");
502 if (QString(trackProducer.get("hide")).toInt() != 1) {
503 kDebug() << "// ADIDNG TRACK: " << i;
504 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
505 transition->set("mlt_service", "composite");
506 transition->set("a_track", 0);
507 transition->set("b_track", i);
508 transition->set("distort", 1);
509 transition->set("internal_added", "200");
516 tmp = "50%,0:50%x50%";
519 tmp = "0,50%:50%x50%";
523 tmp = "50%,50%:50%x50%";
526 transition->set("geometry", tmp);
527 transition->set("always_active", "1");
528 field->plant_transition(*transition, 0, i);
533 m_mltConsumer->set("refresh", 1);
535 mlt_service serv = m_mltProducer->parent().get_service();
536 mlt_service nextservice = mlt_service_get_producer(serv);
537 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
538 QString mlt_type = mlt_properties_get(properties, "mlt_type");
539 QString resource = mlt_properties_get(properties, "mlt_service");
541 while (mlt_type == "transition") {
542 QString added = mlt_properties_get(MLT_SERVICE_PROPERTIES(nextservice), "internal_added");
543 if (added == "200") {
544 mlt_field_disconnect_service(field->get_field(), nextservice);
546 nextservice = mlt_service_producer(nextservice);
547 if (nextservice == NULL) break;
548 properties = MLT_SERVICE_PROPERTIES(nextservice);
549 mlt_type = mlt_properties_get(properties, "mlt_type");
550 resource = mlt_properties_get(properties, "mlt_service");
551 m_mltConsumer->set("refresh", 1);
556 void Render::getFileProperties(const QDomElement &xml, const QString &clipId, int imageHeight, bool replaceProducer)
558 requestClipInfo info;
560 info.clipId = clipId;
561 info.imageHeight = imageHeight;
562 info.replaceProducer = replaceProducer;
563 m_requestList.append(info);
564 if (!m_infoThread.isRunning())
565 m_infoThread = QtConcurrent::run(this, &Render::getFileProperties2);
568 void Render::forceProcessing(const QString &id)
571 for (int i = 0; i < m_requestList.count(); i++) {
572 requestClipInfo info = m_requestList.at(i);
573 if (info.clipId == id) {
576 m_requestList.removeAt(i);
577 m_requestList.prepend(info);
582 m_infoMutex.unlock();
585 int Render::processingItems() const
587 int count = m_requestList.count();
588 if (m_infoThread.isRunning()) count++;
592 void Render::getFileProperties2()
594 while (!m_requestList.isEmpty()) {
596 requestClipInfo info = m_requestList.takeFirst();
597 m_infoMutex.unlock();
602 if (info.xml.hasAttribute("proxy") && info.xml.attribute("proxy") != "-") {
603 path = info.xml.attribute("proxy");
604 proxyProducer = true;
607 path = info.xml.attribute("resource");
608 proxyProducer = false;
612 Mlt::Producer *producer = NULL;
613 CLIPTYPE type = (CLIPTYPE)info.xml.attribute("type").toInt();
614 //kDebug() << "PROFILE WIDT: "<< info.xml.attribute("mlt_service") << ": "<< m_mltProfile->width() << "\n...................\n\n";
615 /*if (info.xml.attribute("type").toInt() == TEXT && !QFile::exists(url.path())) {
616 emit replyGetFileProperties(clipId, producer, QMap < QString, QString >(), QMap < QString, QString >(), replaceProducer);
621 producer = new Mlt::Producer(*m_mltProfile, 0, ("colour:" + info.xml.attribute("colour")).toUtf8().constData());
622 } else if (type == TEXT) {
623 producer = new Mlt::Producer(*m_mltProfile, 0, ("kdenlivetitle:" + info.xml.attribute("resource")).toUtf8().constData());
624 if (producer && producer->is_valid() && info.xml.hasAttribute("xmldata"))
625 producer->set("xmldata", info.xml.attribute("xmldata").toUtf8().constData());
626 } else if (url.isEmpty()) {
628 QDomElement mlt = doc.createElement("mlt");
629 QDomElement play = doc.createElement("playlist");
630 doc.appendChild(mlt);
631 mlt.appendChild(play);
632 play.appendChild(doc.importNode(info.xml, true));
633 producer = new Mlt::Producer(*m_mltProfile, "xml-string", doc.toString().toUtf8().constData());
635 producer = new Mlt::Producer(*m_mltProfile, path.toUtf8().constData());
639 if (producer == NULL || producer->is_blank() || !producer->is_valid()) {
640 kDebug() << " / / / / / / / / ERROR / / / / // CANNOT LOAD PRODUCER: "<<path;
642 // Proxy file is corrupted
643 emit removeInvalidProxy(info.clipId, false);
645 else emit removeInvalidClip(info.clipId, info.replaceProducer);
650 if (proxyProducer && info.xml.hasAttribute("proxy_out")) {
651 producer->set("length", info.xml.attribute("proxy_out").toInt() + 1);
652 producer->set("out", info.xml.attribute("proxy_out").toInt());
653 if (producer->get_out() != info.xml.attribute("proxy_out").toInt()) {
654 // Proxy file length is different than original clip length, this will corrupt project so disable this proxy clip
655 emit removeInvalidProxy(info.clipId, true);
661 if (info.xml.hasAttribute("force_aspect_ratio")) {
662 double aspect = info.xml.attribute("force_aspect_ratio").toDouble();
663 if (aspect > 0) producer->set("force_aspect_ratio", aspect);
666 if (info.xml.hasAttribute("force_aspect_num") && info.xml.hasAttribute("force_aspect_den")) {
667 int width = info.xml.attribute("frame_size").section('x', 0, 0).toInt();
668 int height = info.xml.attribute("frame_size").section('x', 1, 1).toInt();
669 int aspectNumerator = info.xml.attribute("force_aspect_num").toInt();
670 int aspectDenominator = info.xml.attribute("force_aspect_den").toInt();
671 if (aspectDenominator != 0 && width != 0)
672 producer->set("force_aspect_ratio", double(height) * aspectNumerator / aspectDenominator / width);
675 if (info.xml.hasAttribute("force_fps")) {
676 double fps = info.xml.attribute("force_fps").toDouble();
677 if (fps > 0) producer->set("force_fps", fps);
680 if (info.xml.hasAttribute("force_progressive")) {
682 int progressive = info.xml.attribute("force_progressive").toInt(&ok);
683 if (ok) producer->set("force_progressive", progressive);
685 if (info.xml.hasAttribute("force_tff")) {
687 int fieldOrder = info.xml.attribute("force_tff").toInt(&ok);
688 if (ok) producer->set("force_tff", fieldOrder);
690 if (info.xml.hasAttribute("threads")) {
691 int threads = info.xml.attribute("threads").toInt();
692 if (threads != 1) producer->set("threads", threads);
694 if (info.xml.hasAttribute("video_index")) {
695 int vindex = info.xml.attribute("video_index").toInt();
696 if (vindex != 0) producer->set("video_index", vindex);
698 if (info.xml.hasAttribute("audio_index")) {
699 int aindex = info.xml.attribute("audio_index").toInt();
700 if (aindex != 0) producer->set("audio_index", aindex);
702 if (info.xml.hasAttribute("force_colorspace")) {
703 int colorspace = info.xml.attribute("force_colorspace").toInt();
704 if (colorspace != 0) producer->set("force_colorspace", colorspace);
706 if (info.xml.hasAttribute("full_luma")) {
707 int full_luma = info.xml.attribute("full_luma").toInt();
708 if (full_luma != 0) producer->set("set.force_full_luma", full_luma);
713 if (info.xml.hasAttribute("out")) clipOut = info.xml.attribute("out").toInt();
715 // setup length here as otherwise default length (currently 15000 frames in MLT) will be taken even if outpoint is larger
716 if (type == COLOR || type == TEXT || type == IMAGE || type == SLIDESHOW) {
718 if (info.xml.hasAttribute("length")) {
719 if (clipOut > 0) duration = clipOut + 1;
720 length = info.xml.attribute("length").toInt();
721 clipOut = length - 1;
723 else length = info.xml.attribute("out").toInt() - info.xml.attribute("in").toInt();
724 producer->set("length", length);
727 if (clipOut > 0) producer->set_in_and_out(info.xml.attribute("in").toInt(), clipOut);
729 producer->set("id", info.clipId.toUtf8().constData());
731 if (info.xml.hasAttribute("templatetext"))
732 producer->set("templatetext", info.xml.attribute("templatetext").toUtf8().constData());
734 if ((!info.replaceProducer && info.xml.hasAttribute("file_hash")) || info.xml.hasAttribute("proxy")) {
735 // Clip already has all properties
736 if (info.replaceProducer) emit blockClipMonitor(info.clipId);
737 emit replyGetFileProperties(info.clipId, producer, stringMap(), stringMap(), info.replaceProducer, true);
741 int imageWidth = (int)((double) info.imageHeight * m_mltProfile->width() / m_mltProfile->height() + 0.5);
742 int fullWidth = (int)((double) info.imageHeight * m_mltProfile->dar() + 0.5);
743 stringMap filePropertyMap;
744 stringMap metadataPropertyMap;
746 int frameNumber = info.xml.attribute("thumbnail", "0").toInt();
747 if (frameNumber != 0) producer->seek(frameNumber);
749 filePropertyMap["duration"] = QString::number(duration > 0 ? duration : producer->get_playtime());
750 //kDebug() << "/////// PRODUCER: " << url.path() << " IS: " << producer->get_playtime();
752 Mlt::Frame *frame = producer->get_frame();
754 if (type == SLIDESHOW) {
755 int ttl = info.xml.hasAttribute("ttl") ? info.xml.attribute("ttl").toInt() : 0;
756 if (ttl) producer->set("ttl", ttl);
757 if (!info.xml.attribute("animation").isEmpty()) {
758 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "affine");
759 if (filter && filter->is_valid()) {
761 QString geometry = SlideshowClip::animationToGeometry(info.xml.attribute("animation"), cycle);
762 if (!geometry.isEmpty()) {
763 if (info.xml.attribute("animation").contains("low-pass")) {
764 Mlt::Filter *blur = new Mlt::Filter(*m_mltProfile, "boxblur");
765 if (blur && blur->is_valid())
766 producer->attach(*blur);
768 filter->set("transition.geometry", geometry.toUtf8().data());
769 filter->set("transition.cycle", cycle);
770 producer->attach(*filter);
774 if (info.xml.attribute("fade") == "1") {
775 // user wants a fade effect to slideshow
776 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "luma");
777 if (filter && filter->is_valid()) {
778 if (ttl) filter->set("cycle", ttl);
779 if (info.xml.hasAttribute("luma_duration") && !info.xml.attribute("luma_duration").isEmpty()) filter->set("duration", info.xml.attribute("luma_duration").toInt());
780 if (info.xml.hasAttribute("luma_file") && !info.xml.attribute("luma_file").isEmpty()) {
781 filter->set("luma.resource", info.xml.attribute("luma_file").toUtf8().constData());
782 if (info.xml.hasAttribute("softness")) {
783 int soft = info.xml.attribute("softness").toInt();
784 filter->set("luma.softness", (double) soft / 100.0);
787 producer->attach(*filter);
790 if (info.xml.attribute("crop") == "1") {
791 // user wants to center crop the slides
792 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "crop");
793 if (filter && filter->is_valid()) {
794 filter->set("center", 1);
795 producer->attach(*filter);
800 if (producer->get_double("meta.media.frame_rate_den") > 0) {
801 filePropertyMap["fps"] = locale.toString(producer->get_double("meta.media.frame_rate_num") / producer->get_double("meta.media.frame_rate_den"));
802 } else filePropertyMap["fps"] = producer->get("source_fps");
804 if (frame && frame->is_valid()) {
805 filePropertyMap["frame_size"] = QString::number(frame->get_int("width")) + 'x' + QString::number(frame->get_int("height"));
806 filePropertyMap["frequency"] = QString::number(frame->get_int("frequency"));
807 filePropertyMap["channels"] = QString::number(frame->get_int("channels"));
808 filePropertyMap["aspect_ratio"] = frame->get("aspect_ratio");
810 if (frame->get_int("test_image") == 0) {
811 if (url.path().endsWith(".mlt") || url.path().endsWith(".westley") || url.path().endsWith(".kdenlive")) {
812 filePropertyMap["type"] = "playlist";
813 metadataPropertyMap["comment"] = QString::fromUtf8(producer->get("title"));
814 } else if (frame->get_int("test_audio") == 0)
815 filePropertyMap["type"] = "av";
817 filePropertyMap["type"] = "video";
823 img = KThumb::getFrame(frame, imageWidth, fullWidth, info.imageHeight);
824 variance = KThumb::imageVariance(img);
825 if (frameNumber == 0 && variance< 6) {
826 // Thumbnail is not interesting (for example all black, seek to fetch better thumb
828 producer->seek(frameNumber);
830 frame = producer->get_frame();
833 } while (variance == -1);
834 emit replyGetImage(info.clipId, img);
835 } else if (frame->get_int("test_audio") == 0) {
836 emit replyGetImage(info.clipId, "audio-x-generic", fullWidth, info.imageHeight);
837 filePropertyMap["type"] = "audio";
841 // Retrieve audio / video codec name
845 if (producer->get_int("video_index") > -1) {
846 /*if (context->duration == AV_NOPTS_VALUE) {
847 kDebug() << " / / / / / / / /ERROR / / / CLIP HAS UNKNOWN DURATION";
848 emit removeInvalidClip(clipId);
852 // Get the video_index
853 int default_video = producer->get_int("video_index");
855 int default_audio = producer->get_int("audio_index");
858 // Find maximum stream index values
859 for (int ix = 0; ix < producer->get_int("meta.media.nb_streams"); ix++) {
860 snprintf(property, sizeof(property), "meta.media.%d.stream.type", ix);
861 QString type = producer->get(property);
864 else if (type == "audio")
867 filePropertyMap["default_video"] = QString::number(default_video);
868 filePropertyMap["video_max"] = QString::number(video_max);
869 filePropertyMap["default_audio"] = QString::number(default_audio);
870 filePropertyMap["audio_max"] = QString::number(audio_max);
872 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", default_video);
873 if (producer->get(property)) {
874 filePropertyMap["videocodec"] = producer->get(property);
876 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
877 if (producer->get(property))
878 filePropertyMap["videocodec"] = producer->get(property);
881 query = QString("meta.media.%1.codec.pix_fmt").arg(default_video);
882 filePropertyMap["pix_fmt"] = producer->get(query.toUtf8().constData());
883 filePropertyMap["colorspace"] = producer->get("meta.media.colorspace");
885 } else kDebug() << " / / / / /WARNING, VIDEO CONTEXT IS NULL!!!!!!!!!!!!!!";
886 if (producer->get_int("audio_index") > -1) {
887 // Get the audio_index
888 int index = producer->get_int("audio_index");
890 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", index);
891 if (producer->get(property)) {
892 filePropertyMap["audiocodec"] = producer->get(property);
894 snprintf(property, sizeof(property), "meta.media.%d.codec.name", index);
895 if (producer->get(property))
896 filePropertyMap["audiocodec"] = producer->get(property);
901 Mlt::Properties metadata;
902 metadata.pass_values(*producer, "meta.attr.");
903 int count = metadata.count();
904 for (int i = 0; i < count; i ++) {
905 QString name = metadata.get_name(i);
906 QString value = QString::fromUtf8(metadata.get(i));
907 if (name.endsWith("markup") && !value.isEmpty())
908 metadataPropertyMap[ name.section('.', 0, -2)] = value;
911 if (info.replaceProducer) emit blockClipMonitor(info.clipId);
912 emit replyGetFileProperties(info.clipId, producer, filePropertyMap, metadataPropertyMap, info.replaceProducer);
913 // FIXME: should delete this to avoid a leak...
920 /** Create the producer from the MLT XML QDomDocument */
921 void Render::initSceneList()
923 kDebug() << "-------- INIT SCENE LIST ------_";
925 QDomElement mlt = doc.createElement("mlt");
926 doc.appendChild(mlt);
927 QDomElement prod = doc.createElement("producer");
928 prod.setAttribute("resource", "colour");
929 prod.setAttribute("colour", "red");
930 prod.setAttribute("id", "black");
931 prod.setAttribute("in", "0");
932 prod.setAttribute("out", "0");
934 QDomElement tractor = doc.createElement("tractor");
935 QDomElement multitrack = doc.createElement("multitrack");
937 QDomElement playlist1 = doc.createElement("playlist");
938 playlist1.appendChild(prod);
939 multitrack.appendChild(playlist1);
940 QDomElement playlist2 = doc.createElement("playlist");
941 multitrack.appendChild(playlist2);
942 QDomElement playlist3 = doc.createElement("playlist");
943 multitrack.appendChild(playlist3);
944 QDomElement playlist4 = doc.createElement("playlist");
945 multitrack.appendChild(playlist4);
946 QDomElement playlist5 = doc.createElement("playlist");
947 multitrack.appendChild(playlist5);
948 tractor.appendChild(multitrack);
949 mlt.appendChild(tractor);
950 // kDebug()<<doc.toString();
952 QString tmp = QString("<mlt><producer resource=\"colour\" colour=\"red\" id=\"red\" /><tractor><multitrack><playlist></playlist><playlist></playlist><playlist /><playlist /><playlist></playlist></multitrack></tractor></mlt>");*/
953 setSceneList(doc, 0);
957 int Render::setProducer(Mlt::Producer *producer, int position)
959 QMutexLocker locker(&m_mutex);
960 if (m_winid == -1) return -1;
962 if (!m_mltConsumer->is_stopped()) {
963 m_mltConsumer->stop();
965 m_mltConsumer->set("refresh", 0);
972 m_mltProducer->set_speed(0);
973 delete m_mltProducer;
974 m_mltProducer = NULL;
978 if (producer && producer->is_valid()) {
979 m_mltProducer = new Mlt::Producer(producer->get_producer());
980 } else m_mltProducer = m_blackClip->cut(0, 50);
982 if (!m_mltProducer || !m_mltProducer->is_valid()) {
983 kDebug() << " WARNING - - - - -INVALID PLAYLIST: ";
986 int volume = KdenliveSettings::volume();
987 m_mltProducer->set("meta.volume", (double)volume / 100);
988 m_fps = m_mltProducer->get_fps();
990 int error = connectPlaylist();
992 if (position != -1) {
993 m_mltProducer->seek(position);
994 emit rendererPosition(position);
995 } else emit rendererPosition((int) m_mltProducer->position());
999 int Render::setSceneList(QDomDocument list, int position)
1001 return setSceneList(list.toString(), position);
1004 int Render::setSceneList(QString playlist, int position)
1006 if (m_winid == -1) return -1;
1009 //kDebug() << "////// RENDER, SET SCENE LIST:\n" << playlist <<"\n..........:::.";
1011 // Remove previous profile info
1013 doc.setContent(playlist);
1014 QDomElement profile = doc.documentElement().firstChildElement("profile");
1015 doc.documentElement().removeChild(profile);
1016 playlist = doc.toString();
1018 if (m_mltConsumer) {
1019 if (!m_mltConsumer->is_stopped()) {
1020 m_mltConsumer->stop();
1022 m_mltConsumer->set("refresh", 0);
1024 kWarning() << "/////// ERROR, TRYING TO USE NULL MLT CONSUMER";
1027 m_requestList.clear();
1028 m_infoThread.waitForFinished();
1030 if (m_mltProducer) {
1031 m_mltProducer->set_speed(0);
1032 //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
1034 /*Mlt::Service service(m_mltProducer->parent().get_service());
1035 mlt_service_lock(service.get_service());
1037 if (service.type() == tractor_type) {
1038 Mlt::Tractor tractor(service);
1039 Mlt::Field *field = tractor.field();
1040 mlt_service nextservice = mlt_service_get_producer(service.get_service());
1041 mlt_service nextservicetodisconnect;
1042 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
1043 QString mlt_type = mlt_properties_get(properties, "mlt_type");
1044 QString resource = mlt_properties_get(properties, "mlt_service");
1045 // Delete all transitions
1046 while (mlt_type == "transition") {
1047 nextservicetodisconnect = nextservice;
1048 nextservice = mlt_service_producer(nextservice);
1049 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
1050 if (nextservice == NULL) break;
1051 properties = MLT_SERVICE_PROPERTIES(nextservice);
1052 mlt_type = mlt_properties_get(properties, "mlt_type");
1053 resource = mlt_properties_get(properties, "mlt_service");
1057 for (int trackNb = tractor.count() - 1; trackNb >= 0; --trackNb) {
1058 Mlt::Producer trackProducer(tractor.track(trackNb));
1059 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1060 if (trackPlaylist.type() == playlist_type) trackPlaylist.clear();
1064 mlt_service_unlock(service.get_service());*/
1066 qDeleteAll(m_slowmotionProducers.values());
1067 m_slowmotionProducers.clear();
1069 delete m_mltProducer;
1070 m_mltProducer = NULL;
1075 m_locale = QLocale();
1077 m_mltProducer = new Mlt::Producer(*m_mltProfile, "xml-string", playlist.toUtf8().constData());
1078 if (!m_mltProducer || !m_mltProducer->is_valid()) {
1079 kDebug() << " WARNING - - - - -INVALID PLAYLIST: " << playlist.toUtf8().constData();
1080 m_mltProducer = m_blackClip->cut(0, 50);
1084 int volume = KdenliveSettings::volume();
1085 m_mltProducer->set("meta.volume", (double)volume / 100);
1086 m_mltProducer->optimise();
1088 /*if (KdenliveSettings::osdtimecode()) {
1089 // Attach filter for on screen display of timecode
1091 QString attr = "attr_check";
1092 mlt_filter filter = mlt_factory_filter( "data_feed", (char*) attr.ascii() );
1093 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
1094 mlt_producer_attach( m_mltProducer->get_producer(), filter );
1095 mlt_filter_close( filter );
1097 m_osdInfo = new Mlt::Filter("data_show");
1098 m_osdInfo->set("resource", m_osdProfile.toUtf8().constData());
1099 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1100 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
1101 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
1102 m_osdInfo->set("dynamic", "1");
1104 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1106 m_osdInfo->set("dynamic", "0");
1109 m_fps = m_mltProducer->get_fps();
1110 if (position != 0) {
1111 // Seek to correct place after opening project.
1112 m_mltProducer->seek(position);
1115 kDebug() << "// NEW SCENE LIST DURATION SET TO: " << m_mltProducer->get_playtime();
1116 if (error == 0) error = connectPlaylist();
1117 else connectPlaylist();
1118 fillSlowMotionProducers();
1119 blockSignals(false);
1122 //kDebug()<<"// SETSCN LST, POS: "<<position;
1123 //if (position != 0) emit rendererPosition(position);
1126 void Render::checkMaxThreads()
1128 // Make sure we don't use too much threads, MLT avformat does not cope with too much threads
1129 // Currently, Kdenlive uses the following avformat threads:
1130 // One thread to get info when adding a clip
1131 // One thread to create the timeline video thumbnails
1132 // One thread to create the audio thumbnails
1133 Mlt::Service service(m_mltProducer->parent().get_service());
1134 if (service.type() != tractor_type) {
1135 kWarning() << "// TRACTOR PROBLEM";
1138 Mlt::Tractor tractor(service);
1139 int mltMaxThreads = mlt_service_cache_get_size(service.get_service(), "producer_avformat");
1140 int requestedThreads = tractor.count() + 4;
1141 if (requestedThreads > mltMaxThreads) {
1142 mlt_service_cache_set_size(service.get_service(), "producer_avformat", requestedThreads);
1143 kDebug()<<"// MLT threads updated to: "<<mlt_service_cache_get_size(service.get_service(), "producer_avformat");
1147 const QString Render::sceneList()
1150 Mlt::Profile profile((mlt_profile) 0);
1151 Mlt::Consumer xmlConsumer(profile, "xml:kdenlive_playlist");
1152 m_mltProducer->optimise();
1153 xmlConsumer.set("terminate_on_pause", 1);
1154 Mlt::Producer prod(m_mltProducer->get_producer());
1155 bool split = m_isSplitView;
1156 if (split) slotSplitView(false);
1157 xmlConsumer.connect(prod);
1158 xmlConsumer.start();
1159 while (!xmlConsumer.is_stopped()) {}
1160 playlist = QString::fromUtf8(xmlConsumer.get("kdenlive_playlist"));
1161 if (split) slotSplitView(true);
1165 bool Render::saveSceneList(QString path, QDomElement kdenliveData)
1169 doc.setContent(sceneList(), false);
1170 if (!kdenliveData.isNull()) {
1171 // add Kdenlive specific tags
1172 QDomNode mlt = doc.elementsByTagName("mlt").at(0);
1173 mlt.appendChild(doc.importNode(kdenliveData, true));
1175 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
1176 kWarning() << "////// ERROR writing to file: " << path;
1179 file.write(doc.toString().toUtf8());
1180 if (file.error() != QFile::NoError) {
1188 void Render::saveZone(KUrl url, QString desc, QPoint zone)
1190 Mlt::Consumer xmlConsumer(*m_mltProfile, ("xml:" + url.path()).toUtf8().constData());
1191 m_mltProducer->optimise();
1192 xmlConsumer.set("terminate_on_pause", 1);
1193 if (m_name == "clip") {
1194 Mlt::Producer *prod = m_mltProducer->cut(zone.x(), zone.y());
1196 list.insert_at(0, prod, 0);
1198 list.set("title", desc.toUtf8().constData());
1199 xmlConsumer.connect(list);
1202 //TODO: not working yet, save zone from timeline
1203 Mlt::Producer *p1 = new Mlt::Producer(m_mltProducer->get_producer());
1204 /* Mlt::Service service(p1->parent().get_service());
1205 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";*/
1207 //Mlt::Producer *prod = p1->cut(zone.x(), zone.y());
1208 //prod->set("title", desc.toUtf8().constData());
1209 xmlConsumer.connect(*p1); //list);
1212 xmlConsumer.start();
1215 double Render::fps() const
1220 int Render::connectPlaylist()
1222 if (!m_mltConsumer) return -1;
1223 //m_mltConsumer->set("refresh", "0");
1224 m_mltConsumer->connect(*m_mltProducer);
1225 m_mltProducer->set_speed(0);
1226 if (m_mltConsumer->start() == -1) {
1227 // ARGH CONSUMER BROKEN!!!!
1228 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."));
1229 delete m_mltConsumer;
1230 m_mltConsumer = NULL;
1233 emit durationChanged(m_mltProducer->get_playtime());
1238 void Render::refreshDisplay()
1241 if (!m_mltProducer) return;
1242 //m_mltConsumer->set("refresh", 0);
1244 //mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1245 /*if (KdenliveSettings::osdtimecode()) {
1246 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
1247 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
1248 m_osdInfo->set("dynamic", "1");
1249 m_mltProducer->attach(*m_osdInfo);
1252 m_mltProducer->detach(*m_osdInfo);
1253 m_osdInfo->set("dynamic", "0");
1258 int Render::volume() const
1260 if (!m_mltConsumer || !m_mltProducer) return -1;
1261 return ((int) 100 * m_mltProducer->get_double("meta.volume"));
1264 void Render::slotSetVolume(int volume)
1266 if (!m_mltConsumer || !m_mltProducer) return;
1267 m_mltProducer->set("meta.volume", (double)volume / 100.0);
1270 m_mltConsumer->set("refresh", 0);
1271 // Attach filter for on screen display of timecode
1272 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1273 mlt_properties_set_double( properties, "meta.volume", volume );
1274 mlt_properties_set_int( properties, "meta.attr.osdvolume", 1);
1275 mlt_properties_set( properties, "meta.attr.osdvolume.markup", i18n("Volume: ") + QString::number(volume * 100));
1277 if (!KdenliveSettings::osdtimecode()) {
1278 m_mltProducer->detach(*m_osdInfo);
1279 mlt_properties_set_int( properties, "meta.attr.timecode", 0);
1280 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1283 //m_osdTimer->setSingleShot(2500);
1286 void Render::slotOsdTimeout()
1288 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1289 mlt_properties_set_int(properties, "meta.attr.osdvolume", 0);
1290 mlt_properties_set(properties, "meta.attr.osdvolume.markup", NULL);
1291 //if (!KdenliveSettings::osdtimecode()) m_mltProducer->detach(*m_osdInfo);
1295 void Render::start()
1297 if (m_winid == -1) {
1298 kDebug() << "----- BROKEN MONITOR: " << m_name << ", RESTART";
1301 if (m_mltConsumer && m_mltConsumer->is_stopped()) {
1302 if (m_mltConsumer->start() == -1) {
1303 //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."));
1304 kDebug(QtWarningMsg) << "/ / / / CANNOT START MONITOR";
1313 if (m_mltProducer == NULL) return;
1314 if (m_mltConsumer && !m_mltConsumer->is_stopped()) {
1315 //kDebug() << "///////////// RENDER STOPPED: " << m_name;
1316 //m_mltConsumer->set("refresh", 0);
1317 m_mltConsumer->stop();
1318 // delete m_mltConsumer;
1319 // m_mltConsumer = NULL;
1322 if (m_mltProducer) {
1323 if (m_isZoneMode) resetZoneMode();
1324 m_mltProducer->set_speed(0.0);
1325 //m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1326 //kDebug() << m_mltProducer->get_length();
1330 void Render::stop(const GenTime & startTime)
1332 if (m_mltProducer) {
1333 if (m_isZoneMode) resetZoneMode();
1334 m_mltProducer->set_speed(0.0);
1335 m_mltProducer->seek((int) startTime.frames(m_fps));
1337 m_mltConsumer->purge();
1340 void Render::pause()
1342 if (!m_mltProducer || !m_mltConsumer)
1344 if (m_mltProducer->get_speed() == 0.0) return;
1345 if (m_isZoneMode) resetZoneMode();
1346 m_mltConsumer->set("refresh", 0);
1347 m_mltProducer->set_speed(0.0);
1349 The 2 lines below create a flicker loop
1350 emit rendererPosition(m_framePosition);
1351 m_mltProducer->seek(m_framePosition);*/
1352 m_mltConsumer->purge();
1355 void Render::switchPlay(bool play)
1357 if (!m_mltProducer || !m_mltConsumer)
1359 if (m_isZoneMode) resetZoneMode();
1360 if (play && m_mltProducer->get_speed() == 0.0) {
1361 if (m_name == "clip" && m_framePosition == (int) m_mltProducer->get_out()) m_mltProducer->seek(0);
1362 m_mltProducer->set_speed(1.0);
1363 m_mltConsumer->set("refresh", 1);
1364 if (m_mltConsumer->is_stopped()) m_mltConsumer->start();
1367 m_mltConsumer->set("refresh", "0");
1369 m_mltProducer->seek(m_framePosition);
1370 /*m_mltConsumer->set("refresh", 0);
1371 m_mltConsumer->stop();
1372 m_mltConsumer->purge();
1373 m_mltProducer->set_speed(0.0);
1374 //m_framePosition = m_mltProducer->position();
1375 m_mltProducer->seek(m_framePosition);
1376 emit rendererPosition(m_framePosition);*/
1380 void Render::play(double speed)
1384 // if (speed == 0.0) m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1385 m_mltProducer->set_speed(speed);
1386 /*if (speed == 0.0) {
1387 m_mltProducer->seek((int) m_framePosition + 1);
1388 m_mltConsumer->purge();
1393 void Render::play(const GenTime & startTime)
1395 if (!m_mltProducer || !m_mltConsumer)
1397 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1398 m_mltProducer->set_speed(1.0);
1399 m_mltConsumer->set("refresh", 1);
1402 void Render::loopZone(const GenTime & startTime, const GenTime & stopTime)
1404 if (!m_mltProducer || !m_mltConsumer)
1406 //m_mltProducer->set("eof", "loop");
1407 m_isLoopMode = true;
1408 m_loopStart = startTime;
1409 playZone(startTime, stopTime);
1412 void Render::playZone(const GenTime & startTime, const GenTime & stopTime)
1414 if (!m_mltProducer || !m_mltConsumer)
1416 if (!m_isZoneMode) m_originalOut = m_mltProducer->get_playtime() - 1;
1417 m_mltProducer->set("out", (int)(stopTime.frames(m_fps)));
1418 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1419 m_mltProducer->set_speed(1.0);
1420 m_mltConsumer->set("refresh", 1);
1421 m_isZoneMode = true;
1424 void Render::resetZoneMode()
1426 if (!m_isZoneMode && !m_isLoopMode) return;
1427 m_mltProducer->set("out", m_originalOut);
1428 //m_mltProducer->set("eof", "pause");
1429 m_isZoneMode = false;
1430 m_isLoopMode = false;
1433 void Render::seekToFrame(int pos)
1438 m_mltProducer->seek(pos);
1442 void Render::seekToFrameDiff(int diff)
1447 m_mltProducer->seek(m_mltProducer->position() + diff);
1451 void Render::doRefresh()
1453 // Use a Timer so that we don't refresh too much
1454 if (m_mltConsumer) {
1455 if (m_mltConsumer->is_stopped()) m_mltConsumer->start();
1456 m_mltConsumer->set("refresh", 1);
1460 void Render::refresh()
1464 if (m_mltConsumer) {
1465 m_mltConsumer->set("refresh", 1);
1466 if (m_mltConsumer->is_stopped()) m_mltConsumer->start();
1470 void Render::setDropFrames(bool show)
1472 if (m_mltConsumer) {
1473 int dropFrames = KdenliveSettings::mltthreads();
1474 if (show == false) dropFrames = -dropFrames;
1475 m_mltConsumer->stop();
1477 m_mltConsumer->set("real_time", dropFrames);
1479 m_mltConsumer->set("play.real_time", dropFrames);
1481 if (m_mltConsumer->start() == -1) {
1482 kDebug(QtWarningMsg) << "ERROR, Cannot start monitor";
1488 double Render::playSpeed()
1490 if (m_mltProducer) return m_mltProducer->get_speed();
1494 GenTime Render::seekPosition() const
1496 if (m_mltProducer) return GenTime((int) m_mltProducer->position(), m_fps);
1497 else return GenTime();
1500 int Render::seekFramePosition() const
1502 if (m_mltProducer) return (int) m_mltProducer->position();
1506 const QString & Render::rendererName() const
1511 void Render::emitFrameUpdated(Mlt::Frame& frame)
1513 mlt_image_format format = mlt_image_rgb24;
1516 const uchar* image = frame.get_image(format, width, height);
1517 QImage qimage(width, height, QImage::Format_RGB888);
1518 memcpy(qimage.bits(), image, width * height * 3);
1519 emit frameUpdated(qimage);
1522 void Render::emitFrameNumber(double position)
1524 m_framePosition = position;
1525 emit rendererPosition((int) position);
1528 void Render::emitConsumerStopped()
1530 // This is used to know when the playing stopped
1531 if (m_mltProducer) {
1532 double pos = m_mltProducer->position();
1533 if (m_isLoopMode) play(m_loopStart);
1534 else if (m_isZoneMode) resetZoneMode();
1535 emit rendererStopped((int) pos);
1539 void Render::exportFileToFirewire(QString /*srcFileName*/, int /*port*/, GenTime /*startTime*/, GenTime /*endTime*/)
1541 KMessageBox::sorry(0, i18n("Firewire is not enabled on your system.\n Please install Libiec61883 and recompile Kdenlive"));
1544 void Render::exportCurrentFrame(KUrl url, bool /*notify*/)
1546 if (!m_mltProducer) {
1547 KMessageBox::sorry(qApp->activeWindow(), i18n("There is no clip, cannot extract frame."));
1551 //int height = 1080;//KdenliveSettings::defaultheight();
1552 //int width = 1940; //KdenliveSettings::displaywidth();
1554 QPixmap pix; // = KThumb::getFrame(m_mltProducer, -1, width, height);
1556 QPixmap pix(width, height);
1557 Mlt::Filter m_convert(*m_mltProfile, "avcolour_space");
1558 m_convert.set("forced", mlt_image_rgb24a);
1559 m_mltProducer->attach(m_convert);
1560 Mlt::Frame * frame = m_mltProducer->get_frame();
1561 m_mltProducer->detach(m_convert);
1563 pix = frameThumbnail(frame, width, height);
1566 pix.save(url.path(), "PNG");
1567 //if (notify) QApplication::postEvent(qApp->activeWindow(), new UrlEvent(url, 10003));
1571 void Render::showFrame(Mlt::Frame& frame)
1573 m_framePosition = qMax(frame.get_int("_position"), 0);
1574 emit rendererPosition((int) m_framePosition);
1575 mlt_image_format format = mlt_image_rgb24a;
1578 const uchar* image = frame.get_image(format, width, height);
1579 QImage qimage(width, height, QImage::Format_ARGB32_Premultiplied);
1580 memcpy(qimage.scanLine(0), image, width * height * 4);
1581 emit showImageSignal(qimage);
1582 if (analyseAudio) showAudio(frame);
1583 if (sendFrameForAnalysis && frame.get_frame()->convert_image) {
1584 emit frameUpdated(qimage.rgbSwapped());
1588 void Render::showAudio(Mlt::Frame& frame)
1590 if (!frame.is_valid() || frame.get_int("test_audio") != 0) {
1593 mlt_audio_format audio_format = mlt_audio_s16;
1595 int num_channels = 0;
1597 int16_t* data = (int16_t*)frame.get_audio(audio_format, freq, num_channels, samples);
1603 // Data format: [ c00 c10 c01 c11 c02 c12 c03 c13 ... c0{samples-1} c1{samples-1} for 2 channels.
1604 // So the vector is of size samples*channels.
1605 QVector<int16_t> sampleVector(samples*num_channels);
1606 memcpy(sampleVector.data(), data, samples*num_channels*sizeof(int16_t));
1609 emit audioSamplesSignal(sampleVector, freq, num_channels, samples);
1614 * MLT playlist direct manipulation.
1617 void Render::mltCheckLength(Mlt::Tractor *tractor)
1619 //kDebug()<<"checking track length: "<<track<<"..........";
1621 int trackNb = tractor->count();
1625 Mlt::Producer trackProducer(tractor->track(0));
1626 duration = trackProducer.get_playtime() - 1;
1627 m_mltProducer->set("out", duration);
1628 emit durationChanged(duration);
1631 while (trackNb > 1) {
1632 Mlt::Producer trackProducer(tractor->track(trackNb - 1));
1633 trackDuration = trackProducer.get_playtime() - 1;
1634 // kDebug() << " / / /DURATON FOR TRACK " << trackNb - 1 << " = " << trackDuration;
1635 if (trackDuration > duration) duration = trackDuration;
1639 Mlt::Producer blackTrackProducer(tractor->track(0));
1641 if (blackTrackProducer.get_playtime() - 1 != duration) {
1642 Mlt::Playlist blackTrackPlaylist((mlt_playlist) blackTrackProducer.get_service());
1643 Mlt::Producer *blackclip = blackTrackPlaylist.get_clip(0);
1644 if (blackclip && blackclip->is_blank()) {
1649 if (blackclip == NULL || blackTrackPlaylist.count() != 1) {
1650 blackTrackPlaylist.clear();
1651 m_blackClip->set("length", duration + 1);
1652 m_blackClip->set("out", duration);
1653 blackclip = m_blackClip->cut(0, duration);
1654 blackTrackPlaylist.insert_at(0, blackclip, 1);
1656 if (duration > blackclip->parent().get_length()) {
1657 blackclip->parent().set("length", duration + 1);
1658 blackclip->parent().set("out", duration);
1659 blackclip->set("length", duration + 1);
1661 blackTrackPlaylist.resize_clip(0, 0, duration);
1665 m_mltProducer->set("out", duration);
1666 emit durationChanged(duration);
1670 Mlt::Producer *Render::checkSlowMotionProducer(Mlt::Producer *prod, QDomElement element)
1672 if (element.attribute("speed", "1.0").toDouble() == 1.0 && element.attribute("strobe", "1").toInt() == 1) return prod;
1674 // We want a slowmotion producer
1675 double speed = element.attribute("speed", "1.0").toDouble();
1676 int strobe = element.attribute("strobe", "1").toInt();
1677 QString url = QString::fromUtf8(prod->get("resource"));
1678 url.append('?' + locale.toString(speed));
1679 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
1680 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1681 if (!slowprod || slowprod->get_producer() == NULL) {
1682 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
1683 if (strobe > 1) slowprod->set("strobe", strobe);
1684 QString id = prod->parent().get("id");
1685 if (id.contains('_')) id = id.section('_', 0, 0);
1686 QString producerid = "slowmotion:" + id + ':' + locale.toString(speed);
1687 if (strobe > 1) producerid.append(':' + QString::number(strobe));
1688 slowprod->set("id", producerid.toUtf8().constData());
1689 m_slowmotionProducers.insert(url, slowprod);
1694 int Render::mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod, bool overwrite, bool push)
1696 if (m_mltProducer == NULL) {
1697 kDebug() << "PLAYLIST NOT INITIALISED //////";
1701 kDebug() << "Cannot insert clip without producer //////";
1704 Mlt::Producer parentProd(m_mltProducer->parent());
1705 if (parentProd.get_producer() == NULL) {
1706 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1710 Mlt::Service service(parentProd.get_service());
1711 if (service.type() != tractor_type) {
1712 kWarning() << "// TRACTOR PROBLEM";
1715 Mlt::Tractor tractor(service);
1716 if (info.track > tractor.count() - 1) {
1717 kDebug() << "ERROR TRYING TO INSERT CLIP ON TRACK " << info.track << ", at POS: " << info.startPos.frames(25);
1720 mlt_service_lock(service.get_service());
1721 Mlt::Producer trackProducer(tractor.track(info.track));
1722 int trackDuration = trackProducer.get_playtime() - 1;
1723 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1724 //kDebug()<<"/// INSERT cLIP: "<<info.cropStart.frames(m_fps)<<", "<<info.startPos.frames(m_fps)<<"-"<<info.endPos.frames(m_fps);
1725 prod = checkSlowMotionProducer(prod, element);
1726 if (prod == NULL || !prod->is_valid()) {
1727 mlt_service_unlock(service.get_service());
1731 int cutPos = (int) info.cropStart.frames(m_fps);
1732 if (cutPos < 0) cutPos = 0;
1733 int insertPos = (int) info.startPos.frames(m_fps);
1734 int cutDuration = (int)(info.endPos - info.startPos).frames(m_fps) - 1;
1735 Mlt::Producer *clip = prod->cut(cutPos, cutDuration + cutPos);
1736 if (overwrite && (insertPos < trackDuration)) {
1737 // Replace zone with blanks
1738 //trackPlaylist.split_at(insertPos, true);
1739 trackPlaylist.remove_region(insertPos, cutDuration + 1);
1740 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1741 trackPlaylist.insert_blank(clipIndex, cutDuration);
1743 trackPlaylist.split_at(insertPos, true);
1744 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1745 trackPlaylist.insert_blank(clipIndex, cutDuration);
1747 int newIndex = trackPlaylist.insert_at(insertPos, clip, 1);
1749 /*if (QString(prod->get("transparency")).toInt() == 1)
1750 mltAddClipTransparency(info, info.track - 1, QString(prod->get("id")).toInt());*/
1752 if (info.track != 0 && (newIndex + 1 == trackPlaylist.count())) mltCheckLength(&tractor);
1753 mlt_service_unlock(service.get_service());
1754 /*tractor.multitrack()->refresh();
1755 tractor.refresh();*/
1760 void Render::mltCutClip(int track, GenTime position)
1762 Mlt::Service service(m_mltProducer->parent().get_service());
1763 if (service.type() != tractor_type) {
1764 kWarning() << "// TRACTOR PROBLEM";
1768 Mlt::Tractor tractor(service);
1769 Mlt::Producer trackProducer(tractor.track(track));
1770 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1773 /* // Display playlist info
1774 kDebug()<<"//////////// BEFORE";
1775 for (int i = 0; i < trackPlaylist.count(); i++) {
1776 int blankStart = trackPlaylist.clip_start(i);
1777 int blankDuration = trackPlaylist.clip_length(i) - 1;
1779 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1780 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1783 int cutPos = (int) position.frames(m_fps);
1785 int clipIndex = trackPlaylist.get_clip_index_at(cutPos);
1786 if (trackPlaylist.is_blank(clipIndex)) {
1787 kDebug() << "// WARNING, TRYING TO CUT A BLANK";
1790 mlt_service_lock(service.get_service());
1791 int clipStart = trackPlaylist.clip_start(clipIndex);
1792 trackPlaylist.split(clipIndex, cutPos - clipStart - 1);
1793 mlt_service_unlock(service.get_service());
1795 // duplicate effects
1796 Mlt::Producer *original = trackPlaylist.get_clip_at(clipStart);
1797 Mlt::Producer *clip = trackPlaylist.get_clip_at(cutPos);
1799 if (original == NULL || clip == NULL) {
1800 kDebug() << "// ERROR GRABBING CLIP AFTER SPLIT";
1802 Mlt::Service clipService(original->get_service());
1803 Mlt::Service dupService(clip->get_service());
1807 Mlt::Filter *filter = clipService.filter(ct);
1809 // Only duplicate Kdenlive filters, and skip the fade in effects
1810 if (filter->is_valid() && strcmp(filter->get("kdenlive_id"), "") && strcmp(filter->get("kdenlive_id"), "fadein") && strcmp(filter->get("kdenlive_id"), "fade_from_black")) {
1811 // looks like there is no easy way to duplicate a filter,
1812 // so we will create a new one and duplicate its properties
1813 Mlt::Filter *dup = new Mlt::Filter(*m_mltProfile, filter->get("mlt_service"));
1814 if (dup && dup->is_valid()) {
1815 Mlt::Properties entries(filter->get_properties());
1816 for (int i = 0; i < entries.count(); i++) {
1817 dup->set(entries.get_name(i), entries.get(i));
1819 dupService.attach(*dup);
1823 filter = clipService.filter(ct);
1826 /* // Display playlist info
1827 kDebug()<<"//////////// AFTER";
1828 for (int i = 0; i < trackPlaylist.count(); i++) {
1829 int blankStart = trackPlaylist.clip_start(i);
1830 int blankDuration = trackPlaylist.clip_length(i) - 1;
1832 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1833 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1838 bool Render::mltUpdateClip(ItemInfo info, QDomElement element, Mlt::Producer *prod)
1842 kDebug() << "Cannot update clip with null producer //////";
1845 Mlt::Service service(m_mltProducer->parent().get_service());
1846 if (service.type() != tractor_type) {
1847 kWarning() << "// TRACTOR PROBLEM";
1850 Mlt::Tractor tractor(service);
1851 Mlt::Producer trackProducer(tractor.track(tractor.count() - 1 - info.track));
1852 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1853 int startPos = info.startPos.frames(m_fps);
1854 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
1855 if (trackPlaylist.is_blank(clipIndex)) {
1856 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << startPos;
1859 mlt_service_lock(service.get_service());
1860 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
1862 QList <Mlt::Filter *> filtersList;
1863 Mlt::Service sourceService(clip->get_service());
1865 Mlt::Filter *filter = sourceService.filter(ct);
1867 if (filter->get_int("kdenlive_ix") != 0) {
1868 filtersList.append(filter);
1871 filter = sourceService.filter(ct);
1874 trackPlaylist.replace_with_blank(clipIndex);
1876 //if (!mltRemoveClip(info.track, info.startPos)) return false;
1877 prod = checkSlowMotionProducer(prod, element);
1878 if (prod == NULL || !prod->is_valid()) {
1879 mlt_service_unlock(service.get_service());
1883 Mlt::Producer *clip2 = prod->cut(info.cropStart.frames(m_fps), (info.cropDuration + info.cropStart).frames(m_fps) - 1);
1884 trackPlaylist.insert_at(info.startPos.frames(m_fps), clip2, 1);
1888 //if (mltInsertClip(info, element, prod) == -1) return false;
1889 if (!filtersList.isEmpty()) {
1890 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1891 Mlt::Producer *destclip = trackPlaylist.get_clip(clipIndex);
1892 Mlt::Service destService(destclip->get_service());
1894 for (int i = 0; i < filtersList.count(); i++)
1895 destService.attach(*(filtersList.at(i)));
1897 mlt_service_unlock(service.get_service());
1902 bool Render::mltRemoveClip(int track, GenTime position)
1904 Mlt::Service service(m_mltProducer->parent().get_service());
1905 if (service.type() != tractor_type) {
1906 kWarning() << "// TRACTOR PROBLEM";
1910 Mlt::Tractor tractor(service);
1911 Mlt::Producer trackProducer(tractor.track(track));
1912 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1913 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
1915 if (trackPlaylist.is_blank(clipIndex)) {
1916 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << position.frames(m_fps);
1917 //mlt_service_unlock(service.get_service());
1920 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
1921 if (clip) delete clip;
1922 trackPlaylist.consolidate_blanks(0);
1924 /* // Display playlist info
1925 kDebug()<<"//// AFTER";
1926 for (int i = 0; i < trackPlaylist.count(); i++) {
1927 int blankStart = trackPlaylist.clip_start(i);
1928 int blankDuration = trackPlaylist.clip_length(i) - 1;
1930 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1931 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1934 if (track != 0 && trackPlaylist.count() <= clipIndex) mltCheckLength(&tractor);
1938 int Render::mltGetSpaceLength(const GenTime &pos, int track, bool fromBlankStart)
1940 if (!m_mltProducer) {
1941 kDebug() << "PLAYLIST NOT INITIALISED //////";
1944 Mlt::Producer parentProd(m_mltProducer->parent());
1945 if (parentProd.get_producer() == NULL) {
1946 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1950 Mlt::Service service(parentProd.get_service());
1951 Mlt::Tractor tractor(service);
1952 int insertPos = pos.frames(m_fps);
1954 Mlt::Producer trackProducer(tractor.track(track));
1955 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1956 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1957 if (clipIndex == trackPlaylist.count()) {
1958 // We are after the end of the playlist
1961 if (!trackPlaylist.is_blank(clipIndex)) return 0;
1962 if (fromBlankStart) return trackPlaylist.clip_length(clipIndex);
1963 return trackPlaylist.clip_length(clipIndex) + trackPlaylist.clip_start(clipIndex) - insertPos;
1966 int Render::mltTrackDuration(int track)
1968 if (!m_mltProducer) {
1969 kDebug() << "PLAYLIST NOT INITIALISED //////";
1972 Mlt::Producer parentProd(m_mltProducer->parent());
1973 if (parentProd.get_producer() == NULL) {
1974 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1978 Mlt::Service service(parentProd.get_service());
1979 Mlt::Tractor tractor(service);
1981 Mlt::Producer trackProducer(tractor.track(track));
1982 return trackProducer.get_playtime() - 1;
1985 void Render::mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime &duration, const GenTime &timeOffset)
1987 if (!m_mltProducer) {
1988 kDebug() << "PLAYLIST NOT INITIALISED //////";
1991 Mlt::Producer parentProd(m_mltProducer->parent());
1992 if (parentProd.get_producer() == NULL) {
1993 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1996 //kDebug()<<"// CLP STRT LST: "<<trackClipStartList;
1997 //kDebug()<<"// TRA STRT LST: "<<trackTransitionStartList;
1999 Mlt::Service service(parentProd.get_service());
2000 Mlt::Tractor tractor(service);
2001 mlt_service_lock(service.get_service());
2002 int diff = duration.frames(m_fps);
2003 int offset = timeOffset.frames(m_fps);
2007 // insert space in one track only
2008 Mlt::Producer trackProducer(tractor.track(track));
2009 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2010 insertPos = trackClipStartList.value(track);
2011 if (insertPos != -1) {
2012 insertPos += offset;
2013 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
2015 trackPlaylist.insert_blank(clipIndex, diff - 1);
2017 if (!trackPlaylist.is_blank(clipIndex)) clipIndex --;
2018 if (!trackPlaylist.is_blank(clipIndex)) {
2019 kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
2021 int position = trackPlaylist.clip_start(clipIndex);
2022 int blankDuration = trackPlaylist.clip_length(clipIndex);
2023 if (blankDuration + diff == 0) {
2024 trackPlaylist.remove(clipIndex);
2025 } else trackPlaylist.remove_region(position, -diff);
2027 trackPlaylist.consolidate_blanks(0);
2029 // now move transitions
2030 mlt_service serv = m_mltProducer->parent().get_service();
2031 mlt_service nextservice = mlt_service_get_producer(serv);
2032 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2033 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2034 QString resource = mlt_properties_get(properties, "mlt_service");
2036 while (mlt_type == "transition") {
2037 mlt_transition tr = (mlt_transition) nextservice;
2038 int currentTrack = mlt_transition_get_b_track(tr);
2039 int currentIn = (int) mlt_transition_get_in(tr);
2040 int currentOut = (int) mlt_transition_get_out(tr);
2041 insertPos = trackTransitionStartList.value(track);
2042 if (insertPos != -1) {
2043 insertPos += offset;
2044 if (track == currentTrack && currentOut > insertPos && resource != "mix") {
2045 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
2048 nextservice = mlt_service_producer(nextservice);
2049 if (nextservice == NULL) break;
2050 properties = MLT_SERVICE_PROPERTIES(nextservice);
2051 mlt_type = mlt_properties_get(properties, "mlt_type");
2052 resource = mlt_properties_get(properties, "mlt_service");
2055 for (int trackNb = tractor.count() - 1; trackNb >= 1; --trackNb) {
2056 Mlt::Producer trackProducer(tractor.track(trackNb));
2057 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2059 //int clipNb = trackPlaylist.count();
2060 insertPos = trackClipStartList.value(trackNb);
2061 if (insertPos != -1) {
2062 insertPos += offset;
2064 /* kDebug()<<"-------------\nTRACK "<<trackNb<<" HAS "<<clipNb<<" CLPIS";
2065 kDebug() << "INSERT SPACE AT: "<<insertPos<<", DIFF: "<<diff<<", TK: "<<trackNb;
2066 for (int i = 0; i < clipNb; i++) {
2067 kDebug()<<"CLIP "<<i<<", START: "<<trackPlaylist.clip_start(i)<<", END: "<<trackPlaylist.clip_start(i) + trackPlaylist.clip_length(i);
2068 if (trackPlaylist.is_blank(i)) kDebug()<<"++ BLANK ++ ";
2069 kDebug()<<"-------------";
2071 kDebug()<<"END-------------";*/
2074 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
2076 trackPlaylist.insert_blank(clipIndex, diff - 1);
2078 if (!trackPlaylist.is_blank(clipIndex)) {
2081 if (!trackPlaylist.is_blank(clipIndex)) {
2082 kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
2084 int position = trackPlaylist.clip_start(clipIndex);
2085 int blankDuration = trackPlaylist.clip_length(clipIndex);
2086 if (diff + blankDuration == 0) {
2087 trackPlaylist.remove(clipIndex);
2088 } else trackPlaylist.remove_region(position, - diff);
2090 trackPlaylist.consolidate_blanks(0);
2093 // now move transitions
2094 mlt_service serv = m_mltProducer->parent().get_service();
2095 mlt_service nextservice = mlt_service_get_producer(serv);
2096 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2097 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2098 QString resource = mlt_properties_get(properties, "mlt_service");
2100 while (mlt_type == "transition") {
2101 mlt_transition tr = (mlt_transition) nextservice;
2102 int currentIn = (int) mlt_transition_get_in(tr);
2103 int currentOut = (int) mlt_transition_get_out(tr);
2104 int currentTrack = mlt_transition_get_b_track(tr);
2105 insertPos = trackTransitionStartList.value(currentTrack);
2106 if (insertPos != -1) {
2107 insertPos += offset;
2108 if (currentOut > insertPos && resource != "mix") {
2109 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
2112 nextservice = mlt_service_producer(nextservice);
2113 if (nextservice == NULL) break;
2114 properties = MLT_SERVICE_PROPERTIES(nextservice);
2115 mlt_type = mlt_properties_get(properties, "mlt_type");
2116 resource = mlt_properties_get(properties, "mlt_service");
2119 mlt_service_unlock(service.get_service());
2120 mltCheckLength(&tractor);
2121 m_mltConsumer->set("refresh", 1);
2125 void Render::mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest)
2127 if (source == dest) return;
2128 Mlt::Service sourceService(source->get_service());
2129 Mlt::Service destService(dest->get_service());
2131 // move all effects to the correct producer
2133 Mlt::Filter *filter = sourceService.filter(ct);
2135 if (filter->get_int("kdenlive_ix") != 0) {
2136 sourceService.detach(*filter);
2137 destService.attach(*filter);
2139 filter = sourceService.filter(ct);
2143 int Render::mltChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, double speed, double /*oldspeed*/, int strobe, Mlt::Producer *prod)
2146 Mlt::Service service(m_mltProducer->parent().get_service());
2147 if (service.type() != tractor_type) {
2148 kWarning() << "// TRACTOR PROBLEM";
2152 //kDebug() << "Changing clip speed, set in and out: " << info.cropStart.frames(m_fps) << " to " << (info.endPos - info.startPos).frames(m_fps) - 1;
2153 Mlt::Tractor tractor(service);
2154 Mlt::Producer trackProducer(tractor.track(info.track));
2155 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2156 int startPos = info.startPos.frames(m_fps);
2157 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
2158 int clipLength = trackPlaylist.clip_length(clipIndex);
2160 Mlt::Producer *original = trackPlaylist.get_clip(clipIndex);
2161 if (original == NULL) {
2164 if (!original->is_valid() || original->is_blank()) {
2169 Mlt::Producer clipparent = original->parent();
2170 if (!clipparent.is_valid() || clipparent.is_blank()) {
2176 QString serv = clipparent.get("mlt_service");
2177 QString id = clipparent.get("id");
2178 if (speed <= 0 && speed > -1) speed = 1.0;
2179 //kDebug() << "CLIP SERVICE: " << serv;
2180 if ((serv == "avformat" || serv == "avformat-novalidate") && (speed != 1.0 || strobe > 1)) {
2181 mlt_service_lock(service.get_service());
2182 QString url = QString::fromUtf8(clipparent.get("resource"));
2183 url.append('?' + m_locale.toString(speed));
2184 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
2185 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
2186 if (!slowprod || slowprod->get_producer() == NULL) {
2187 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
2188 if (strobe > 1) slowprod->set("strobe", strobe);
2189 QString producerid = "slowmotion:" + id + ':' + m_locale.toString(speed);
2190 if (strobe > 1) producerid.append(':' + QString::number(strobe));
2191 slowprod->set("id", producerid.toUtf8().constData());
2192 // copy producer props
2193 double ar = original->parent().get_double("force_aspect_ratio");
2194 if (ar != 0.0) slowprod->set("force_aspect_ratio", ar);
2195 double fps = original->parent().get_double("force_fps");
2196 if (fps != 0.0) slowprod->set("force_fps", fps);
2197 int threads = original->parent().get_int("threads");
2198 if (threads != 0) slowprod->set("threads", threads);
2199 if (original->parent().get("force_progressive"))
2200 slowprod->set("force_progressive", original->parent().get_int("force_progressive"));
2201 if (original->parent().get("force_tff"))
2202 slowprod->set("force_tff", original->parent().get_int("force_tff"));
2203 int ix = original->parent().get_int("video_index");
2204 if (ix != 0) slowprod->set("video_index", ix);
2205 int colorspace = original->parent().get_int("force_colorspace");
2206 if (colorspace != 0) slowprod->set("force_colorspace", colorspace);
2207 int full_luma = original->parent().get_int("set.force_full_luma");
2208 if (full_luma != 0) slowprod->set("set.force_full_luma", full_luma);
2209 m_slowmotionProducers.insert(url, slowprod);
2211 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2212 trackPlaylist.consolidate_blanks(0);
2214 // Check that the blank space is long enough for our new duration
2215 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2216 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2218 if (clipIndex + 1 < trackPlaylist.count() && (startPos + clipLength / speed > blankEnd)) {
2219 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2220 cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)(info.cropStart.frames(m_fps) / speed + maxLength.frames(m_fps) - 1));
2221 } else cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)((info.cropStart.frames(m_fps) + clipLength) / speed - 1));
2223 // move all effects to the correct producer
2224 mltPasteEffects(clip, cut);
2225 trackPlaylist.insert_at(startPos, cut, 1);
2228 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2229 newLength = trackPlaylist.clip_length(clipIndex);
2230 mlt_service_unlock(service.get_service());
2231 } else if (speed == 1.0 && strobe < 2) {
2232 mlt_service_lock(service.get_service());
2234 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2235 trackPlaylist.consolidate_blanks(0);
2237 // Check that the blank space is long enough for our new duration
2238 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2239 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2242 int originalStart = (int)(speedIndependantInfo.cropStart.frames(m_fps));
2243 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + speedIndependantInfo.cropDuration).frames(m_fps) > blankEnd) {
2244 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2245 cut = prod->cut(originalStart, (int)(originalStart + maxLength.frames(m_fps) - 1));
2246 } else cut = prod->cut(originalStart, (int)(originalStart + speedIndependantInfo.cropDuration.frames(m_fps)) - 1);
2248 // move all effects to the correct producer
2249 mltPasteEffects(clip, cut);
2251 trackPlaylist.insert_at(startPos, cut, 1);
2254 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2255 newLength = trackPlaylist.clip_length(clipIndex);
2256 mlt_service_unlock(service.get_service());
2258 } else if (serv == "framebuffer") {
2259 mlt_service_lock(service.get_service());
2260 QString url = QString::fromUtf8(clipparent.get("resource"));
2261 url = url.section('?', 0, 0);
2262 url.append('?' + m_locale.toString(speed));
2263 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
2264 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
2265 if (!slowprod || slowprod->get_producer() == NULL) {
2266 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
2267 slowprod->set("strobe", strobe);
2268 QString producerid = "slowmotion:" + id.section(':', 1, 1) + ':' + m_locale.toString(speed);
2269 if (strobe > 1) producerid.append(':' + QString::number(strobe));
2270 slowprod->set("id", producerid.toUtf8().constData());
2271 // copy producer props
2272 double ar = original->parent().get_double("force_aspect_ratio");
2273 if (ar != 0.0) slowprod->set("force_aspect_ratio", ar);
2274 double fps = original->parent().get_double("force_fps");
2275 if (fps != 0.0) slowprod->set("force_fps", fps);
2276 if (original->parent().get("force_progressive"))
2277 slowprod->set("force_progressive", original->parent().get_int("force_progressive"));
2278 if (original->parent().get("force_tff"))
2279 slowprod->set("force_tff", original->parent().get_int("force_tff"));
2280 int threads = original->parent().get_int("threads");
2281 if (threads != 0) slowprod->set("threads", threads);
2282 int ix = original->parent().get_int("video_index");
2283 if (ix != 0) slowprod->set("video_index", ix);
2284 int colorspace = original->parent().get_int("force_colorspace");
2285 if (colorspace != 0) slowprod->set("force_colorspace", colorspace);
2286 int full_luma = original->parent().get_int("set.force_full_luma");
2287 if (full_luma != 0) slowprod->set("set.force_full_luma", full_luma);
2288 m_slowmotionProducers.insert(url, slowprod);
2290 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2291 trackPlaylist.consolidate_blanks(0);
2293 GenTime duration = speedIndependantInfo.cropDuration / speed;
2294 int originalStart = (int)(speedIndependantInfo.cropStart.frames(m_fps) / speed);
2296 // Check that the blank space is long enough for our new duration
2297 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2298 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2301 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + duration).frames(m_fps) > blankEnd) {
2302 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2303 cut = slowprod->cut(originalStart, (int)(originalStart + maxLength.frames(m_fps) - 1));
2304 } else cut = slowprod->cut(originalStart, (int)(originalStart + duration.frames(m_fps)) - 1);
2306 // move all effects to the correct producer
2307 mltPasteEffects(clip, cut);
2309 trackPlaylist.insert_at(startPos, cut, 1);
2312 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2313 newLength = trackPlaylist.clip_length(clipIndex);
2315 mlt_service_unlock(service.get_service());
2318 if (clipIndex + 1 == trackPlaylist.count()) mltCheckLength(&tractor);
2322 bool Render::mltRemoveTrackEffect(int track, int index, bool updateIndex)
2324 Mlt::Service service(m_mltProducer->parent().get_service());
2325 bool success = false;
2326 Mlt::Tractor tractor(service);
2327 Mlt::Producer trackProducer(tractor.track(track));
2328 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2329 Mlt::Service clipService(trackPlaylist.get_service());
2331 mlt_service_lock(service.get_service());
2333 Mlt::Filter *filter = clipService.filter(ct);
2335 if ((index == -1 && strcmp(filter->get("kdenlive_id"), "")) || filter->get_int("kdenlive_ix") == index) {
2336 if (clipService.detach(*filter) == 0) success = true;
2337 } else if (updateIndex) {
2338 // Adjust the other effects index
2339 if (filter->get_int("kdenlive_ix") > index) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2342 filter = clipService.filter(ct);
2344 mlt_service_unlock(service.get_service());
2349 bool Render::mltRemoveEffect(int track, GenTime position, int index, bool updateIndex, bool doRefresh)
2351 if (position < GenTime()) {
2352 // Remove track effect
2353 return mltRemoveTrackEffect(track, index, updateIndex);
2355 Mlt::Service service(m_mltProducer->parent().get_service());
2356 bool success = false;
2357 Mlt::Tractor tractor(service);
2358 Mlt::Producer trackProducer(tractor.track(track));
2359 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2361 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2362 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2364 kDebug() << " / / / CANNOT FIND CLIP TO REMOVE EFFECT";
2368 Mlt::Service clipService(clip->get_service());
2369 int duration = clip->get_playtime();
2371 // Check if clip is visible in monitor
2372 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2373 if (diff < 0 || diff > duration) doRefresh = false;
2377 mlt_service_lock(service.get_service());
2379 Mlt::Filter *filter = clipService.filter(ct);
2381 if ((index == -1 && strcmp(filter->get("kdenlive_id"), "")) || filter->get_int("kdenlive_ix") == index) {// && filter->get("kdenlive_id") == id) {
2382 if (clipService.detach(*filter) == 0) success = true;
2383 //kDebug()<<"Deleted filter id:"<<filter->get("kdenlive_id")<<", ix:"<<filter->get("kdenlive_ix")<<", SERVICE:"<<filter->get("mlt_service");
2384 } else if (updateIndex) {
2385 // Adjust the other effects index
2386 if (filter->get_int("kdenlive_ix") > index) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2389 filter = clipService.filter(ct);
2391 mlt_service_unlock(service.get_service());
2392 if (doRefresh) refresh();
2396 bool Render::mltAddTrackEffect(int track, EffectsParameterList params)
2398 Mlt::Service service(m_mltProducer->parent().get_service());
2399 Mlt::Tractor tractor(service);
2400 Mlt::Producer trackProducer(tractor.track(track));
2401 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2402 Mlt::Service trackService(trackProducer.get_service()); //trackPlaylist
2403 return mltAddEffect(trackService, params, trackProducer.get_playtime() - 1, true);
2407 bool Render::mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh)
2410 Mlt::Service service(m_mltProducer->parent().get_service());
2412 Mlt::Tractor tractor(service);
2413 Mlt::Producer trackProducer(tractor.track(track));
2414 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2416 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2417 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2422 Mlt::Service clipService(clip->get_service());
2423 int duration = clip->get_playtime();
2425 // Check if clip is visible in monitor
2426 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2427 if (diff < 0 || diff > duration) doRefresh = false;
2430 return mltAddEffect(clipService, params, duration, doRefresh);
2433 bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int duration, bool doRefresh)
2435 bool updateIndex = false;
2436 const int filter_ix = params.paramValue("kdenlive_ix").toInt();
2437 const QString region = params.paramValue("region");
2439 mlt_service_lock(service.get_service());
2441 Mlt::Filter *filter = service.filter(ct);
2443 if (filter->get_int("kdenlive_ix") == filter_ix) {
2444 // A filter at that position already existed, so we will increase all indexes later
2449 filter = service.filter(ct);
2452 if (params.paramValue("id") == "speed") {
2453 // special case, speed effect is not really inserted, we just update the other effects index (kdenlive_ix)
2455 filter = service.filter(ct);
2457 if (filter->get_int("kdenlive_ix") >= filter_ix) {
2458 if (updateIndex) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") + 1);
2461 filter = service.filter(ct);
2463 mlt_service_unlock(service.get_service());
2464 if (doRefresh) refresh();
2469 // temporarily remove all effects after insert point
2470 QList <Mlt::Filter *> filtersList;
2472 filter = service.filter(ct);
2474 if (filter->get_int("kdenlive_ix") >= filter_ix) {
2475 filtersList.append(filter);
2476 service.detach(*filter);
2478 filter = service.filter(ct);
2482 QString tag = params.paramValue("tag");
2483 //kDebug() << " / / INSERTING EFFECT: " << tag << ", REGI: " << region;
2484 char *filterTag = qstrdup(tag.toUtf8().constData());
2485 char *filterId = qstrdup(params.paramValue("id").toUtf8().constData());
2486 QHash<QString, QString>::Iterator it;
2487 QString kfr = params.paramValue("keyframes");
2489 if (!kfr.isEmpty()) {
2490 QStringList keyFrames = kfr.split(';', QString::SkipEmptyParts);
2491 //kDebug() << "// ADDING KEYFRAME EFFECT: " << params.paramValue("keyframes");
2492 char *starttag = qstrdup(params.paramValue("starttag", "start").toUtf8().constData());
2493 char *endtag = qstrdup(params.paramValue("endtag", "end").toUtf8().constData());
2494 //kDebug() << "// ADDING KEYFRAME TAGS: " << starttag << ", " << endtag;
2495 //double max = params.paramValue("max").toDouble();
2496 double min = params.paramValue("min").toDouble();
2497 double factor = params.paramValue("factor", "1").toDouble();
2498 double paramOffset = params.paramValue("offset", "0").toDouble();
2499 params.removeParam("starttag");
2500 params.removeParam("endtag");
2501 params.removeParam("keyframes");
2502 params.removeParam("min");
2503 params.removeParam("max");
2504 params.removeParam("factor");
2505 params.removeParam("offset");
2507 // Special case, only one keyframe, means we want a constant value
2508 if (keyFrames.count() == 1) {
2509 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2510 if (filter && filter->is_valid()) {
2511 filter->set("kdenlive_id", filterId);
2512 int x1 = keyFrames.at(0).section(':', 0, 0).toInt();
2513 double y1 = keyFrames.at(0).section(':', 1, 1).toDouble();
2514 for (int j = 0; j < params.count(); j++) {
2515 filter->set(params.at(j).name().toUtf8().constData(), params.at(j).value().toUtf8().constData());
2517 filter->set("in", x1);
2518 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2519 filter->set(starttag, m_locale.toString(((min + y1) - paramOffset) / factor).toUtf8().data());
2520 service.attach(*filter);
2522 } else for (int i = 0; i < keyFrames.size() - 1; ++i) {
2523 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2524 if (filter && filter->is_valid()) {
2525 filter->set("kdenlive_id", filterId);
2526 int x1 = keyFrames.at(i).section(':', 0, 0).toInt() + offset;
2527 double y1 = keyFrames.at(i).section(':', 1, 1).toDouble();
2528 int x2 = keyFrames.at(i + 1).section(':', 0, 0).toInt();
2529 double y2 = keyFrames.at(i + 1).section(':', 1, 1).toDouble();
2530 if (x2 == -1) x2 = duration;
2532 for (int j = 0; j < params.count(); j++) {
2533 filter->set(params.at(j).name().toUtf8().constData(), params.at(j).value().toUtf8().constData());
2536 filter->set("in", x1);
2537 filter->set("out", x2);
2538 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2539 filter->set(starttag, m_locale.toString(((min + y1) - paramOffset) / factor).toUtf8().data());
2540 filter->set(endtag, m_locale.toString(((min + y2) - paramOffset) / factor).toUtf8().data());
2541 service.attach(*filter);
2548 Mlt::Filter *filter;
2550 if (!region.isEmpty()) {
2551 filter = new Mlt::Filter(*m_mltProfile, "region");
2552 } else filter = new Mlt::Filter(*m_mltProfile, filterTag);
2553 if (filter && filter->is_valid()) {
2554 filter->set("kdenlive_id", filterId);
2555 if (!region.isEmpty()) {
2556 filter->set("resource", region.toUtf8().constData());
2557 filter->set("kdenlive_ix", params.paramValue("kdenlive_ix").toUtf8().constData());
2558 filter->set("filter0", filterTag);
2559 prefix = "filter0.";
2560 params.removeParam("id");
2561 params.removeParam("region");
2562 params.removeParam("kdenlive_ix");
2565 kDebug() << "filter is NULL";
2566 mlt_service_unlock(service.get_service());
2569 params.removeParam("kdenlive_id");
2570 if (params.hasParam("_sync_in_out")) {
2571 // This effect must sync in / out with parent clip
2572 params.removeParam("_sync_in_out");
2573 filter->set_in_and_out(service.get_int("in"), service.get_int("out"));
2576 for (int j = 0; j < params.count(); j++) {
2577 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2581 QString effectArgs = params.paramValue("id").section('_', 1);
2583 params.removeParam("id");
2584 params.removeParam("kdenlive_ix");
2585 params.removeParam("tag");
2586 params.removeParam("disable");
2587 params.removeParam("region");
2589 for (int j = 0; j < params.count(); j++) {
2590 effectArgs.append(' ' + params.at(j).value());
2592 //kDebug() << "SOX EFFECTS: " << effectArgs.simplified();
2593 filter->set("effect", effectArgs.simplified().toUtf8().constData());
2596 // attach filter to the clip
2597 service.attach(*filter);
2602 // re-add following filters
2603 for (int i = 0; i < filtersList.count(); i++) {
2604 Mlt::Filter *filter = filtersList.at(i);
2606 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") + 1);
2607 service.attach(*filter);
2609 mlt_service_unlock(service.get_service());
2610 if (doRefresh) refresh();
2614 bool Render::mltEditTrackEffect(int track, EffectsParameterList params)
2616 Mlt::Service service(m_mltProducer->parent().get_service());
2617 Mlt::Tractor tractor(service);
2618 Mlt::Producer trackProducer(tractor.track(track));
2619 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2620 Mlt::Service clipService(trackPlaylist.get_service());
2622 QString index = params.paramValue("kdenlive_ix");
2623 QString tag = params.paramValue("tag");
2625 Mlt::Filter *filter = clipService.filter(ct);
2627 if (filter->get_int("kdenlive_ix") == index.toInt()) {
2631 filter = clipService.filter(ct);
2635 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT! " << index << ", " << tag;
2636 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2638 bool success = false;//mltAddTrackEffect(track, params);
2642 QString ser = filter->get("mlt_service");
2643 if (ser == "region") prefix = "filter0.";
2644 mlt_service_lock(service.get_service());
2645 for (int j = 0; j < params.count(); j++) {
2646 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2648 mlt_service_unlock(service.get_service());
2655 bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList params)
2657 int index = params.paramValue("kdenlive_ix").toInt();
2658 QString tag = params.paramValue("tag");
2660 if (!params.paramValue("keyframes").isEmpty() || /*it.key().startsWith("#") || */tag.startsWith("ladspa") || tag == "sox" || tag == "autotrack_rectangle" || params.hasParam("region")) {
2661 // This is a keyframe effect, to edit it, we remove it and re-add it.
2662 bool success = mltRemoveEffect(track, position, index, false);
2663 // if (!success) kDebug() << "// ERROR Removing effect : " << index;
2664 if (position < GenTime())
2665 success = mltAddTrackEffect(track, params);
2667 success = mltAddEffect(track, position, params);
2668 // if (!success) kDebug() << "// ERROR Adding effect : " << index;
2671 if (position < GenTime()) {
2672 return mltEditTrackEffect(track, params);
2675 Mlt::Service service(m_mltProducer->parent().get_service());
2676 Mlt::Tractor tractor(service);
2677 Mlt::Producer trackProducer(tractor.track(track));
2678 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2680 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2681 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2683 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2687 int duration = clip->get_playtime();
2688 bool doRefresh = true;
2689 // Check if clip is visible in monitor
2690 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2691 if (diff < 0 || diff > duration)
2695 Mlt::Filter *filter = clip->filter(ct);
2697 if (filter->get_int("kdenlive_ix") == index) {
2701 filter = clip->filter(ct);
2705 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT! " << index << ", " << tag;
2706 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2708 bool success = mltAddEffect(track, position, params);
2712 QString ser = filter->get("mlt_service");
2713 if (ser == "region") prefix = "filter0.";
2714 if (params.hasParam("_sync_in_out")) {
2715 // This effect must sync in / out with parent clip
2716 params.removeParam("_sync_in_out");
2717 filter->set_in_and_out(clip->get_in(), clip->get_out());
2719 mlt_service_lock(service.get_service());
2720 for (int j = 0; j < params.count(); j++) {
2721 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2725 mlt_service_unlock(service.get_service());
2727 if (doRefresh) refresh();
2731 void Render::mltUpdateEffectPosition(int track, GenTime position, int oldPos, int newPos)
2733 Mlt::Service service(m_mltProducer->parent().get_service());
2734 Mlt::Tractor tractor(service);
2735 Mlt::Producer trackProducer(tractor.track(track));
2736 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2738 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2739 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2741 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2745 Mlt::Service clipService(clip->get_service());
2746 int duration = clip->get_playtime();
2747 bool doRefresh = true;
2748 // Check if clip is visible in monitor
2749 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2750 if (diff < 0 || diff > duration) doRefresh = false;
2754 Mlt::Filter *filter = clipService.filter(ct);
2756 int pos = filter->get_int("kdenlive_ix");
2757 if (pos == oldPos) {
2758 filter->set("kdenlive_ix", newPos);
2760 filter = clipService.filter(ct);
2762 if (doRefresh) refresh();
2765 void Render::mltMoveEffect(int track, GenTime position, int oldPos, int newPos)
2767 if (position < GenTime()) {
2768 mltMoveTrackEffect(track, oldPos, newPos);
2771 Mlt::Service service(m_mltProducer->parent().get_service());
2772 Mlt::Tractor tractor(service);
2773 Mlt::Producer trackProducer(tractor.track(track));
2774 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2776 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2777 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2779 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2783 Mlt::Service clipService(clip->get_service());
2784 int duration = clip->get_playtime();
2785 bool doRefresh = true;
2786 // Check if clip is visible in monitor
2787 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2788 if (diff < 0 || diff > duration) doRefresh = false;
2792 QList <Mlt::Filter *> filtersList;
2793 Mlt::Filter *filter = clipService.filter(ct);
2795 if (newPos > oldPos) {
2797 if (!found && filter->get_int("kdenlive_ix") == oldPos) {
2798 filter->set("kdenlive_ix", newPos);
2799 filtersList.append(filter);
2800 clipService.detach(*filter);
2801 filter = clipService.filter(ct);
2802 while (filter && filter->get_int("kdenlive_ix") <= newPos) {
2803 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2805 filter = clipService.filter(ct);
2809 if (filter && filter->get_int("kdenlive_ix") > newPos) {
2810 filtersList.append(filter);
2811 clipService.detach(*filter);
2813 filter = clipService.filter(ct);
2817 if (filter->get_int("kdenlive_ix") == oldPos) {
2818 filter->set("kdenlive_ix", newPos);
2819 filtersList.append(filter);
2820 clipService.detach(*filter);
2822 filter = clipService.filter(ct);
2826 filter = clipService.filter(ct);
2828 int pos = filter->get_int("kdenlive_ix");
2829 if (pos >= newPos) {
2830 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
2831 filtersList.append(filter);
2832 clipService.detach(*filter);
2834 filter = clipService.filter(ct);
2838 for (int i = 0; i < filtersList.count(); i++) {
2839 clipService.attach(*(filtersList.at(i)));
2842 if (doRefresh) refresh();
2845 void Render::mltMoveTrackEffect(int track, int oldPos, int newPos)
2847 Mlt::Service service(m_mltProducer->parent().get_service());
2848 Mlt::Tractor tractor(service);
2849 Mlt::Producer trackProducer(tractor.track(track));
2850 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2851 Mlt::Service clipService(trackPlaylist.get_service());
2853 QList <Mlt::Filter *> filtersList;
2854 Mlt::Filter *filter = clipService.filter(ct);
2856 if (newPos > oldPos) {
2858 if (!found && filter->get_int("kdenlive_ix") == oldPos) {
2859 filter->set("kdenlive_ix", newPos);
2860 filtersList.append(filter);
2861 clipService.detach(*filter);
2862 filter = clipService.filter(ct);
2863 while (filter && filter->get_int("kdenlive_ix") <= newPos) {
2864 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2866 filter = clipService.filter(ct);
2870 if (filter && filter->get_int("kdenlive_ix") > newPos) {
2871 filtersList.append(filter);
2872 clipService.detach(*filter);
2874 filter = clipService.filter(ct);
2878 if (filter->get_int("kdenlive_ix") == oldPos) {
2879 filter->set("kdenlive_ix", newPos);
2880 filtersList.append(filter);
2881 clipService.detach(*filter);
2883 filter = clipService.filter(ct);
2887 filter = clipService.filter(ct);
2889 int pos = filter->get_int("kdenlive_ix");
2890 if (pos >= newPos) {
2891 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
2892 filtersList.append(filter);
2893 clipService.detach(*filter);
2895 filter = clipService.filter(ct);
2899 for (int i = 0; i < filtersList.count(); i++) {
2900 clipService.attach(*(filtersList.at(i)));
2905 bool Render::mltResizeClipEnd(ItemInfo info, GenTime clipDuration)
2907 Mlt::Service service(m_mltProducer->parent().get_service());
2908 Mlt::Tractor tractor(service);
2909 Mlt::Producer trackProducer(tractor.track(info.track));
2910 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2912 /* // Display playlist info
2913 kDebug()<<"//////////// BEFORE RESIZE";
2914 for (int i = 0; i < trackPlaylist.count(); i++) {
2915 int blankStart = trackPlaylist.clip_start(i);
2916 int blankDuration = trackPlaylist.clip_length(i) - 1;
2918 if (trackPlaylist.is_blank(i)) blk = "(blank)";
2919 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
2922 if (trackPlaylist.is_blank_at((int) info.startPos.frames(m_fps))) {
2923 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
2926 mlt_service_lock(service.get_service());
2927 int clipIndex = trackPlaylist.get_clip_index_at((int) info.startPos.frames(m_fps));
2928 //kDebug() << "// SELECTED CLIP START: " << trackPlaylist.clip_start(clipIndex);
2929 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2931 int previousStart = clip->get_in();
2932 int newDuration = (int) clipDuration.frames(m_fps) - 1;
2933 int diff = newDuration - (trackPlaylist.clip_length(clipIndex) - 1);
2935 int currentOut = newDuration + previousStart;
2936 if (currentOut > clip->get_length()) {
2937 clip->parent().set("length", currentOut + 1);
2938 clip->parent().set("out", currentOut);
2939 clip->set("length", currentOut + 1);
2942 /*if (newDuration > clip->get_out()) {
2943 clip->parent().set_in_and_out(0, newDuration + 1);
2944 clip->set_in_and_out(0, newDuration + 1);
2947 trackPlaylist.resize_clip(clipIndex, previousStart, newDuration + previousStart);
2948 trackPlaylist.consolidate_blanks(0);
2949 // skip to next clip
2951 //kDebug() << "//////// RESIZE CLIP: " << clipIndex << "( pos: " << info.startPos.frames(25) << "), DIFF: " << diff << ", CURRENT DUR: " << previousDuration << ", NEW DUR: " << newDuration << ", IX: " << clipIndex << ", MAX: " << trackPlaylist.count();
2953 // clip was made longer, trim next blank if there is one.
2954 if (clipIndex < trackPlaylist.count()) {
2955 // If this is not the last clip in playlist
2956 if (trackPlaylist.is_blank(clipIndex)) {
2957 int blankStart = trackPlaylist.clip_start(clipIndex);
2958 int blankDuration = trackPlaylist.clip_length(clipIndex);
2959 if (diff > blankDuration) {
2960 kDebug() << "// ERROR blank clip is not large enough to get back required space!!!";
2962 if (diff - blankDuration == 0) {
2963 trackPlaylist.remove(clipIndex);
2964 } else trackPlaylist.remove_region(blankStart, diff);
2966 kDebug() << "/// RESIZE ERROR, NXT CLIP IS NOT BLK: " << clipIndex;
2969 } else if (clipIndex != trackPlaylist.count()) trackPlaylist.insert_blank(clipIndex, 0 - diff - 1);
2970 trackPlaylist.consolidate_blanks(0);
2971 mlt_service_unlock(service.get_service());
2973 if (info.track != 0 && clipIndex == trackPlaylist.count()) mltCheckLength(&tractor);
2974 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
2975 //mltResizeTransparency(previousStart, previousStart, previousStart + newDuration, track, QString(clip->parent().get("id")).toInt());
2976 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
2977 ItemInfo transpinfo;
2978 transpinfo.startPos = info.startPos;
2979 transpinfo.endPos = info.startPos + clipDuration;
2980 transpinfo.track = info.track;
2981 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
2983 m_mltConsumer->set("refresh", 1);
2987 void Render::mltChangeTrackState(int track, bool mute, bool blind)
2989 Mlt::Service service(m_mltProducer->parent().get_service());
2990 Mlt::Tractor tractor(service);
2991 Mlt::Producer trackProducer(tractor.track(track));
2993 // Make sure muting will not produce problems with our audio mixing transition,
2994 // because audio mixing is done between each track and the lowest one
2995 bool audioMixingBroken = false;
2996 if (mute && trackProducer.get_int("hide") < 2 ) {
2997 // We mute a track with sound
2998 if (track == getLowestNonMutedAudioTrack(tractor)) audioMixingBroken = true;
2999 kDebug()<<"Muting track: "<<track <<" / "<<getLowestNonMutedAudioTrack(tractor);
3001 else if (!mute && trackProducer.get_int("hide") > 1 ) {
3002 // We un-mute a previously muted track
3003 if (track < getLowestNonMutedAudioTrack(tractor)) audioMixingBroken = true;
3007 if (blind) trackProducer.set("hide", 3);
3008 else trackProducer.set("hide", 2);
3010 trackProducer.set("hide", 1);
3012 trackProducer.set("hide", 0);
3014 if (audioMixingBroken) fixAudioMixing(tractor);
3016 tractor.multitrack()->refresh();
3021 int Render::getLowestNonMutedAudioTrack(Mlt::Tractor tractor)
3023 for (int i = 1; i < tractor.count(); i++) {
3024 Mlt::Producer trackProducer(tractor.track(i));
3025 if (trackProducer.get_int("hide") < 2) return i;
3027 return tractor.count() - 1;
3030 void Render::fixAudioMixing(Mlt::Tractor tractor)
3032 // Make sure the audio mixing transitions are applied to the lowest audible (non muted) track
3033 int lowestTrack = getLowestNonMutedAudioTrack(tractor);
3035 mlt_service serv = m_mltProducer->parent().get_service();
3036 Mlt::Field *field = tractor.field();
3037 mlt_service_lock(serv);
3039 mlt_service nextservice = mlt_service_get_producer(serv);
3040 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3041 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3042 QString resource = mlt_properties_get(properties, "mlt_service");
3044 mlt_service nextservicetodisconnect;
3045 // Delete all audio mixing transitions
3046 while (mlt_type == "transition") {
3047 if (resource == "mix") {
3048 nextservicetodisconnect = nextservice;
3049 nextservice = mlt_service_producer(nextservice);
3050 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
3052 else nextservice = mlt_service_producer(nextservice);
3053 if (nextservice == NULL) break;
3054 properties = MLT_SERVICE_PROPERTIES(nextservice);
3055 mlt_type = mlt_properties_get(properties, "mlt_type");
3056 resource = mlt_properties_get(properties, "mlt_service");
3059 // Re-add correct audio transitions
3060 for (int i = lowestTrack + 1; i < tractor.count(); i++) {
3061 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "mix");
3062 transition->set("always_active", 1);
3063 transition->set("combine", 1);
3064 transition->set("internal_added", 237);
3065 field->plant_transition(*transition, lowestTrack, i);
3067 mlt_service_unlock(serv);
3070 bool Render::mltResizeClipCrop(ItemInfo info, GenTime diff)
3072 Mlt::Service service(m_mltProducer->parent().get_service());
3073 int frameOffset = (int) diff.frames(m_fps);
3074 Mlt::Tractor tractor(service);
3075 Mlt::Producer trackProducer(tractor.track(info.track));
3076 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3077 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
3078 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
3081 mlt_service_lock(service.get_service());
3082 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
3083 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
3085 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
3086 mlt_service_unlock(service.get_service());
3089 int previousStart = clip->get_in();
3090 int previousOut = clip->get_out();
3092 trackPlaylist.resize_clip(clipIndex, previousStart + frameOffset, previousOut + frameOffset);
3093 mlt_service_unlock(service.get_service());
3094 m_mltConsumer->set("refresh", 1);
3098 bool Render::mltResizeClipStart(ItemInfo info, GenTime diff)
3100 //kDebug() << "//////// RSIZING CLIP from: "<<info.startPos.frames(25)<<" to "<<diff.frames(25);
3101 Mlt::Service service(m_mltProducer->parent().get_service());
3102 int moveFrame = (int) diff.frames(m_fps);
3103 Mlt::Tractor tractor(service);
3104 Mlt::Producer trackProducer(tractor.track(info.track));
3105 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3106 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
3107 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
3110 mlt_service_lock(service.get_service());
3111 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
3112 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
3113 if (clip == NULL || clip->is_blank()) {
3114 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
3115 mlt_service_unlock(service.get_service());
3118 int previousStart = clip->get_in();
3119 int previousOut = clip->get_out();
3121 previousStart += moveFrame;
3123 if (previousStart < 0) {
3124 // this is possible for images and color clips
3125 previousOut -= previousStart;
3129 int length = previousOut + 1;
3130 if (length > clip->get_length()) {
3131 clip->parent().set("length", length + 1);
3132 clip->parent().set("out", length);
3133 clip->set("length", length + 1);
3137 // kDebug() << "RESIZE, new start: " << previousStart << ", " << previousOut;
3138 trackPlaylist.resize_clip(clipIndex, previousStart, previousOut);
3139 if (moveFrame > 0) {
3140 trackPlaylist.insert_blank(clipIndex, moveFrame - 1);
3142 //int midpos = info.startPos.frames(m_fps) + moveFrame - 1;
3143 int blankIndex = clipIndex - 1;
3144 int blankLength = trackPlaylist.clip_length(blankIndex);
3145 // kDebug() << " + resizing blank length " << blankLength << ", SIZE DIFF: " << moveFrame;
3146 if (! trackPlaylist.is_blank(blankIndex)) {
3147 kDebug() << "WARNING, CLIP TO RESIZE IS NOT BLANK";
3149 if (blankLength + moveFrame == 0)
3150 trackPlaylist.remove(blankIndex);
3152 trackPlaylist.resize_clip(blankIndex, 0, blankLength + moveFrame - 1);
3154 trackPlaylist.consolidate_blanks(0);
3155 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
3156 //mltResizeTransparency(previousStart, (int) moveEnd.frames(m_fps), (int) (moveEnd + out - in).frames(m_fps), track, QString(clip->parent().get("id")).toInt());
3157 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
3158 ItemInfo transpinfo;
3159 transpinfo.startPos = info.startPos + diff;
3160 transpinfo.endPos = info.startPos + diff + (info.endPos - info.startPos);
3161 transpinfo.track = info.track;
3162 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
3164 //m_mltConsumer->set("refresh", 1);
3165 mlt_service_unlock(service.get_service());
3166 m_mltConsumer->set("refresh", 1);
3170 bool Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd, Mlt::Producer *prod, bool overwrite, bool insert)
3172 return mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps), prod, overwrite, insert);
3176 bool Render::mltUpdateClipProducer(int track, int pos, Mlt::Producer *prod)
3178 if (prod == NULL || !prod->is_valid()) {
3179 kDebug() << "// Warning, CLIP on track " << track << ", at: " << pos << " is invalid, cannot update it!!!";
3182 //kDebug() << "// TRYING TO UPDATE CLIP at: " << pos << ", TK: " << track;
3183 Mlt::Service service(m_mltProducer->parent().get_service());
3184 if (service.type() != tractor_type) {
3185 kWarning() << "// TRACTOR PROBLEM";
3188 mlt_service_lock(service.get_service());
3189 Mlt::Tractor tractor(service);
3190 Mlt::Producer trackProducer(tractor.track(track));
3191 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3192 int clipIndex = trackPlaylist.get_clip_index_at(pos);
3193 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3194 if (clipProducer == NULL || clipProducer->is_blank()) {
3195 kDebug() << "// ERROR UPDATING CLIP PROD";
3196 delete clipProducer;
3197 mlt_service_unlock(service.get_service());
3200 Mlt::Producer *clip = prod->cut(clipProducer->get_in(), clipProducer->get_out());
3201 if (!clip || !clip->is_valid()) {
3202 if (clip) delete clip;
3203 delete clipProducer;
3204 mlt_service_unlock(service.get_service());
3207 // move all effects to the correct producer
3208 mltPasteEffects(clipProducer, clip);
3209 trackPlaylist.insert_at(pos, clip, 1);
3211 delete clipProducer;
3212 mlt_service_unlock(service.get_service());
3216 bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd, Mlt::Producer *prod, bool overwrite, bool /*insert*/)
3218 Mlt::Service service(m_mltProducer->parent().get_service());
3219 if (service.type() != tractor_type) {
3220 kWarning() << "// TRACTOR PROBLEM";
3224 Mlt::Tractor tractor(service);
3225 mlt_service_lock(service.get_service());
3226 Mlt::Producer trackProducer(tractor.track(startTrack));
3227 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3228 int clipIndex = trackPlaylist.get_clip_index_at(moveStart);
3229 //kDebug() << "////// LOOKING FOR CLIP TO MOVE, INDEX: " << clipIndex;
3230 bool checkLength = false;
3231 if (endTrack == startTrack) {
3232 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3233 if ((!overwrite && !trackPlaylist.is_blank_at(moveEnd)) || !clipProducer || !clipProducer->is_valid() || clipProducer->is_blank()) {
3234 // error, destination is not empty
3236 if (!trackPlaylist.is_blank_at(moveEnd) && clipProducer->is_valid()) trackPlaylist.insert_at(moveStart, clipProducer, 1);
3237 delete clipProducer;
3239 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
3240 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
3241 mlt_service_unlock(service.get_service());
3244 trackPlaylist.consolidate_blanks(0);
3246 trackPlaylist.remove_region(moveEnd, clipProducer->get_playtime());
3247 int clipIndex = trackPlaylist.get_clip_index_at(moveEnd);
3248 trackPlaylist.insert_blank(clipIndex, clipProducer->get_playtime() - 1);
3250 int newIndex = trackPlaylist.insert_at(moveEnd, clipProducer, 1);
3251 trackPlaylist.consolidate_blanks(1);
3252 delete clipProducer;
3253 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
3254 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
3256 if (newIndex + 1 == trackPlaylist.count()) checkLength = true;
3258 //mlt_service_unlock(service.get_service());
3260 Mlt::Producer destTrackProducer(tractor.track(endTrack));
3261 Mlt::Playlist destTrackPlaylist((mlt_playlist) destTrackProducer.get_service());
3262 if (!overwrite && !destTrackPlaylist.is_blank_at(moveEnd)) {
3263 // error, destination is not empty
3264 mlt_service_unlock(service.get_service());
3267 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3268 if (!clipProducer || clipProducer->is_blank()) {
3269 // error, destination is not empty
3270 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
3271 if (clipProducer) delete clipProducer;
3272 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
3273 mlt_service_unlock(service.get_service());
3276 trackPlaylist.consolidate_blanks(0);
3277 destTrackPlaylist.consolidate_blanks(1);
3278 Mlt::Producer *clip;
3279 // check if we are moving a slowmotion producer
3280 QString serv = clipProducer->parent().get("mlt_service");
3281 QString currentid = clipProducer->parent().get("id");
3282 if (serv == "framebuffer" || currentid.endsWith("_video")) {
3283 clip = clipProducer;
3286 // Special case: prod is null when using placeholder clips.
3287 // in that case, use the producer existing in playlist. Note that
3288 // it will bypass the one producer per track logic and might cause
3289 // Sound cracks if clip is moved so that it overlaps another copy of itself
3290 clip = clipProducer->cut(clipProducer->get_in(), clipProducer->get_out());
3291 } else clip = prod->cut(clipProducer->get_in(), clipProducer->get_out());
3294 // move all effects to the correct producer
3295 mltPasteEffects(clipProducer, clip);
3298 destTrackPlaylist.remove_region(moveEnd, clip->get_playtime());
3299 int clipIndex = destTrackPlaylist.get_clip_index_at(moveEnd);
3300 destTrackPlaylist.insert_blank(clipIndex, clip->get_playtime() - 1);
3303 int newIndex = destTrackPlaylist.insert_at(moveEnd, clip, 1);
3305 if (clip == clipProducer) {
3310 delete clipProducer;
3312 destTrackPlaylist.consolidate_blanks(0);
3313 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
3314 kDebug() << "//////// moving clip transparency";
3315 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
3317 if (clipIndex > trackPlaylist.count()) checkLength = true;
3318 else if (newIndex + 1 == destTrackPlaylist.count()) checkLength = true;
3321 mlt_service_unlock(service.get_service());
3322 if (checkLength) mltCheckLength(&tractor);
3324 //m_mltConsumer->set("refresh", 1);
3329 QList <int> Render::checkTrackSequence(int track)
3332 Mlt::Service service(m_mltProducer->parent().get_service());
3333 if (service.type() != tractor_type) {
3334 kWarning() << "// TRACTOR PROBLEM";
3337 Mlt::Tractor tractor(service);
3338 mlt_service_lock(service.get_service());
3339 Mlt::Producer trackProducer(tractor.track(track));
3340 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3341 int clipNb = trackPlaylist.count();
3342 //kDebug() << "// PARSING SCENE TRACK: " << t << ", CLIPS: " << clipNb;
3343 for (int i = 0; i < clipNb; i++) {
3344 Mlt::Producer *c = trackPlaylist.get_clip(i);
3345 int pos = trackPlaylist.clip_start(i);
3346 if (!list.contains(pos)) list.append(pos);
3347 pos += c->get_playtime();
3348 if (!list.contains(pos)) list.append(pos);
3354 bool Render::mltMoveTransition(QString type, int startTrack, int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut)
3356 int new_in = (int)newIn.frames(m_fps);
3357 int new_out = (int)newOut.frames(m_fps) - 1;
3358 if (new_in >= new_out) return false;
3359 int old_in = (int)oldIn.frames(m_fps);
3360 int old_out = (int)oldOut.frames(m_fps) - 1;
3362 Mlt::Service service(m_mltProducer->parent().get_service());
3363 Mlt::Tractor tractor(service);
3364 Mlt::Field *field = tractor.field();
3366 bool doRefresh = true;
3367 // Check if clip is visible in monitor
3368 int diff = old_out - m_mltProducer->position();
3369 if (diff < 0 || diff > old_out - old_in) doRefresh = false;
3371 diff = new_out - m_mltProducer->position();
3372 if (diff < 0 || diff > new_out - new_in) doRefresh = false;
3374 mlt_service_lock(service.get_service());
3376 mlt_service nextservice = mlt_service_get_producer(service.get_service());
3377 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3378 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3379 QString resource = mlt_properties_get(properties, "mlt_service");
3380 int old_pos = (int)(old_in + old_out) / 2;
3383 while (mlt_type == "transition") {
3384 Mlt::Transition transition((mlt_transition) nextservice);
3385 int currentTrack = transition.get_b_track();
3386 int currentIn = (int) transition.get_in();
3387 int currentOut = (int) transition.get_out();
3389 if (resource == type && startTrack == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
3391 if (newTrack - startTrack != 0) {
3392 Mlt::Properties trans_props(transition.get_properties());
3393 Mlt::Transition new_transition(*m_mltProfile, transition.get("mlt_service"));
3394 Mlt::Properties new_trans_props(new_transition.get_properties());
3395 new_trans_props.inherit(trans_props);
3396 new_transition.set_in_and_out(new_in, new_out);
3397 field->disconnect_service(transition);
3398 mltPlantTransition(field, new_transition, newTransitionTrack, newTrack);
3399 //field->plant_transition(new_transition, newTransitionTrack, newTrack);
3400 } else transition.set_in_and_out(new_in, new_out);
3403 nextservice = mlt_service_producer(nextservice);
3404 if (nextservice == NULL) break;
3405 properties = MLT_SERVICE_PROPERTIES(nextservice);
3406 mlt_type = mlt_properties_get(properties, "mlt_type");
3407 resource = mlt_properties_get(properties, "mlt_service");
3409 mlt_service_unlock(service.get_service());
3410 if (doRefresh) refresh();
3411 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3416 void Render::mltPlantTransition(Mlt::Field *field, Mlt::Transition &tr, int a_track, int b_track)
3418 mlt_service nextservice = mlt_service_get_producer(field->get_service());
3419 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3420 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3421 QString resource = mlt_properties_get(properties, "mlt_service");
3422 QList <Mlt::Transition *> trList;
3424 while (mlt_type == "transition") {
3425 Mlt::Transition transition((mlt_transition) nextservice);
3426 int aTrack = transition.get_a_track();
3427 int bTrack = transition.get_b_track();
3428 if (resource != "mix" && (aTrack < a_track || (aTrack == a_track && bTrack > b_track))) {
3429 Mlt::Properties trans_props(transition.get_properties());
3430 Mlt::Transition *cp = new Mlt::Transition(*m_mltProfile, transition.get("mlt_service"));
3431 Mlt::Properties new_trans_props(cp->get_properties());
3432 new_trans_props.inherit(trans_props);
3434 field->disconnect_service(transition);
3436 //else kDebug() << "// FOUND TRANS OK, "<<resource<< ", A_: " << aTrack << ", B_ "<<bTrack;
3438 nextservice = mlt_service_producer(nextservice);
3439 if (nextservice == NULL) break;
3440 properties = MLT_SERVICE_PROPERTIES(nextservice);
3441 mlt_type = mlt_properties_get(properties, "mlt_type");
3442 resource = mlt_properties_get(properties, "mlt_service");
3444 field->plant_transition(tr, a_track, b_track);
3446 // re-add upper transitions
3447 for (int i = trList.count() - 1; i >= 0; i--) {
3448 //kDebug()<< "REPLANT ON TK: "<<trList.at(i)->get_a_track()<<", "<<trList.at(i)->get_b_track();
3449 field->plant_transition(*trList.at(i), trList.at(i)->get_a_track(), trList.at(i)->get_b_track());
3453 void Render::mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool force)
3455 if (oldTag == tag && !force) mltUpdateTransitionParams(tag, a_track, b_track, in, out, xml);
3457 //kDebug()<<"// DELETING TRANS: "<<a_track<<"-"<<b_track;
3458 mltDeleteTransition(oldTag, a_track, b_track, in, out, xml, false);
3459 mltAddTransition(tag, a_track, b_track, in, out, xml, false);
3462 if (m_mltProducer->position() >= in.frames(m_fps) && m_mltProducer->position() <= out.frames(m_fps)) refresh();
3465 void Render::mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml)
3467 mlt_service serv = m_mltProducer->parent().get_service();
3468 mlt_service_lock(serv);
3470 mlt_service nextservice = mlt_service_get_producer(serv);
3471 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3472 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3473 QString resource = mlt_properties_get(properties, "mlt_service");
3474 int in_pos = (int) in.frames(m_fps);
3475 int out_pos = (int) out.frames(m_fps) - 1;
3477 while (mlt_type == "transition") {
3478 mlt_transition tr = (mlt_transition) nextservice;
3479 int currentTrack = mlt_transition_get_b_track(tr);
3480 int currentBTrack = mlt_transition_get_a_track(tr);
3481 int currentIn = (int) mlt_transition_get_in(tr);
3482 int currentOut = (int) mlt_transition_get_out(tr);
3484 // kDebug()<<"Looking for transition : " << currentIn <<'x'<<currentOut<< ", OLD oNE: "<<in_pos<<'x'<<out_pos;
3485 if (resource == type && b_track == currentTrack && currentIn == in_pos && currentOut == out_pos) {
3486 QMap<QString, QString> map = mltGetTransitionParamsFromXml(xml);
3487 QMap<QString, QString>::Iterator it;
3489 mlt_properties transproperties = MLT_TRANSITION_PROPERTIES(tr);
3491 QString currentId = mlt_properties_get(transproperties, "kdenlive_id");
3492 if (currentId != xml.attribute("id")) {
3493 // The transition ID is not the same, so reset all properties
3494 mlt_properties_set(transproperties, "kdenlive_id", xml.attribute("id").toUtf8().constData());
3495 // Cleanup previous properties
3496 QStringList permanentProps;
3497 permanentProps << "factory" << "kdenlive_id" << "mlt_service" << "mlt_type" << "in";
3498 permanentProps << "out" << "a_track" << "b_track";
3499 for (int i = 0; i < mlt_properties_count(transproperties); i++) {
3500 QString propName = mlt_properties_get_name(transproperties, i);
3501 if (!propName.startsWith('_') && ! permanentProps.contains(propName)) {
3502 mlt_properties_set(transproperties, propName.toUtf8().constData(), "");
3507 mlt_properties_set_int(transproperties, "force_track", xml.attribute("force_track").toInt());
3508 mlt_properties_set_int(transproperties, "automatic", xml.attribute("automatic", "0").toInt());
3510 if (currentBTrack != a_track) {
3511 mlt_properties_set_int(transproperties, "a_track", a_track);
3513 for (it = map.begin(); it != map.end(); ++it) {
3515 mlt_properties_set(transproperties, key.toUtf8().constData(), it.value().toUtf8().constData());
3516 //kDebug() << " ------ UPDATING TRANS PARAM: " << key.toUtf8().constData() << ": " << it.value().toUtf8().constData();
3517 //filter->set("kdenlive_id", id);
3521 nextservice = mlt_service_producer(nextservice);
3522 if (nextservice == NULL) break;
3523 properties = MLT_SERVICE_PROPERTIES(nextservice);
3524 mlt_type = mlt_properties_get(properties, "mlt_type");
3525 resource = mlt_properties_get(properties, "mlt_service");
3527 mlt_service_unlock(serv);
3529 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3532 void Render::mltDeleteTransition(QString tag, int /*a_track*/, int b_track, GenTime in, GenTime out, QDomElement /*xml*/, bool /*do_refresh*/)
3534 mlt_service serv = m_mltProducer->parent().get_service();
3535 mlt_service_lock(serv);
3537 Mlt::Service service(serv);
3538 Mlt::Tractor tractor(service);
3539 Mlt::Field *field = tractor.field();
3541 //if (do_refresh) m_mltConsumer->set("refresh", 0);
3543 mlt_service nextservice = mlt_service_get_producer(serv);
3544 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3545 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3546 QString resource = mlt_properties_get(properties, "mlt_service");
3548 const int old_pos = (int)((in + out).frames(m_fps) / 2);
3549 //kDebug() << " del trans pos: " << in.frames(25) << "-" << out.frames(25);
3551 while (mlt_type == "transition") {
3552 mlt_transition tr = (mlt_transition) nextservice;
3553 int currentTrack = mlt_transition_get_b_track(tr);
3554 int currentIn = (int) mlt_transition_get_in(tr);
3555 int currentOut = (int) mlt_transition_get_out(tr);
3556 //kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
3558 if (resource == tag && b_track == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
3559 mlt_field_disconnect_service(field->get_field(), nextservice);
3562 nextservice = mlt_service_producer(nextservice);
3563 if (nextservice == NULL) break;
3564 properties = MLT_SERVICE_PROPERTIES(nextservice);
3565 mlt_type = mlt_properties_get(properties, "mlt_type");
3566 resource = mlt_properties_get(properties, "mlt_service");
3568 mlt_service_unlock(serv);
3570 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3573 QMap<QString, QString> Render::mltGetTransitionParamsFromXml(QDomElement xml)
3575 QDomNodeList attribs = xml.elementsByTagName("parameter");
3576 QMap<QString, QString> map;
3577 for (int i = 0; i < attribs.count(); i++) {
3578 QDomElement e = attribs.item(i).toElement();
3579 QString name = e.attribute("name");
3580 //kDebug()<<"-- TRANSITION PARAM: "<<name<<" = "<< e.attribute("name")<<" / " << e.attribute("value");
3581 map[name] = e.attribute("default");
3582 if (!e.attribute("value").isEmpty()) {
3583 map[name] = e.attribute("value");
3585 if (e.attribute("type") != "addedgeometry" && (e.attribute("factor", "1") != "1" || e.attribute("offset", "0") != "0")) {
3586 map[name] = m_locale.toString((map.value(name).toDouble() - e.attribute("offset", "0").toDouble()) / e.attribute("factor", "1").toDouble());
3587 //map[name]=map[name].replace(".",","); //FIXME how to solve locale conversion of . ,
3590 if (e.attribute("namedesc").contains(';')) {
3591 QString format = e.attribute("format");
3592 QStringList separators = format.split("%d", QString::SkipEmptyParts);
3593 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
3595 QTextStream txtNeu(&neu);
3596 if (values.size() > 0)
3597 txtNeu << (int)values[0].toDouble();
3599 for (i = 0; i < separators.size() && i + 1 < values.size(); i++) {
3600 txtNeu << separators[i];
3601 txtNeu << (int)(values[i+1].toDouble());
3603 if (i < separators.size())
3604 txtNeu << separators[i];
3605 map[e.attribute("name")] = neu;
3612 void Render::mltAddClipTransparency(ItemInfo info, int transitiontrack, int id)
3614 kDebug() << "///////// ADDING CLIP TRANSPARENCY AT: " << info.startPos.frames(25);
3615 Mlt::Service service(m_mltProducer->parent().get_service());
3616 Mlt::Tractor tractor(service);
3617 Mlt::Field *field = tractor.field();
3619 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
3620 transition->set_in_and_out((int) info.startPos.frames(m_fps), (int) info.endPos.frames(m_fps) - 1);
3621 transition->set("transparency", id);
3622 transition->set("fill", 1);
3623 transition->set("internal_added", 237);
3624 field->plant_transition(*transition, transitiontrack, info.track);
3628 void Render::mltDeleteTransparency(int pos, int track, int id)
3630 Mlt::Service service(m_mltProducer->parent().get_service());
3631 Mlt::Tractor tractor(service);
3632 Mlt::Field *field = tractor.field();
3634 //if (do_refresh) m_mltConsumer->set("refresh", 0);
3635 mlt_service serv = m_mltProducer->parent().get_service();
3637 mlt_service nextservice = mlt_service_get_producer(serv);
3638 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3639 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3640 QString resource = mlt_properties_get(properties, "mlt_service");
3642 while (mlt_type == "transition") {
3643 mlt_transition tr = (mlt_transition) nextservice;
3644 int currentTrack = mlt_transition_get_b_track(tr);
3645 int currentIn = (int) mlt_transition_get_in(tr);
3646 int currentOut = (int) mlt_transition_get_out(tr);
3647 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3648 kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
3650 if (resource == "composite" && track == currentTrack && currentIn == pos && transitionId == id) {
3651 //kDebug() << " / / / / /DELETE TRANS DOOOMNE";
3652 mlt_field_disconnect_service(field->get_field(), nextservice);
3655 nextservice = mlt_service_producer(nextservice);
3656 if (nextservice == NULL) break;
3657 properties = MLT_SERVICE_PROPERTIES(nextservice);
3658 mlt_type = mlt_properties_get(properties, "mlt_type");
3659 resource = mlt_properties_get(properties, "mlt_service");
3661 //if (do_refresh) m_mltConsumer->set("refresh", 1);
3664 void Render::mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id)
3666 Mlt::Service service(m_mltProducer->parent().get_service());
3667 Mlt::Tractor tractor(service);
3669 mlt_service_lock(service.get_service());
3670 m_mltConsumer->set("refresh", 0);
3672 mlt_service serv = m_mltProducer->parent().get_service();
3673 mlt_service nextservice = mlt_service_get_producer(serv);
3674 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3675 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3676 QString resource = mlt_properties_get(properties, "mlt_service");
3677 kDebug() << "// resize transpar from: " << oldStart << ", TO: " << newStart << 'x' << newEnd << ", " << track << ", " << id;
3678 while (mlt_type == "transition") {
3679 mlt_transition tr = (mlt_transition) nextservice;
3680 int currentTrack = mlt_transition_get_b_track(tr);
3681 int currentIn = (int) mlt_transition_get_in(tr);
3682 //mlt_properties props = MLT_TRANSITION_PROPERTIES(tr);
3683 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3684 kDebug() << "// resize transpar current in: " << currentIn << ", Track: " << currentTrack << ", id: " << id << 'x' << transitionId ;
3685 if (resource == "composite" && track == currentTrack && currentIn == oldStart && transitionId == id) {
3686 kDebug() << " / / / / /RESIZE TRANS TO: " << newStart << 'x' << newEnd;
3687 mlt_transition_set_in_and_out(tr, newStart, newEnd);
3690 nextservice = mlt_service_producer(nextservice);
3691 if (nextservice == NULL) break;
3692 properties = MLT_SERVICE_PROPERTIES(nextservice);
3693 mlt_type = mlt_properties_get(properties, "mlt_type");
3694 resource = mlt_properties_get(properties, "mlt_service");
3696 mlt_service_unlock(service.get_service());
3697 m_mltConsumer->set("refresh", 1);
3701 void Render::mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id)
3703 Mlt::Service service(m_mltProducer->parent().get_service());
3704 Mlt::Tractor tractor(service);
3706 mlt_service_lock(service.get_service());
3707 m_mltConsumer->set("refresh", 0);
3709 mlt_service serv = m_mltProducer->parent().get_service();
3710 mlt_service nextservice = mlt_service_get_producer(serv);
3711 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3712 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3713 QString resource = mlt_properties_get(properties, "mlt_service");
3715 while (mlt_type == "transition") {
3716 mlt_transition tr = (mlt_transition) nextservice;
3717 int currentTrack = mlt_transition_get_b_track(tr);
3718 int currentaTrack = mlt_transition_get_a_track(tr);
3719 int currentIn = (int) mlt_transition_get_in(tr);
3720 int currentOut = (int) mlt_transition_get_out(tr);
3721 //mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3722 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3723 //kDebug()<<" + TRANSITION "<<id<<" == "<<transitionId<<", START TMIE: "<<currentIn<<", LOOK FR: "<<startTime<<", TRACK: "<<currentTrack<<'x'<<startTrack;
3724 if (resource == "composite" && transitionId == id && startTime == currentIn && startTrack == currentTrack) {
3725 kDebug() << "//////MOVING";
3726 mlt_transition_set_in_and_out(tr, endTime, endTime + currentOut - currentIn);
3727 if (endTrack != startTrack) {
3728 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3729 mlt_properties_set_int(properties, "a_track", currentaTrack + endTrack - currentTrack);
3730 mlt_properties_set_int(properties, "b_track", endTrack);
3734 nextservice = mlt_service_producer(nextservice);
3735 if (nextservice == NULL) break;
3736 properties = MLT_SERVICE_PROPERTIES(nextservice);
3737 mlt_type = mlt_properties_get(properties, "mlt_type");
3738 resource = mlt_properties_get(properties, "mlt_service");
3740 mlt_service_unlock(service.get_service());
3741 m_mltConsumer->set("refresh", 1);
3745 bool Render::mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool do_refresh)
3747 if (in >= out) return false;
3748 QMap<QString, QString> args = mltGetTransitionParamsFromXml(xml);
3749 Mlt::Service service(m_mltProducer->parent().get_service());
3751 Mlt::Tractor tractor(service);
3752 Mlt::Field *field = tractor.field();
3754 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, tag.toUtf8().constData());
3755 if (out != GenTime())
3756 transition->set_in_and_out((int) in.frames(m_fps), (int) out.frames(m_fps) - 1);
3758 if (do_refresh && (m_mltProducer->position() < in.frames(m_fps) || m_mltProducer->position() > out.frames(m_fps))) do_refresh = false;
3759 QMap<QString, QString>::Iterator it;
3761 if (xml.attribute("automatic") == "1") transition->set("automatic", 1);
3762 //kDebug() << " ------ ADDING TRANSITION PARAMs: " << args.count();
3763 if (xml.hasAttribute("id"))
3764 transition->set("kdenlive_id", xml.attribute("id").toUtf8().constData());
3765 if (xml.hasAttribute("force_track"))
3766 transition->set("force_track", xml.attribute("force_track").toInt());
3768 for (it = args.begin(); it != args.end(); ++it) {
3770 if (!it.value().isEmpty())
3771 transition->set(key.toUtf8().constData(), it.value().toUtf8().constData());
3772 //kDebug() << " ------ ADDING TRANS PARAM: " << key << ": " << it.value();
3774 // attach transition
3775 mlt_service_lock(service.get_service());
3776 mltPlantTransition(field, *transition, a_track, b_track);
3777 // field->plant_transition(*transition, a_track, b_track);
3778 mlt_service_unlock(service.get_service());
3779 if (do_refresh) refresh();
3783 void Render::mltSavePlaylist()
3785 kWarning() << "// UPDATING PLAYLIST TO DISK++++++++++++++++";
3786 Mlt::Consumer fileConsumer(*m_mltProfile, "xml");
3787 fileConsumer.set("resource", "/tmp/playlist.mlt");
3789 Mlt::Service service(m_mltProducer->get_service());
3791 fileConsumer.connect(service);
3792 fileConsumer.start();
3795 const QList <Mlt::Producer *> Render::producersList()
3797 QList <Mlt::Producer *> prods;
3798 if (m_mltProducer == NULL) return prods;
3799 Mlt::Service service(m_mltProducer->parent().get_service());
3800 if (service.type() != tractor_type) return prods;
3801 Mlt::Tractor tractor(service);
3804 int trackNb = tractor.count();
3805 for (int t = 1; t < trackNb; t++) {
3806 Mlt::Producer *tt = tractor.track(t);
3807 Mlt::Producer trackProducer(tt);
3809 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3810 int clipNb = trackPlaylist.count();
3811 for (int i = 0; i < clipNb; i++) {
3812 Mlt::Producer *c = trackPlaylist.get_clip(i);
3813 if (c == NULL) continue;
3814 QString prodId = c->parent().get("id");
3815 if (!c->is_blank() && !ids.contains(prodId) && !prodId.startsWith("slowmotion") && !prodId.isEmpty()) {
3816 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
3819 prods.append(nprod);
3828 void Render::fillSlowMotionProducers()
3830 if (m_mltProducer == NULL) return;
3831 Mlt::Service service(m_mltProducer->parent().get_service());
3832 if (service.type() != tractor_type) return;
3834 Mlt::Tractor tractor(service);
3836 int trackNb = tractor.count();
3837 for (int t = 1; t < trackNb; t++) {
3838 Mlt::Producer *tt = tractor.track(t);
3839 Mlt::Producer trackProducer(tt);
3841 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3842 int clipNb = trackPlaylist.count();
3843 for (int i = 0; i < clipNb; i++) {
3844 Mlt::Producer *c = trackPlaylist.get_clip(i);
3845 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
3847 QString id = nprod->parent().get("id");
3848 if (id.startsWith("slowmotion:") && !nprod->is_blank()) {
3849 // this is a slowmotion producer, add it to the list
3850 QString url = QString::fromUtf8(nprod->get("resource"));
3851 int strobe = nprod->get_int("strobe");
3852 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
3853 if (!m_slowmotionProducers.contains(url)) {
3854 m_slowmotionProducers.insert(url, nprod);
3856 } else delete nprod;
3863 void Render::mltInsertTrack(int ix, bool videoTrack)
3867 Mlt::Service service(m_mltProducer->parent().get_service());
3868 mlt_service_lock(service.get_service());
3869 if (service.type() != tractor_type) {
3870 kWarning() << "// TRACTOR PROBLEM";
3874 Mlt::Tractor tractor(service);
3876 Mlt::Playlist playlist;
3877 int ct = tractor.count();
3879 kDebug() << "// ERROR, TRYING TO insert TRACK " << ix << ", max: " << ct;
3885 Mlt::Producer *prodToMove = new Mlt::Producer(tractor.track(pos));
3886 tractor.set_track(playlist, pos);
3887 Mlt::Producer newProd(tractor.track(pos));
3888 if (!videoTrack) newProd.set("hide", 1);
3890 for (; pos <= ct; pos++) {
3891 Mlt::Producer *prodToMove2 = new Mlt::Producer(tractor.track(pos));
3892 tractor.set_track(*prodToMove, pos);
3893 prodToMove = prodToMove2;
3896 tractor.set_track(playlist, ix);
3897 Mlt::Producer newProd(tractor.track(ix));
3898 if (!videoTrack) newProd.set("hide", 1);
3904 mlt_service serv = m_mltProducer->parent().get_service();
3905 mlt_service nextservice = mlt_service_get_producer(serv);
3906 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3907 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3908 QString resource = mlt_properties_get(properties, "mlt_service");
3910 while (mlt_type == "transition") {
3911 if (resource != "mix") {
3912 mlt_transition tr = (mlt_transition) nextservice;
3913 int currentTrack = mlt_transition_get_b_track(tr);
3914 int currentaTrack = mlt_transition_get_a_track(tr);
3915 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3917 if (currentTrack >= ix) {
3918 mlt_properties_set_int(properties, "b_track", currentTrack + 1);
3919 mlt_properties_set_int(properties, "a_track", currentaTrack + 1);
3922 nextservice = mlt_service_producer(nextservice);
3923 if (nextservice == NULL) break;
3924 properties = MLT_SERVICE_PROPERTIES(nextservice);
3925 mlt_type = mlt_properties_get(properties, "mlt_type");
3926 resource = mlt_properties_get(properties, "mlt_service");
3929 // Add audio mix transition to last track
3930 Mlt::Field *field = tractor.field();
3931 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "mix");
3932 transition->set("a_track", 1);
3933 transition->set("b_track", ct);
3934 transition->set("always_active", 1);
3935 transition->set("internal_added", 237);
3936 transition->set("combine", 1);
3937 field->plant_transition(*transition, 1, ct);
3938 //mlt_service_unlock(m_mltConsumer->get_service());
3939 mlt_service_unlock(service.get_service());
3940 //tractor.multitrack()->refresh();
3941 //tractor.refresh();
3942 blockSignals(false);
3946 void Render::mltDeleteTrack(int ix)
3949 doc.setContent(sceneList(), false);
3950 int tracksCount = doc.elementsByTagName("track").count() - 1;
3951 QDomNode track = doc.elementsByTagName("track").at(ix);
3952 QDomNode tractor = doc.elementsByTagName("tractor").at(0);
3953 QDomNodeList transitions = doc.elementsByTagName("transition");
3954 for (int i = 0; i < transitions.count(); i++) {
3955 QDomElement e = transitions.at(i).toElement();
3956 QDomNodeList props = e.elementsByTagName("property");
3957 QMap <QString, QString> mappedProps;
3958 for (int j = 0; j < props.count(); j++) {
3959 QDomElement f = props.at(j).toElement();
3960 mappedProps.insert(f.attribute("name"), f.firstChild().nodeValue());
3962 if (mappedProps.value("mlt_service") == "mix" && mappedProps.value("b_track").toInt() == tracksCount) {
3963 tractor.removeChild(transitions.at(i));
3965 } else if (mappedProps.value("mlt_service") != "mix" && (mappedProps.value("b_track").toInt() >= ix || mappedProps.value("a_track").toInt() >= ix)) {
3966 // Transition needs to be moved
3967 int a_track = mappedProps.value("a_track").toInt();
3968 int b_track = mappedProps.value("b_track").toInt();
3969 if (a_track > 0 && a_track >= ix) a_track --;
3970 if (b_track == ix) {
3971 // transition was on the deleted track, so remove it
3972 tractor.removeChild(transitions.at(i));
3976 if (b_track > 0 && b_track > ix) b_track --;
3977 for (int j = 0; j < props.count(); j++) {
3978 QDomElement f = props.at(j).toElement();
3979 if (f.attribute("name") == "a_track") f.firstChild().setNodeValue(QString::number(a_track));
3980 else if (f.attribute("name") == "b_track") f.firstChild().setNodeValue(QString::number(b_track));
3985 tractor.removeChild(track);
3986 //kDebug() << "/////////// RESULT SCENE: \n" << doc.toString();
3987 setSceneList(doc.toString(), m_framePosition);
3988 emit refreshDocumentProducers(false, false);
3992 void Render::updatePreviewSettings()
3994 kDebug() << "////// RESTARTING CONSUMER";
3995 if (!m_mltConsumer || !m_mltProducer) return;
3996 if (m_mltProducer->get_playtime() == 0) return;
3997 Mlt::Service service(m_mltProducer->parent().get_service());
3998 if (service.type() != tractor_type) return;
4000 //m_mltConsumer->set("refresh", 0);
4001 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
4002 m_mltConsumer->purge();
4003 QString scene = sceneList();
4005 if (m_mltProducer) {
4006 pos = m_mltProducer->position();
4009 setSceneList(scene, pos);
4013 QString Render::updateSceneListFps(double current_fps, double new_fps, QString scene)
4015 // Update all frame positions to the new fps value
4016 //WARNING: there are probably some effects or other that hold a frame value
4017 // as parameter and will also need to be updated here!
4019 doc.setContent(scene);
4021 double factor = new_fps / current_fps;
4022 QDomNodeList producers = doc.elementsByTagName("producer");
4023 for (int i = 0; i < producers.count(); i++) {
4024 QDomElement prod = producers.at(i).toElement();
4025 prod.removeAttribute("in");
4026 prod.removeAttribute("out");
4028 QDomNodeList props = prod.childNodes();
4029 for (int j = 0; j < props.count(); j++) {
4030 QDomElement param = props.at(j).toElement();
4031 QString paramName = param.attribute("name");
4032 if (paramName.startsWith("meta.") || paramName == "length") {
4033 prod.removeChild(props.at(j));
4039 QDomNodeList entries = doc.elementsByTagName("entry");
4040 for (int i = 0; i < entries.count(); i++) {
4041 QDomElement entry = entries.at(i).toElement();
4042 int in = entry.attribute("in").toInt();
4043 int out = entry.attribute("out").toInt();
4044 in = factor * in + 0.5;
4045 out = factor * out + 0.5;
4046 entry.setAttribute("in", in);
4047 entry.setAttribute("out", out);
4050 QDomNodeList blanks = doc.elementsByTagName("blank");
4051 for (int i = 0; i < blanks.count(); i++) {
4052 QDomElement blank = blanks.at(i).toElement();
4053 int length = blank.attribute("length").toInt();
4054 length = factor * length + 0.5;
4055 blank.setAttribute("length", QString::number(length));
4058 QDomNodeList filters = doc.elementsByTagName("filter");
4059 for (int i = 0; i < filters.count(); i++) {
4060 QDomElement filter = filters.at(i).toElement();
4061 int in = filter.attribute("in").toInt();
4062 int out = filter.attribute("out").toInt();
4063 in = factor * in + 0.5;
4064 out = factor * out + 0.5;
4065 filter.setAttribute("in", in);
4066 filter.setAttribute("out", out);
4069 QDomNodeList transitions = doc.elementsByTagName("transition");
4070 for (int i = 0; i < transitions.count(); i++) {
4071 QDomElement transition = transitions.at(i).toElement();
4072 int in = transition.attribute("in").toInt();
4073 int out = transition.attribute("out").toInt();
4074 in = factor * in + 0.5;
4075 out = factor * out + 0.5;
4076 transition.setAttribute("in", in);
4077 transition.setAttribute("out", out);
4078 QDomNodeList props = transition.childNodes();
4079 for (int j = 0; j < props.count(); j++) {
4080 QDomElement param = props.at(j).toElement();
4081 QString paramName = param.attribute("name");
4082 if (paramName == "geometry") {
4083 QString geom = param.firstChild().nodeValue();
4084 QStringList keys = geom.split(';');
4085 QStringList newKeys;
4086 for (int k = 0; k < keys.size(); ++k) {
4087 if (keys.at(k).contains('=')) {
4088 int pos = keys.at(k).section('=', 0, 0).toInt();
4089 pos = factor * pos + 0.5;
4090 newKeys.append(QString::number(pos) + '=' + keys.at(k).section('=', 1));
4091 } else newKeys.append(keys.at(k));
4093 param.firstChild().setNodeValue(newKeys.join(";"));
4097 QDomElement tractor = doc.elementsByTagName("tractor").at(0).toElement();
4098 int out = tractor.attribute("out").toInt();
4099 out = factor * out + 0.5;
4100 tractor.setAttribute("out", out);
4101 emit durationChanged(out);
4103 //kDebug() << "///////////////////////////// " << out << " \n" << doc.toString() << "\n-------------------------";
4104 return doc.toString();
4108 void Render::sendFrameUpdate()
4110 if (m_mltProducer) {
4111 Mlt::Frame * frame = m_mltProducer->get_frame();
4112 emitFrameUpdated(*frame);
4117 Mlt::Producer* Render::getProducer()
4119 return m_mltProducer;
4123 #include "renderer.moc"