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>
52 static void kdenlive_callback(void* /*ptr*/, int level, const char* fmt, va_list vl)
54 if (level > MLT_LOG_ERROR) return;
56 QApplication::postEvent(qApp->activeWindow(), new MltErrorEvent(error.vsprintf(fmt, vl).simplified()));
61 static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr)
63 // detect if the producer has finished playing. Is there a better way to do it?
64 if (self->m_isBlocked) return;
65 Mlt::Frame frame(frame_ptr);
66 if (!frame.is_valid()) return;
67 self->emitFrameNumber(mlt_frame_get_position(frame_ptr));
68 if (self->sendFrameForAnalysis && frame_ptr->convert_image) {
69 self->emitFrameUpdated(frame);
71 if (self->analyseAudio) {
72 self->showAudio(frame);
74 if (frame.get_double("_speed") == 0.0) {
75 self->emitConsumerStopped();
76 } else if (frame.get_double("_speed") < 0.0 && mlt_frame_get_position(frame_ptr) <= 0) {
78 self->emitConsumerStopped();
82 static void consumer_gl_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr)
84 // detect if the producer has finished playing. Is there a better way to do it?
85 if (self->m_isBlocked) return;
86 Mlt::Frame frame(frame_ptr);
87 self->showFrame(frame);
88 if (frame.get_double("_speed") == 0.0) {
89 self->emitConsumerStopped();
90 } else if (frame.get_double("_speed") < 0.0 && mlt_frame_get_position(frame_ptr) <= 0) {
92 self->emitConsumerStopped();
96 Render::Render(const QString & rendererName, int winid, QString profile, QWidget *parent) :
99 analyseAudio(KdenliveSettings::monitor_audio()),
100 sendFrameForAnalysis(false),
101 m_name(rendererName),
106 m_externalConsumer(false),
109 m_isSplitView(false),
113 if (profile.isEmpty()) profile = KdenliveSettings::current_profile();
114 buildConsumer(profile);
116 m_mltProducer = m_blackClip->cut(0, 50);
117 m_mltConsumer->connect(*m_mltProducer);
118 m_mltProducer->set_speed(0.0);
129 void Render::closeMlt()
133 Mlt::Service service(m_mltProducer->parent().get_service());
134 mlt_service_lock(service.get_service());
136 if (service.type() == tractor_type) {
137 Mlt::Tractor tractor(service);
138 Mlt::Field *field = tractor.field();
139 mlt_service nextservice = mlt_service_get_producer(service.get_service());
140 mlt_service nextservicetodisconnect;
141 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
142 QString mlt_type = mlt_properties_get(properties, "mlt_type");
143 QString resource = mlt_properties_get(properties, "mlt_service");
144 // Delete all transitions
145 while (mlt_type == "transition") {
146 nextservicetodisconnect = nextservice;
147 nextservice = mlt_service_producer(nextservice);
148 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
149 if (nextservice == NULL) break;
150 properties = MLT_SERVICE_PROPERTIES(nextservice);
151 mlt_type = mlt_properties_get(properties, "mlt_type");
152 resource = mlt_properties_get(properties, "mlt_service");
155 for (int trackNb = tractor.count() - 1; trackNb >= 0; --trackNb) {
156 Mlt::Producer trackProducer(tractor.track(trackNb));
157 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
158 if (trackPlaylist.type() == playlist_type) trackPlaylist.clear();
163 mlt_service_unlock(service.get_service());
166 kDebug() << "// // // CLOSE RENDERER " << m_name;
167 delete m_mltConsumer;
168 delete m_mltProducer;
173 void Render::slotSwitchFullscreen()
175 if (m_mltConsumer) m_mltConsumer->set("full_screen", 1);
178 void Render::buildConsumer(const QString profileName)
180 m_activeProfile = profileName;
181 char *tmp = qstrdup(m_activeProfile.toUtf8().constData());
182 setenv("MLT_PROFILE", tmp, 1);
186 //TODO: uncomment following line when everything is clean
187 // uncommented Feb 2011 --Granjow
188 if (m_mltProfile) delete m_mltProfile;
190 m_mltProfile = new Mlt::Profile(tmp);
191 m_mltProfile->get_profile()->is_explicit = 1;
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->set("window_id", m_winid);
246 m_mltConsumer->set("resize", 1);
247 m_mltConsumer->set("terminate_on_pause", 1);
248 m_mltConsumer->set("window_background", KdenliveSettings::window_background().name().toUtf8().constData());
249 m_mltConsumer->set("rescale", "nearest");
250 mlt_log_set_callback(kdenlive_callback);
252 QString audioDevice = KdenliveSettings::audiodevicename();
253 if (!audioDevice.isEmpty())
254 m_mltConsumer->set("audio_device", audioDevice.toUtf8().constData());
256 if (!videoDriver.isEmpty())
257 m_mltConsumer->set("video_driver", videoDriver.toUtf8().constData());
259 QString audioDriver = KdenliveSettings::audiodrivername();
262 // Disabled because the "auto" detected driver was sometimes wrong
263 if (audioDriver.isEmpty())
264 audioDriver = KdenliveSettings::autoaudiodrivername();
267 if (!audioDriver.isEmpty())
268 m_mltConsumer->set("audio_driver", audioDriver.toUtf8().constData());
270 m_mltConsumer->set("progressive", 1);
271 m_mltConsumer->set("audio_buffer", 1024);
272 m_mltConsumer->set("frequency", 48000);
273 m_mltConsumer->set("real_time", KdenliveSettings::mltthreads());
276 Mlt::Producer *Render::invalidProducer(const QString &id)
278 Mlt::Producer *clip = new Mlt::Producer(*m_mltProfile, "colour", "red");
279 clip->set("id", id.toUtf8().constData());
280 clip->set("mlt_type", "producer");
284 int Render::resetProfile(const QString profileName)
287 if (m_externalConsumer == KdenliveSettings::external_display()) {
288 if (KdenliveSettings::external_display() && m_activeProfile == profileName) return 1;
289 QString videoDriver = KdenliveSettings::videodrivername();
290 QString currentDriver = m_mltConsumer->get("video_driver");
291 if (getenv("SDL_VIDEO_YUV_HWACCEL") != NULL && currentDriver == "x11") currentDriver = "x11_noaccel";
292 QString background = KdenliveSettings::window_background().name();
293 QString currentBackground = m_mltConsumer->get("window_background");
294 if (m_activeProfile == profileName && currentDriver == videoDriver && background == currentBackground) {
295 kDebug() << "reset to same profile, nothing to do";
300 if (m_isSplitView) slotSplitView(false);
301 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
302 m_mltConsumer->purge();
303 delete m_mltConsumer;
304 m_mltConsumer = NULL;
306 QString scene = sceneList();
308 double current_fps = m_mltProfile->fps();
313 pos = m_mltProducer->position();
315 Mlt::Service service(m_mltProducer->get_service());
316 if (service.type() == tractor_type) {
317 Mlt::Tractor tractor(service);
318 for (int trackNb = tractor.count() - 1; trackNb >= 0; --trackNb) {
319 Mlt::Producer trackProducer(tractor.track(trackNb));
320 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
321 trackPlaylist.clear();
325 delete m_mltProducer;
327 m_mltProducer = NULL;
328 buildConsumer(profileName);
329 double new_fps = m_mltProfile->fps();
330 if (current_fps != new_fps) {
331 // fps changed, we must update the scenelist positions
332 scene = updateSceneListFps(current_fps, new_fps, scene);
334 //kDebug() << "//RESET WITHSCENE: " << scene;
335 setSceneList(scene, pos);
336 // producers have changed (different profile), so reset them...
337 emit refreshDocumentProducers();
338 /*Mlt::Producer *producer = new Mlt::Producer(*m_mltProfile , "xml-string", scene.toUtf8().constData());
339 m_mltProducer = producer;
340 m_blackClip = new Mlt::Producer(*m_mltProfile , "colour", "black");
341 m_mltProducer->optimise();
342 m_mltProducer->set_speed(0);
345 //delete m_mltProfile;
346 // mlt_properties properties = MLT_CONSUMER_PROPERTIES(m_mltConsumer->get_consumer());
347 //mlt_profile prof = m_mltProfile->get_profile();
348 //mlt_properties_set_data(properties, "_profile", prof, 0, (mlt_destructor)mlt_profile_close, NULL);
349 //mlt_properties_set(properties, "profile", "hdv_1080_50i");
350 //m_mltConsumer->set("profile", (char *) profile.toUtf8().data());
351 //m_mltProfile = new Mlt::Profile((char*) profile.toUtf8().data());
353 //apply_profile_properties( m_mltProfile, m_mltConsumer->get_consumer(), properties );
358 void Render::seek(GenTime time)
363 m_mltProducer->seek((int)(time.frames(m_fps)));
367 void Render::seek(int time)
372 m_mltProducer->seek(time);
377 /*QPixmap Render::frameThumbnail(Mlt::Frame *frame, int width, int height, bool border) {
378 QPixmap pix(width, height);
380 mlt_image_format format = mlt_image_rgb24a;
381 uint8_t *thumb = frame->get_image(format, width, height);
382 QImage image(thumb, width, height, QImage::Format_ARGB32);
384 if (!image.isNull()) {
385 pix = pix.fromImage(image);
387 QPainter painter(&pix);
388 painter.drawRect(0, 0, width - 1, height - 1);
390 } else pix.fill(Qt::black);
394 int Render::frameRenderWidth() const
396 return m_mltProfile->width();
399 int Render::renderWidth() const
401 return (int)(m_mltProfile->height() * m_mltProfile->dar() + 0.5);
404 int Render::renderHeight() const
406 return m_mltProfile->height();
409 QImage Render::extractFrame(int frame_position, int width, int height)
412 width = renderWidth();
413 height = renderHeight();
414 } else if (width % 2 == 1) width++;
416 if (!m_mltProducer) {
417 QImage pix(width, height, QImage::Format_RGB32);
421 return KThumb::getFrame(m_mltProducer, frame_position, width, height);
424 QPixmap Render::getImageThumbnail(KUrl url, int /*width*/, int /*height*/)
428 if (url.fileName().startsWith(".all.")) { // check for slideshow
429 QString fileType = url.fileName().right(3);
431 QStringList::Iterator it;
433 QDir dir(url.directory());
435 filter << "*." + fileType;
436 filter << "*." + fileType.toUpper();
437 more = dir.entryList(filter, QDir::Files);
438 im.load(url.directory() + '/' + more.at(0));
439 } else im.load(url.path());
440 //pixmap = im.scaled(width, height);
444 double Render::consumerRatio() const
446 if (!m_mltConsumer) return 1.0;
447 return (m_mltConsumer->get_double("aspect_ratio_num") / m_mltConsumer->get_double("aspect_ratio_den"));
451 int Render::getLength()
455 // kDebug()<<"////// LENGTH: "<<mlt_producer_get_playtime(m_mltProducer->get_producer());
456 return mlt_producer_get_playtime(m_mltProducer->get_producer());
461 bool Render::isValid(KUrl url)
463 Mlt::Producer producer(*m_mltProfile, url.path().toUtf8().constData());
464 if (producer.is_blank())
470 double Render::dar() const
472 return m_mltProfile->dar();
475 double Render::sar() const
477 return m_mltProfile->sar();
480 void Render::slotSplitView(bool doit)
482 m_isSplitView = doit;
483 Mlt::Service service(m_mltProducer->parent().get_service());
484 Mlt::Tractor tractor(service);
485 if (service.type() != tractor_type || tractor.count() < 2) return;
486 Mlt::Field *field = tractor.field();
488 for (int i = 1, screen = 0; i < tractor.count() && screen < 4; i++) {
489 Mlt::Producer trackProducer(tractor.track(i));
490 kDebug() << "// TRACK: " << i << ", HIDE: " << trackProducer.get("hide");
491 if (QString(trackProducer.get("hide")).toInt() != 1) {
492 kDebug() << "// ADIDNG TRACK: " << i;
493 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
494 transition->set("mlt_service", "composite");
495 transition->set("a_track", 0);
496 transition->set("b_track", i);
497 transition->set("distort", 1);
498 transition->set("internal_added", "200");
505 tmp = "50%,0:50%x50%";
508 tmp = "0,50%:50%x50%";
512 tmp = "50%,50%:50%x50%";
515 transition->set("geometry", tmp);
516 transition->set("always_active", "1");
517 field->plant_transition(*transition, 0, i);
522 m_mltConsumer->set("refresh", 1);
524 mlt_service serv = m_mltProducer->parent().get_service();
525 mlt_service nextservice = mlt_service_get_producer(serv);
526 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
527 QString mlt_type = mlt_properties_get(properties, "mlt_type");
528 QString resource = mlt_properties_get(properties, "mlt_service");
530 while (mlt_type == "transition") {
531 QString added = mlt_properties_get(MLT_SERVICE_PROPERTIES(nextservice), "internal_added");
532 if (added == "200") {
533 mlt_field_disconnect_service(field->get_field(), nextservice);
535 nextservice = mlt_service_producer(nextservice);
536 if (nextservice == NULL) break;
537 properties = MLT_SERVICE_PROPERTIES(nextservice);
538 mlt_type = mlt_properties_get(properties, "mlt_type");
539 resource = mlt_properties_get(properties, "mlt_service");
540 m_mltConsumer->set("refresh", 1);
545 void Render::getFileProperties(const QDomElement xml, const QString &clipId, int imageHeight, bool replaceProducer, bool selectClip)
549 if (xml.hasAttribute("proxy") && xml.attribute("proxy") != "-") {
550 path = xml.attribute("proxy");
551 proxyProducer = true;
554 path = xml.attribute("resource");
555 proxyProducer = false;
559 KUrl url = KUrl(path);
560 Mlt::Producer *producer = NULL;
561 CLIPTYPE type = (CLIPTYPE)xml.attribute("type").toInt();
562 //kDebug() << "PROFILE WIDT: "<< xml.attribute("mlt_service") << ": "<< m_mltProfile->width() << "\n...................\n\n";
563 /*if (xml.attribute("type").toInt() == TEXT && !QFile::exists(url.path())) {
564 emit replyGetFileProperties(clipId, producer, QMap < QString, QString >(), QMap < QString, QString >(), replaceProducer);
569 producer = new Mlt::Producer(*m_mltProfile, 0, ("colour:" + xml.attribute("colour")).toUtf8().constData());
570 } else if (type == TEXT) {
571 producer = new Mlt::Producer(*m_mltProfile, 0, ("kdenlivetitle:" + xml.attribute("resource")).toUtf8().constData());
572 if (producer && producer->is_valid() && xml.hasAttribute("xmldata"))
573 producer->set("xmldata", xml.attribute("xmldata").toUtf8().constData());
574 } else if (url.isEmpty()) {
576 QDomElement mlt = doc.createElement("mlt");
577 QDomElement play = doc.createElement("playlist");
578 doc.appendChild(mlt);
579 mlt.appendChild(play);
580 play.appendChild(doc.importNode(xml, true));
581 producer = new Mlt::Producer(*m_mltProfile, "xml-string", doc.toString().toUtf8().constData());
583 producer = new Mlt::Producer(*m_mltProfile, url.path().toUtf8().constData());
587 if (producer == NULL || producer->is_blank() || !producer->is_valid()) {
588 kDebug() << " / / / / / / / / ERROR / / / / // CANNOT LOAD PRODUCER: ";
590 // Proxy file is corrupted
591 emit removeInvalidProxy(clipId, false);
593 else emit removeInvalidClip(clipId, replaceProducer);
598 if (proxyProducer && xml.hasAttribute("proxy_out") && producer->get_out() != xml.attribute("proxy_out").toInt()) {
599 // Proxy file length is different than original clip length, this will corrupt project so disable this proxy clip
600 emit removeInvalidProxy(clipId, true);
603 if (xml.hasAttribute("force_aspect_ratio")) {
604 double aspect = xml.attribute("force_aspect_ratio").toDouble();
605 if (aspect > 0) producer->set("force_aspect_ratio", aspect);
608 if (xml.hasAttribute("force_aspect_num") && xml.hasAttribute("force_aspect_den")) {
609 int width = xml.attribute("frame_size").section('x', 0, 0).toInt();
610 int height = xml.attribute("frame_size").section('x', 1, 1).toInt();
611 int aspectNumerator = xml.attribute("force_aspect_num").toInt();
612 int aspectDenominator = xml.attribute("force_aspect_den").toInt();
613 if (aspectDenominator != 0 && width != 0)
614 producer->set("force_aspect_ratio", double(height) * aspectNumerator / aspectDenominator / width);
617 if (xml.hasAttribute("force_fps")) {
618 double fps = xml.attribute("force_fps").toDouble();
619 if (fps > 0) producer->set("force_fps", fps);
622 if (xml.hasAttribute("force_progressive")) {
624 int progressive = xml.attribute("force_progressive").toInt(&ok);
625 if (ok) producer->set("force_progressive", progressive);
627 if (xml.hasAttribute("force_tff")) {
629 int fieldOrder = xml.attribute("force_tff").toInt(&ok);
630 if (ok) producer->set("force_tff", fieldOrder);
632 if (xml.hasAttribute("threads")) {
633 int threads = xml.attribute("threads").toInt();
634 if (threads != 1) producer->set("threads", threads);
636 if (xml.hasAttribute("video_index")) {
637 int vindex = xml.attribute("video_index").toInt();
638 if (vindex != 0) producer->set("video_index", vindex);
640 if (xml.hasAttribute("audio_index")) {
641 int aindex = xml.attribute("audio_index").toInt();
642 if (aindex != 0) producer->set("audio_index", aindex);
644 if (xml.hasAttribute("force_colorspace")) {
645 int colorspace = xml.attribute("force_colorspace").toInt();
646 if (colorspace != 0) producer->set("force_colorspace", colorspace);
648 if (xml.hasAttribute("full_luma")) {
649 int full_luma = xml.attribute("full_luma").toInt();
650 if (full_luma != 0) producer->set("set.force_full_luma", full_luma);
655 if (xml.hasAttribute("out")) clipOut = xml.attribute("out").toInt();
657 // setup length here as otherwise default length (currently 15000 frames in MLT) will be taken even if outpoint is larger
658 if (type == COLOR || type == TEXT || type == IMAGE || type == SLIDESHOW) {
660 if (xml.hasAttribute("length")) {
661 if (clipOut > 0) duration = clipOut + 1;
662 length = xml.attribute("length").toInt();
663 clipOut = length - 1;
665 else length = xml.attribute("out").toInt() - xml.attribute("in").toInt();
666 producer->set("length", length);
669 if (clipOut > 0) producer->set_in_and_out(xml.attribute("in").toInt(), clipOut);
671 producer->set("id", clipId.toUtf8().constData());
673 if (xml.hasAttribute("templatetext"))
674 producer->set("templatetext", xml.attribute("templatetext").toUtf8().constData());
676 if ((!replaceProducer && xml.hasAttribute("file_hash")) || xml.hasAttribute("proxy")) {
677 // Clip already has all properties
678 emit replyGetFileProperties(clipId, producer, QMap < QString, QString >(), QMap < QString, QString >(), replaceProducer, selectClip);
682 int width = (int)(imageHeight * m_mltProfile->dar() + 0.5);
683 QMap < QString, QString > filePropertyMap;
684 QMap < QString, QString > metadataPropertyMap;
686 int frameNumber = xml.attribute("thumbnail", "0").toInt();
687 if (frameNumber != 0) producer->seek(frameNumber);
689 filePropertyMap["duration"] = QString::number(duration > 0 ? duration : producer->get_playtime());
690 //kDebug() << "/////// PRODUCER: " << url.path() << " IS: " << producer->get_playtime();
692 Mlt::Frame *frame = producer->get_frame();
694 if (type == SLIDESHOW) {
695 int ttl = xml.hasAttribute("ttl") ? xml.attribute("ttl").toInt() : 0;
696 if (ttl) producer->set("ttl", ttl);
697 if (!xml.attribute("animation").isEmpty()) {
698 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "affine");
699 if (filter && filter->is_valid()) {
701 QString geometry = SlideshowClip::animationToGeometry(xml.attribute("animation"), cycle);
702 if (!geometry.isEmpty()) {
703 if (xml.attribute("animation").contains("low-pass")) {
704 Mlt::Filter *blur = new Mlt::Filter(*m_mltProfile, "boxblur");
705 if (blur && blur->is_valid())
706 producer->attach(*blur);
708 filter->set("transition.geometry", geometry.toUtf8().data());
709 filter->set("transition.cycle", cycle);
710 producer->attach(*filter);
714 if (xml.attribute("fade") == "1") {
715 // user wants a fade effect to slideshow
716 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "luma");
717 if (filter && filter->is_valid()) {
718 if (ttl) filter->set("cycle", ttl);
719 if (xml.hasAttribute("luma_duration") && !xml.attribute("luma_duration").isEmpty()) filter->set("duration", xml.attribute("luma_duration").toInt());
720 if (xml.hasAttribute("luma_file") && !xml.attribute("luma_file").isEmpty()) {
721 filter->set("luma.resource", xml.attribute("luma_file").toUtf8().constData());
722 if (xml.hasAttribute("softness")) {
723 int soft = xml.attribute("softness").toInt();
724 filter->set("luma.softness", (double) soft / 100.0);
727 producer->attach(*filter);
730 if (xml.attribute("crop") == "1") {
731 // user wants to center crop the slides
732 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "crop");
733 if (filter && filter->is_valid()) {
734 filter->set("center", 1);
735 producer->attach(*filter);
740 if (producer->get_double("meta.media.frame_rate_den") > 0) {
741 filePropertyMap["fps"] = QString::number(producer->get_double("meta.media.frame_rate_num") / producer->get_double("meta.media.frame_rate_den"));
742 } else filePropertyMap["fps"] = producer->get("source_fps");
744 if (frame && frame->is_valid()) {
745 filePropertyMap["frame_size"] = QString::number(frame->get_int("width")) + 'x' + QString::number(frame->get_int("height"));
746 filePropertyMap["frequency"] = QString::number(frame->get_int("frequency"));
747 filePropertyMap["channels"] = QString::number(frame->get_int("channels"));
748 filePropertyMap["aspect_ratio"] = frame->get("aspect_ratio");
750 if (frame->get_int("test_image") == 0) {
751 if (url.path().endsWith(".mlt") || url.path().endsWith(".westley") || url.path().endsWith(".kdenlive")) {
752 filePropertyMap["type"] = "playlist";
753 metadataPropertyMap["comment"] = QString::fromUtf8(producer->get("title"));
754 } else if (frame->get_int("test_audio") == 0)
755 filePropertyMap["type"] = "av";
757 filePropertyMap["type"] = "video";
760 mlt_image_format format = mlt_image_rgb24a;
761 int frame_width = width;
762 int frame_height = imageHeight;
766 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
767 QImage image((uchar *)data, frame_width, frame_height, QImage::Format_ARGB32_Premultiplied);
769 if (!image.isNull()) {
770 if (frame_width > (2 * width)) {
771 // there was a scaling problem, do it manually
772 QImage scaled = image.scaled(width, imageHeight);
773 pix = QPixmap::fromImage(scaled.rgbSwapped());
774 } else pix = QPixmap::fromImage(image.rgbSwapped());
775 variance = KThumb::imageVariance(image);
779 if (frameNumber == 0 && variance < 6) {
780 // Thumbnail is not interesting (for example all black, seek to fetch better thumb
782 producer->seek(frameNumber);
784 frame = producer->get_frame();
787 } while (variance == -1);
788 emit replyGetImage(clipId, pix);
790 } else if (frame->get_int("test_audio") == 0) {
791 QPixmap pixmap = KIcon("audio-x-generic").pixmap(QSize(width, imageHeight));
792 emit replyGetImage(clipId, pixmap);
793 filePropertyMap["type"] = "audio";
797 // Retrieve audio / video codec name
801 if (producer->get_int("video_index") > -1) {
802 /*if (context->duration == AV_NOPTS_VALUE) {
803 kDebug() << " / / / / / / / /ERROR / / / CLIP HAS UNKNOWN DURATION";
804 emit removeInvalidClip(clipId);
808 // Get the video_index
809 int default_video = producer->get_int("video_index");
811 int default_audio = producer->get_int("audio_index");
814 // Find maximum stream index values
815 for (int ix = 0; ix < producer->get_int("meta.media.nb_streams"); ix++) {
816 snprintf(property, sizeof(property), "meta.media.%d.stream.type", ix);
817 QString type = producer->get(property);
820 else if (type == "audio")
823 filePropertyMap["default_video"] = QString::number(default_video);
824 filePropertyMap["video_max"] = QString::number(video_max);
825 filePropertyMap["default_audio"] = QString::number(default_audio);
826 filePropertyMap["audio_max"] = QString::number(audio_max);
828 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", default_video);
829 if (producer->get(property)) {
830 filePropertyMap["videocodec"] = producer->get(property);
832 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
833 if (producer->get(property))
834 filePropertyMap["videocodec"] = producer->get(property);
837 query = QString("meta.media.%1.codec.pix_fmt").arg(default_video);
838 filePropertyMap["pix_fmt"] = producer->get(query.toUtf8().constData());
839 filePropertyMap["colorspace"] = producer->get("meta.media.colorspace");
841 } else kDebug() << " / / / / /WARNING, VIDEO CONTEXT IS NULL!!!!!!!!!!!!!!";
842 if (producer->get_int("audio_index") > -1) {
843 // Get the audio_index
844 int index = producer->get_int("audio_index");
846 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", index);
847 if (producer->get(property)) {
848 filePropertyMap["audiocodec"] = producer->get(property);
850 snprintf(property, sizeof(property), "meta.media.%d.codec.name", index);
851 if (producer->get(property))
852 filePropertyMap["audiocodec"] = producer->get(property);
857 Mlt::Properties metadata;
858 metadata.pass_values(*producer, "meta.attr.");
859 int count = metadata.count();
860 for (int i = 0; i < count; i ++) {
861 QString name = metadata.get_name(i);
862 QString value = QString::fromUtf8(metadata.get(i));
863 if (name.endsWith("markup") && !value.isEmpty())
864 metadataPropertyMap[ name.section('.', 0, -2)] = value;
867 emit replyGetFileProperties(clipId, producer, filePropertyMap, metadataPropertyMap, replaceProducer, selectClip);
868 // FIXME: should delete this to avoid a leak...
874 /** Create the producer from the MLT XML QDomDocument */
875 void Render::initSceneList()
877 kDebug() << "-------- INIT SCENE LIST ------_";
879 QDomElement mlt = doc.createElement("mlt");
880 doc.appendChild(mlt);
881 QDomElement prod = doc.createElement("producer");
882 prod.setAttribute("resource", "colour");
883 prod.setAttribute("colour", "red");
884 prod.setAttribute("id", "black");
885 prod.setAttribute("in", "0");
886 prod.setAttribute("out", "0");
888 QDomElement tractor = doc.createElement("tractor");
889 QDomElement multitrack = doc.createElement("multitrack");
891 QDomElement playlist1 = doc.createElement("playlist");
892 playlist1.appendChild(prod);
893 multitrack.appendChild(playlist1);
894 QDomElement playlist2 = doc.createElement("playlist");
895 multitrack.appendChild(playlist2);
896 QDomElement playlist3 = doc.createElement("playlist");
897 multitrack.appendChild(playlist3);
898 QDomElement playlist4 = doc.createElement("playlist");
899 multitrack.appendChild(playlist4);
900 QDomElement playlist5 = doc.createElement("playlist");
901 multitrack.appendChild(playlist5);
902 tractor.appendChild(multitrack);
903 mlt.appendChild(tractor);
904 // kDebug()<<doc.toString();
906 QString tmp = QString("<mlt><producer resource=\"colour\" colour=\"red\" id=\"red\" /><tractor><multitrack><playlist></playlist><playlist></playlist><playlist /><playlist /><playlist></playlist></multitrack></tractor></mlt>");*/
907 setSceneList(doc, 0);
911 int Render::setProducer(Mlt::Producer *producer, int position)
913 if (m_winid == -1) return -1;
916 m_mltConsumer->stop();
919 m_mltConsumer->purge();
922 m_mltProducer->set_speed(0);
923 delete m_mltProducer;
924 m_mltProducer = NULL;
928 m_mltProducer = new Mlt::Producer(producer->get_producer());
929 } else m_mltProducer = m_blackClip->cut(0, 50);
931 if (!m_mltProducer || !m_mltProducer->is_valid()) {
932 kDebug() << " WARNING - - - - -INVALID PLAYLIST: ";
935 int volume = KdenliveSettings::volume();
936 m_mltProducer->set("meta.volume", (double)volume / 100);
937 m_fps = m_mltProducer->get_fps();
938 int error = connectPlaylist();
940 if (position != -1) {
941 m_mltProducer->seek(position);
942 emit rendererPosition(position);
943 } else emit rendererPosition((int) m_mltProducer->position());
948 int Render::setSceneList(QDomDocument list, int position)
950 return setSceneList(list.toString(), position);
953 int Render::setSceneList(QString playlist, int position)
955 if (m_winid == -1) return -1;
959 //kDebug() << "////// RENDER, SET SCENE LIST: " << playlist;
961 // Remove previous profile info
963 doc.setContent(playlist);
964 QDomElement profile = doc.documentElement().firstChildElement("profile");
965 doc.documentElement().removeChild(profile);
966 playlist = doc.toString();
969 if (!m_mltConsumer->is_stopped()) {
970 m_mltConsumer->stop();
972 m_mltConsumer->set("refresh", 0);
974 kWarning() << "/////// ERROR, TRYING TO USE NULL MLT CONSUMER";
979 m_mltProducer->set_speed(0);
980 //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
982 Mlt::Service service(m_mltProducer->parent().get_service());
983 mlt_service_lock(service.get_service());
985 if (service.type() == tractor_type) {
986 Mlt::Tractor tractor(service);
987 Mlt::Field *field = tractor.field();
988 mlt_service nextservice = mlt_service_get_producer(service.get_service());
989 mlt_service nextservicetodisconnect;
990 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
991 QString mlt_type = mlt_properties_get(properties, "mlt_type");
992 QString resource = mlt_properties_get(properties, "mlt_service");
993 // Delete all transitions
994 while (mlt_type == "transition") {
995 nextservicetodisconnect = nextservice;
996 nextservice = mlt_service_producer(nextservice);
997 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
998 if (nextservice == NULL) break;
999 properties = MLT_SERVICE_PROPERTIES(nextservice);
1000 mlt_type = mlt_properties_get(properties, "mlt_type");
1001 resource = mlt_properties_get(properties, "mlt_service");
1004 for (int trackNb = tractor.count() - 1; trackNb >= 0; --trackNb) {
1005 Mlt::Producer trackProducer(tractor.track(trackNb));
1006 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1007 if (trackPlaylist.type() == playlist_type) trackPlaylist.clear();
1011 mlt_service_unlock(service.get_service());
1013 qDeleteAll(m_slowmotionProducers.values());
1014 m_slowmotionProducers.clear();
1016 delete m_mltProducer;
1017 m_mltProducer = NULL;
1022 // TODO: Better way to do this
1023 if (KdenliveSettings::projectloading_avformatnovalidate())
1024 playlist.replace(">avformat</property>", ">avformat-novalidate</property>");
1026 playlist.replace(">avformat-novalidate</property>", ">avformat</property>");
1028 m_mltProducer = new Mlt::Producer(*m_mltProfile, "xml-string", playlist.toUtf8().constData());
1029 if (!m_mltProducer || !m_mltProducer->is_valid()) {
1030 kDebug() << " WARNING - - - - -INVALID PLAYLIST: " << playlist.toUtf8().constData();
1031 m_mltProducer = m_blackClip->cut(0, 50);
1034 int volume = KdenliveSettings::volume();
1035 m_mltProducer->set("meta.volume", (double)volume / 100);
1036 m_mltProducer->optimise();
1038 /*if (KdenliveSettings::osdtimecode()) {
1039 // Attach filter for on screen display of timecode
1041 QString attr = "attr_check";
1042 mlt_filter filter = mlt_factory_filter( "data_feed", (char*) attr.ascii() );
1043 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
1044 mlt_producer_attach( m_mltProducer->get_producer(), filter );
1045 mlt_filter_close( filter );
1047 m_osdInfo = new Mlt::Filter("data_show");
1048 m_osdInfo->set("resource", m_osdProfile.toUtf8().constData());
1049 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1050 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
1051 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
1052 m_osdInfo->set("dynamic", "1");
1054 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1056 m_osdInfo->set("dynamic", "0");
1059 m_fps = m_mltProducer->get_fps();
1060 if (position != 0) {
1061 // Seek to correct place after opening project.
1062 m_mltProducer->seek(position);
1065 kDebug() << "// NEW SCENE LIST DURATION SET TO: " << m_mltProducer->get_playtime();
1066 if (error == 0) error = connectPlaylist();
1067 else connectPlaylist();
1068 fillSlowMotionProducers();
1070 m_isBlocked = false;
1071 blockSignals(false);
1074 //kDebug()<<"// SETSCN LST, POS: "<<position;
1075 //if (position != 0) emit rendererPosition(position);
1078 const QString Render::sceneList()
1081 Mlt::Profile profile((mlt_profile) 0);
1082 Mlt::Consumer xmlConsumer(profile, "xml:kdenlive_playlist");
1083 m_mltProducer->optimise();
1084 xmlConsumer.set("terminate_on_pause", 1);
1085 Mlt::Producer prod(m_mltProducer->get_producer());
1086 bool split = m_isSplitView;
1087 if (split) slotSplitView(false);
1088 xmlConsumer.connect(prod);
1089 xmlConsumer.start();
1090 while (!xmlConsumer.is_stopped()) {}
1091 playlist = QString::fromUtf8(xmlConsumer.get("kdenlive_playlist"));
1092 if (split) slotSplitView(true);
1096 bool Render::saveSceneList(QString path, QDomElement kdenliveData)
1100 doc.setContent(sceneList(), false);
1101 if (!kdenliveData.isNull()) {
1102 // add Kdenlive specific tags
1103 QDomNode mlt = doc.elementsByTagName("mlt").at(0);
1104 mlt.appendChild(doc.importNode(kdenliveData, true));
1106 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
1107 kWarning() << "////// ERROR writing to file: " << path;
1110 file.write(doc.toString().toUtf8());
1111 if (file.error() != QFile::NoError) {
1119 void Render::saveZone(KUrl url, QString desc, QPoint zone)
1121 kDebug() << "// SAVING CLIP ZONE, RENDER: " << m_name;
1122 Mlt::Consumer xmlConsumer(*m_mltProfile, ("xml:" + url.path()).toUtf8().constData());
1123 m_mltProducer->optimise();
1124 xmlConsumer.set("terminate_on_pause", 1);
1125 if (m_name == "clip") {
1126 Mlt::Producer *prod = m_mltProducer->cut(zone.x(), zone.y());
1128 list.insert_at(0, prod, 0);
1130 list.set("title", desc.toUtf8().constData());
1131 xmlConsumer.connect(list);
1134 //TODO: not working yet, save zone from timeline
1135 Mlt::Producer *p1 = new Mlt::Producer(m_mltProducer->get_producer());
1136 /* Mlt::Service service(p1->parent().get_service());
1137 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";*/
1139 //Mlt::Producer *prod = p1->cut(zone.x(), zone.y());
1140 //prod->set("title", desc.toUtf8().constData());
1141 xmlConsumer.connect(*p1); //list);
1144 xmlConsumer.start();
1147 double Render::fps() const
1152 int Render::connectPlaylist()
1154 if (!m_mltConsumer) return -1;
1155 //m_mltConsumer->set("refresh", "0");
1156 m_mltConsumer->connect(*m_mltProducer);
1157 m_mltProducer->set_speed(0);
1158 if (m_mltConsumer->start() == -1) {
1159 // ARGH CONSUMER BROKEN!!!!
1160 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."));
1161 delete m_mltConsumer;
1162 m_mltConsumer = NULL;
1165 emit durationChanged(m_mltProducer->get_playtime());
1170 void Render::refreshDisplay()
1173 if (!m_mltProducer) return;
1174 //m_mltConsumer->set("refresh", 0);
1176 //mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1177 /*if (KdenliveSettings::osdtimecode()) {
1178 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
1179 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
1180 m_osdInfo->set("dynamic", "1");
1181 m_mltProducer->attach(*m_osdInfo);
1184 m_mltProducer->detach(*m_osdInfo);
1185 m_osdInfo->set("dynamic", "0");
1190 int Render::volume() const
1192 if (!m_mltConsumer || !m_mltProducer) return -1;
1193 return ((int) 100 * m_mltProducer->get_double("meta.volume"));
1196 void Render::slotSetVolume(int volume)
1198 if (!m_mltConsumer || !m_mltProducer) return;
1199 m_mltProducer->set("meta.volume", (double)volume / 100.0);
1202 m_mltConsumer->set("refresh", 0);
1203 // Attach filter for on screen display of timecode
1204 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1205 mlt_properties_set_double( properties, "meta.volume", volume );
1206 mlt_properties_set_int( properties, "meta.attr.osdvolume", 1);
1207 mlt_properties_set( properties, "meta.attr.osdvolume.markup", i18n("Volume: ") + QString::number(volume * 100));
1209 if (!KdenliveSettings::osdtimecode()) {
1210 m_mltProducer->detach(*m_osdInfo);
1211 mlt_properties_set_int( properties, "meta.attr.timecode", 0);
1212 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1215 //m_osdTimer->setSingleShot(2500);
1218 void Render::slotOsdTimeout()
1220 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1221 mlt_properties_set_int(properties, "meta.attr.osdvolume", 0);
1222 mlt_properties_set(properties, "meta.attr.osdvolume.markup", NULL);
1223 //if (!KdenliveSettings::osdtimecode()) m_mltProducer->detach(*m_osdInfo);
1227 void Render::start()
1229 kDebug() << "----- STARTING MONITOR: " << m_name;
1230 if (m_winid == -1) {
1231 kDebug() << "----- BROKEN MONITOR: " << m_name << ", RESTART";
1234 if (m_mltConsumer && m_mltConsumer->is_stopped()) {
1235 kDebug() << "----- MONITOR: " << m_name << " WAS STOPPED";
1236 if (m_mltConsumer->start() == -1) {
1237 //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."));
1238 kDebug(QtWarningMsg) << "/ / / / CANNOT START MONITOR";
1240 kDebug() << "----- MONITOR: " << m_name << " REFRESH";
1241 m_isBlocked = false;
1245 m_isBlocked = false;
1250 if (m_mltProducer == NULL) return;
1251 if (m_mltConsumer && !m_mltConsumer->is_stopped()) {
1252 kDebug() << "///////////// RENDER STOPPED: " << m_name;
1254 //m_mltConsumer->set("refresh", 0);
1255 m_mltConsumer->stop();
1256 // delete m_mltConsumer;
1257 // m_mltConsumer = NULL;
1259 kDebug() << "///////////// RENDER STOP2-------";
1262 if (m_mltProducer) {
1263 if (m_isZoneMode) resetZoneMode();
1264 m_mltProducer->set_speed(0.0);
1265 //m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1266 //kDebug() << m_mltProducer->get_length();
1268 kDebug() << "///////////// RENDER STOP3-------";
1271 void Render::stop(const GenTime & startTime)
1274 kDebug() << "///////////// RENDER STOP-------2";
1275 if (m_mltProducer) {
1276 if (m_isZoneMode) resetZoneMode();
1277 m_mltProducer->set_speed(0.0);
1278 m_mltProducer->seek((int) startTime.frames(m_fps));
1280 m_mltConsumer->purge();
1283 void Render::pause()
1285 if (!m_mltProducer || !m_mltConsumer)
1287 if (m_mltProducer->get_speed() == 0.0) return;
1288 if (m_isZoneMode) resetZoneMode();
1290 m_mltConsumer->set("refresh", 0);
1291 m_mltProducer->set_speed(0.0);
1293 The 2 lines below create a flicker loop
1294 emit rendererPosition(m_framePosition);
1295 m_mltProducer->seek(m_framePosition);*/
1296 m_mltConsumer->purge();
1299 void Render::switchPlay()
1301 if (!m_mltProducer || !m_mltConsumer)
1303 if (m_isZoneMode) resetZoneMode();
1304 if (m_mltProducer->get_speed() == 0.0) {
1305 m_isBlocked = false;
1306 if (m_name == "clip" && m_framePosition == (int) m_mltProducer->get_out()) m_mltProducer->seek(0);
1307 m_mltProducer->set_speed(1.0);
1308 m_mltConsumer->set("refresh", 1);
1311 m_mltConsumer->set("refresh", 0);
1312 m_mltProducer->set_speed(0.0);
1313 //emit rendererPosition(m_framePosition);
1314 m_mltProducer->seek(m_framePosition);
1315 m_mltConsumer->purge();
1316 //kDebug()<<" ********* RENDER PAUSE: "<<m_mltProducer->get_speed();
1317 //m_mltConsumer->set("refresh", 0);
1318 /*mlt_position position = mlt_producer_position( m_mltProducer->get_producer() );
1319 m_mltProducer->set_speed(0);
1320 m_mltProducer->seek( position );
1321 //m_mltProducer->seek((int) m_framePosition);
1322 m_isBlocked = false;*/
1324 /*if (speed == 0.0) {
1325 m_mltProducer->seek((int) m_framePosition + 1);
1326 m_mltConsumer->purge();
1331 void Render::play(double speed)
1335 // if (speed == 0.0) m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1336 m_isBlocked = false;
1337 m_mltProducer->set_speed(speed);
1338 /*if (speed == 0.0) {
1339 m_mltProducer->seek((int) m_framePosition + 1);
1340 m_mltConsumer->purge();
1345 void Render::play(const GenTime & startTime)
1347 if (!m_mltProducer || !m_mltConsumer)
1349 m_isBlocked = false;
1350 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1351 m_mltProducer->set_speed(1.0);
1352 m_mltConsumer->set("refresh", 1);
1355 void Render::loopZone(const GenTime & startTime, const GenTime & stopTime)
1357 if (!m_mltProducer || !m_mltConsumer)
1359 //m_mltProducer->set("eof", "loop");
1360 m_isLoopMode = true;
1361 m_loopStart = startTime;
1362 playZone(startTime, stopTime);
1365 void Render::playZone(const GenTime & startTime, const GenTime & stopTime)
1367 if (!m_mltProducer || !m_mltConsumer)
1369 m_isBlocked = false;
1370 if (!m_isZoneMode) m_originalOut = m_mltProducer->get_playtime() - 1;
1371 m_mltProducer->set("out", (int)(stopTime.frames(m_fps)));
1372 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1373 m_mltProducer->set_speed(1.0);
1374 m_mltConsumer->set("refresh", 1);
1375 m_isZoneMode = true;
1378 void Render::resetZoneMode()
1380 if (!m_isZoneMode && !m_isLoopMode) return;
1381 m_mltProducer->set("out", m_originalOut);
1382 //m_mltProducer->set("eof", "pause");
1383 m_isZoneMode = false;
1384 m_isLoopMode = false;
1387 void Render::seekToFrame(int pos)
1389 //kDebug()<<" ********* RENDER SEEK TO POS";
1392 m_isBlocked = false;
1394 m_mltProducer->seek(pos);
1398 void Render::seekToFrameDiff(int diff)
1400 //kDebug()<<" ********* RENDER SEEK TO POS";
1403 m_isBlocked = false;
1405 m_mltProducer->seek(m_mltProducer->position() + diff);
1409 void Render::doRefresh()
1411 // Use a Timer so that we don't refresh too much
1412 if (!m_isBlocked && m_mltConsumer) m_mltConsumer->set("refresh", 1);
1415 void Render::refresh()
1417 if (!m_mltProducer || m_isBlocked)
1419 if (m_mltConsumer) {
1420 m_mltConsumer->set("refresh", 1);
1424 void Render::setDropFrames(bool show)
1426 if (m_mltConsumer) {
1427 int dropFrames = KdenliveSettings::mltthreads();
1428 if (show == false) dropFrames = -dropFrames;
1429 m_mltConsumer->stop();
1431 m_mltConsumer->set("real_time", dropFrames);
1433 m_mltConsumer->set("play.real_time", dropFrames);
1435 if (m_mltConsumer->start() == -1) {
1436 kDebug(QtWarningMsg) << "ERROR, Cannot start monitor";
1442 double Render::playSpeed()
1444 if (m_mltProducer) return m_mltProducer->get_speed();
1448 GenTime Render::seekPosition() const
1450 if (m_mltProducer) return GenTime((int) m_mltProducer->position(), m_fps);
1451 else return GenTime();
1454 int Render::seekFramePosition() const
1456 if (m_mltProducer) return (int) m_mltProducer->position();
1460 const QString & Render::rendererName() const
1465 void Render::emitFrameUpdated(Mlt::Frame& frame)
1467 mlt_image_format format = mlt_image_rgb24a;
1470 const uchar* image = frame.get_image(format, width, height);
1471 QImage qimage(width, height, QImage::Format_ARGB32);
1472 memcpy(qimage.bits(), image, width * height * 4);
1473 emit frameUpdated(qimage.rgbSwapped());
1476 void Render::emitFrameNumber(double position)
1478 m_framePosition = position;
1479 emit rendererPosition((int) position);
1482 void Render::emitConsumerStopped()
1484 // This is used to know when the playing stopped
1485 if (m_mltProducer) {
1486 double pos = m_mltProducer->position();
1487 if (m_isLoopMode) play(m_loopStart);
1488 else if (m_isZoneMode) resetZoneMode();
1489 emit rendererStopped((int) pos);
1493 void Render::exportFileToFirewire(QString /*srcFileName*/, int /*port*/, GenTime /*startTime*/, GenTime /*endTime*/)
1495 KMessageBox::sorry(0, i18n("Firewire is not enabled on your system.\n Please install Libiec61883 and recompile Kdenlive"));
1498 void Render::exportCurrentFrame(KUrl url, bool /*notify*/)
1500 if (!m_mltProducer) {
1501 KMessageBox::sorry(qApp->activeWindow(), i18n("There is no clip, cannot extract frame."));
1505 //int height = 1080;//KdenliveSettings::defaultheight();
1506 //int width = 1940; //KdenliveSettings::displaywidth();
1508 QPixmap pix; // = KThumb::getFrame(m_mltProducer, -1, width, height);
1510 QPixmap pix(width, height);
1511 Mlt::Filter m_convert(*m_mltProfile, "avcolour_space");
1512 m_convert.set("forced", mlt_image_rgb24a);
1513 m_mltProducer->attach(m_convert);
1514 Mlt::Frame * frame = m_mltProducer->get_frame();
1515 m_mltProducer->detach(m_convert);
1517 pix = frameThumbnail(frame, width, height);
1520 pix.save(url.path(), "PNG");
1521 //if (notify) QApplication::postEvent(qApp->activeWindow(), new UrlEvent(url, 10003));
1525 void Render::showFrame(Mlt::Frame& frame)
1527 m_framePosition = qMax(frame.get_int("_position"), 0);
1528 emit rendererPosition((int) m_framePosition);
1529 mlt_image_format format = mlt_image_rgb24a;
1532 const uchar* image = frame.get_image(format, width, height);
1533 QImage qimage(width, height, QImage::Format_ARGB32_Premultiplied);
1534 memcpy(qimage.scanLine(0), image, width * height * 4);
1535 emit showImageSignal(qimage);
1536 if (analyseAudio) showAudio(frame);
1537 if (sendFrameForAnalysis && frame.get_frame()->convert_image) {
1538 emit frameUpdated(qimage.rgbSwapped());
1542 void Render::showAudio(Mlt::Frame& frame)
1544 if (!frame.is_valid() || frame.get_int("test_audio") != 0) {
1547 mlt_audio_format audio_format = mlt_audio_s16;
1549 int num_channels = 0;
1551 int16_t* data = (int16_t*)frame.get_audio(audio_format, freq, num_channels, samples);
1557 // Data format: [ c00 c10 c01 c11 c02 c12 c03 c13 ... c0{samples-1} c1{samples-1} for 2 channels.
1558 // So the vector is of size samples*channels.
1559 QVector<int16_t> sampleVector(samples*num_channels);
1560 memcpy(sampleVector.data(), data, samples*num_channels*sizeof(int16_t));
1563 emit audioSamplesSignal(sampleVector, freq, num_channels, samples);
1568 * MLT playlist direct manipulation.
1571 void Render::mltCheckLength(Mlt::Tractor *tractor)
1573 //kDebug()<<"checking track length: "<<track<<"..........";
1575 int trackNb = tractor->count();
1579 Mlt::Producer trackProducer(tractor->track(0));
1580 duration = trackProducer.get_playtime() - 1;
1581 m_mltProducer->set("out", duration);
1582 emit durationChanged(duration);
1585 while (trackNb > 1) {
1586 Mlt::Producer trackProducer(tractor->track(trackNb - 1));
1587 trackDuration = trackProducer.get_playtime() - 1;
1588 // kDebug() << " / / /DURATON FOR TRACK " << trackNb - 1 << " = " << trackDuration;
1589 if (trackDuration > duration) duration = trackDuration;
1593 Mlt::Producer blackTrackProducer(tractor->track(0));
1595 if (blackTrackProducer.get_playtime() - 1 != duration) {
1596 Mlt::Playlist blackTrackPlaylist((mlt_playlist) blackTrackProducer.get_service());
1597 Mlt::Producer *blackclip = blackTrackPlaylist.get_clip(0);
1598 if (blackclip && blackclip->is_blank()) {
1603 if (blackclip == NULL || blackTrackPlaylist.count() != 1) {
1604 blackTrackPlaylist.clear();
1605 m_blackClip->set("length", duration + 1);
1606 m_blackClip->set("out", duration);
1607 blackclip = m_blackClip->cut(0, duration);
1608 blackTrackPlaylist.insert_at(0, blackclip, 1);
1610 if (duration > blackclip->parent().get_length()) {
1611 blackclip->parent().set("length", duration + 1);
1612 blackclip->parent().set("out", duration);
1613 blackclip->set("length", duration + 1);
1615 blackTrackPlaylist.resize_clip(0, 0, duration);
1619 m_mltProducer->set("out", duration);
1620 emit durationChanged(duration);
1624 Mlt::Producer *Render::checkSlowMotionProducer(Mlt::Producer *prod, QDomElement element)
1626 if (element.attribute("speed", "1.0").toDouble() == 1.0 && element.attribute("strobe", "1").toInt() == 1) return prod;
1628 // We want a slowmotion producer
1629 double speed = element.attribute("speed", "1.0").toDouble();
1630 int strobe = element.attribute("strobe", "1").toInt();
1631 QString url = QString::fromUtf8(prod->get("resource"));
1632 url.append('?' + QString::number(speed));
1633 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
1634 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1635 if (!slowprod || slowprod->get_producer() == NULL) {
1636 slowprod = new Mlt::Producer(*m_mltProfile, "framebuffer", url.toUtf8().constData());
1637 if (strobe > 1) slowprod->set("strobe", strobe);
1638 QString id = prod->get("id");
1639 if (id.contains('_')) id = id.section('_', 0, 0);
1640 QString producerid = "slowmotion:" + id + ':' + QString::number(speed);
1641 if (strobe > 1) producerid.append(':' + QString::number(strobe));
1642 slowprod->set("id", producerid.toUtf8().constData());
1643 m_slowmotionProducers.insert(url, slowprod);
1648 int Render::mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod, bool overwrite, bool push)
1650 if (m_mltProducer == NULL) {
1651 kDebug() << "PLAYLIST NOT INITIALISED //////";
1655 kDebug() << "Cannot insert clip without producer //////";
1658 Mlt::Producer parentProd(m_mltProducer->parent());
1659 if (parentProd.get_producer() == NULL) {
1660 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1664 Mlt::Service service(parentProd.get_service());
1665 if (service.type() != tractor_type) {
1666 kWarning() << "// TRACTOR PROBLEM";
1669 Mlt::Tractor tractor(service);
1670 if (info.track > tractor.count() - 1) {
1671 kDebug() << "ERROR TRYING TO INSERT CLIP ON TRACK " << info.track << ", at POS: " << info.startPos.frames(25);
1674 mlt_service_lock(service.get_service());
1675 Mlt::Producer trackProducer(tractor.track(info.track));
1676 int trackDuration = trackProducer.get_playtime() - 1;
1677 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1678 //kDebug()<<"/// INSERT cLIP: "<<info.cropStart.frames(m_fps)<<", "<<info.startPos.frames(m_fps)<<"-"<<info.endPos.frames(m_fps);
1679 prod = checkSlowMotionProducer(prod, element);
1680 if (prod == NULL || !prod->is_valid()) {
1681 mlt_service_unlock(service.get_service());
1685 int cutPos = (int) info.cropStart.frames(m_fps);
1686 if (cutPos < 0) cutPos = 0;
1687 int insertPos = (int) info.startPos.frames(m_fps);
1688 int cutDuration = (int)(info.endPos - info.startPos).frames(m_fps) - 1;
1689 Mlt::Producer *clip = prod->cut(cutPos, cutDuration + cutPos);
1690 if (overwrite && (insertPos < trackDuration)) {
1691 // Replace zone with blanks
1692 //trackPlaylist.split_at(insertPos, true);
1693 trackPlaylist.remove_region(insertPos, cutDuration + 1);
1694 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1695 trackPlaylist.insert_blank(clipIndex, cutDuration);
1697 trackPlaylist.split_at(insertPos, true);
1698 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1699 trackPlaylist.insert_blank(clipIndex, cutDuration);
1701 int newIndex = trackPlaylist.insert_at(insertPos, clip, 1);
1703 /*if (QString(prod->get("transparency")).toInt() == 1)
1704 mltAddClipTransparency(info, info.track - 1, QString(prod->get("id")).toInt());*/
1706 if (info.track != 0 && (newIndex + 1 == trackPlaylist.count())) mltCheckLength(&tractor);
1707 mlt_service_unlock(service.get_service());
1708 /*tractor.multitrack()->refresh();
1709 tractor.refresh();*/
1714 void Render::mltCutClip(int track, GenTime position)
1718 Mlt::Service service(m_mltProducer->parent().get_service());
1719 if (service.type() != tractor_type) {
1720 kWarning() << "// TRACTOR PROBLEM";
1724 Mlt::Tractor tractor(service);
1725 Mlt::Producer trackProducer(tractor.track(track));
1726 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1729 /* // Display playlist info
1730 kDebug()<<"//////////// BEFORE";
1731 for (int i = 0; i < trackPlaylist.count(); i++) {
1732 int blankStart = trackPlaylist.clip_start(i);
1733 int blankDuration = trackPlaylist.clip_length(i) - 1;
1735 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1736 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1739 int cutPos = (int) position.frames(m_fps);
1741 int clipIndex = trackPlaylist.get_clip_index_at(cutPos);
1742 if (trackPlaylist.is_blank(clipIndex)) {
1743 kDebug() << "// WARNING, TRYING TO CUT A BLANK";
1744 m_isBlocked = false;
1747 mlt_service_lock(service.get_service());
1748 int clipStart = trackPlaylist.clip_start(clipIndex);
1749 trackPlaylist.split(clipIndex, cutPos - clipStart - 1);
1750 mlt_service_unlock(service.get_service());
1752 // duplicate effects
1753 Mlt::Producer *original = trackPlaylist.get_clip_at(clipStart);
1754 Mlt::Producer *clip = trackPlaylist.get_clip_at(cutPos);
1756 if (original == NULL || clip == NULL) {
1757 kDebug() << "// ERROR GRABBING CLIP AFTER SPLIT";
1759 Mlt::Service clipService(original->get_service());
1760 Mlt::Service dupService(clip->get_service());
1764 Mlt::Filter *filter = clipService.filter(ct);
1766 // Only duplicate Kdenlive filters, and skip the fade in effects
1767 if (filter->is_valid() && strcmp(filter->get("kdenlive_id"), "") && strcmp(filter->get("kdenlive_id"), "fadein") && strcmp(filter->get("kdenlive_id"), "fade_from_black")) {
1768 // looks like there is no easy way to duplicate a filter,
1769 // so we will create a new one and duplicate its properties
1770 Mlt::Filter *dup = new Mlt::Filter(*m_mltProfile, filter->get("mlt_service"));
1771 if (dup && dup->is_valid()) {
1772 Mlt::Properties entries(filter->get_properties());
1773 for (int i = 0; i < entries.count(); i++) {
1774 dup->set(entries.get_name(i), entries.get(i));
1776 dupService.attach(*dup);
1780 filter = clipService.filter(ct);
1783 /* // Display playlist info
1784 kDebug()<<"//////////// AFTER";
1785 for (int i = 0; i < trackPlaylist.count(); i++) {
1786 int blankStart = trackPlaylist.clip_start(i);
1787 int blankDuration = trackPlaylist.clip_length(i) - 1;
1789 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1790 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1793 m_isBlocked = false;
1796 bool Render::mltUpdateClip(ItemInfo info, QDomElement element, Mlt::Producer *prod)
1800 kDebug() << "Cannot update clip with null producer //////";
1803 Mlt::Service service(m_mltProducer->parent().get_service());
1804 if (service.type() != tractor_type) {
1805 kWarning() << "// TRACTOR PROBLEM";
1808 Mlt::Tractor tractor(service);
1809 Mlt::Producer trackProducer(tractor.track(info.track));
1810 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1811 int startPos = info.startPos.frames(m_fps);
1812 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
1813 if (trackPlaylist.is_blank(clipIndex)) {
1814 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << startPos;
1817 mlt_service_lock(service.get_service());
1818 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
1820 QList <Mlt::Filter *> filtersList;
1821 Mlt::Service sourceService(clip->get_service());
1823 Mlt::Filter *filter = sourceService.filter(ct);
1825 if (filter->get_int("kdenlive_ix") != 0) {
1826 filtersList.append(filter);
1829 filter = sourceService.filter(ct);
1832 trackPlaylist.replace_with_blank(clipIndex);
1834 //if (!mltRemoveClip(info.track, info.startPos)) return false;
1835 prod = checkSlowMotionProducer(prod, element);
1836 if (prod == NULL || !prod->is_valid()) {
1837 mlt_service_unlock(service.get_service());
1841 Mlt::Producer *clip2 = prod->cut(info.cropStart.frames(m_fps), (info.cropDuration + info.cropStart).frames(m_fps) - 1);
1842 trackPlaylist.insert_at(info.startPos.frames(m_fps), clip2, 1);
1846 //if (mltInsertClip(info, element, prod) == -1) return false;
1847 if (!filtersList.isEmpty()) {
1848 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1849 Mlt::Producer *destclip = trackPlaylist.get_clip(clipIndex);
1850 Mlt::Service destService(destclip->get_service());
1852 for (int i = 0; i < filtersList.count(); i++)
1853 destService.attach(*(filtersList.at(i)));
1855 mlt_service_unlock(service.get_service());
1860 bool Render::mltRemoveClip(int track, GenTime position)
1862 Mlt::Service service(m_mltProducer->parent().get_service());
1863 if (service.type() != tractor_type) {
1864 kWarning() << "// TRACTOR PROBLEM";
1868 Mlt::Tractor tractor(service);
1869 mlt_service_lock(service.get_service());
1870 Mlt::Producer trackProducer(tractor.track(track));
1871 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1872 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
1874 // Display playlist info
1875 //kDebug() << "//// BEFORE -( " << position.frames(m_fps) << " )-------------------------------";
1876 /*for (int i = 0; i < trackPlaylist.count(); i++) {
1877 int blankStart = trackPlaylist.clip_start(i);
1878 int blankDuration = trackPlaylist.clip_length(i) - 1;
1880 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1881 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1883 if (trackPlaylist.is_blank(clipIndex)) {
1884 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << position.frames(25);
1885 mlt_service_unlock(service.get_service());
1888 //kDebug()<<"//// Deleting at: "<< (int) position.frames(m_fps) <<" --------------------------------------";
1890 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
1891 if (clip) delete clip;
1892 trackPlaylist.consolidate_blanks(0);
1893 /*if (QString(clip.parent().get("transparency")).toInt() == 1)
1894 mltDeleteTransparency((int) position.frames(m_fps), track, QString(clip.parent().get("id")).toInt());*/
1896 /* // Display playlist info
1897 kDebug()<<"//// AFTER";
1898 for (int i = 0; i < trackPlaylist.count(); i++) {
1899 int blankStart = trackPlaylist.clip_start(i);
1900 int blankDuration = trackPlaylist.clip_length(i) - 1;
1902 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1903 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1905 mlt_service_unlock(service.get_service());
1906 if (track != 0 && trackPlaylist.count() <= clipIndex) mltCheckLength(&tractor);
1907 m_isBlocked = false;
1911 int Render::mltGetSpaceLength(const GenTime pos, int track, bool fromBlankStart)
1913 if (!m_mltProducer) {
1914 kDebug() << "PLAYLIST NOT INITIALISED //////";
1917 Mlt::Producer parentProd(m_mltProducer->parent());
1918 if (parentProd.get_producer() == NULL) {
1919 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1923 Mlt::Service service(parentProd.get_service());
1924 Mlt::Tractor tractor(service);
1925 int insertPos = pos.frames(m_fps);
1927 Mlt::Producer trackProducer(tractor.track(track));
1928 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1929 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1930 if (clipIndex == trackPlaylist.count()) {
1931 // We are after the end of the playlist
1934 if (!trackPlaylist.is_blank(clipIndex)) return 0;
1935 if (fromBlankStart) return trackPlaylist.clip_length(clipIndex);
1936 return trackPlaylist.clip_length(clipIndex) + trackPlaylist.clip_start(clipIndex) - insertPos;
1939 int Render::mltTrackDuration(int track)
1941 if (!m_mltProducer) {
1942 kDebug() << "PLAYLIST NOT INITIALISED //////";
1945 Mlt::Producer parentProd(m_mltProducer->parent());
1946 if (parentProd.get_producer() == NULL) {
1947 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1951 Mlt::Service service(parentProd.get_service());
1952 Mlt::Tractor tractor(service);
1954 Mlt::Producer trackProducer(tractor.track(track));
1955 return trackProducer.get_playtime() - 1;
1958 void Render::mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime duration, const GenTime timeOffset)
1960 if (!m_mltProducer) {
1961 kDebug() << "PLAYLIST NOT INITIALISED //////";
1964 Mlt::Producer parentProd(m_mltProducer->parent());
1965 if (parentProd.get_producer() == NULL) {
1966 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1969 //kDebug()<<"// CLP STRT LST: "<<trackClipStartList;
1970 //kDebug()<<"// TRA STRT LST: "<<trackTransitionStartList;
1972 Mlt::Service service(parentProd.get_service());
1973 Mlt::Tractor tractor(service);
1974 mlt_service_lock(service.get_service());
1975 int diff = duration.frames(m_fps);
1976 int offset = timeOffset.frames(m_fps);
1980 // insert space in one track only
1981 Mlt::Producer trackProducer(tractor.track(track));
1982 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1983 insertPos = trackClipStartList.value(track);
1984 if (insertPos != -1) {
1985 insertPos += offset;
1986 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1988 trackPlaylist.insert_blank(clipIndex, diff - 1);
1990 if (!trackPlaylist.is_blank(clipIndex)) clipIndex --;
1991 if (!trackPlaylist.is_blank(clipIndex)) {
1992 kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
1994 int position = trackPlaylist.clip_start(clipIndex);
1995 int blankDuration = trackPlaylist.clip_length(clipIndex);
1996 if (blankDuration + diff == 0) {
1997 trackPlaylist.remove(clipIndex);
1998 } else trackPlaylist.remove_region(position, -diff);
2000 trackPlaylist.consolidate_blanks(0);
2002 // now move transitions
2003 mlt_service serv = m_mltProducer->parent().get_service();
2004 mlt_service nextservice = mlt_service_get_producer(serv);
2005 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2006 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2007 QString resource = mlt_properties_get(properties, "mlt_service");
2009 while (mlt_type == "transition") {
2010 mlt_transition tr = (mlt_transition) nextservice;
2011 int currentTrack = mlt_transition_get_b_track(tr);
2012 int currentIn = (int) mlt_transition_get_in(tr);
2013 int currentOut = (int) mlt_transition_get_out(tr);
2014 insertPos = trackTransitionStartList.value(track);
2015 if (insertPos != -1) {
2016 insertPos += offset;
2017 if (track == currentTrack && currentOut > insertPos && resource != "mix") {
2018 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
2021 nextservice = mlt_service_producer(nextservice);
2022 if (nextservice == NULL) break;
2023 properties = MLT_SERVICE_PROPERTIES(nextservice);
2024 mlt_type = mlt_properties_get(properties, "mlt_type");
2025 resource = mlt_properties_get(properties, "mlt_service");
2028 for (int trackNb = tractor.count() - 1; trackNb >= 1; --trackNb) {
2029 Mlt::Producer trackProducer(tractor.track(trackNb));
2030 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2032 //int clipNb = trackPlaylist.count();
2033 insertPos = trackClipStartList.value(trackNb);
2034 if (insertPos != -1) {
2035 insertPos += offset;
2037 /* kDebug()<<"-------------\nTRACK "<<trackNb<<" HAS "<<clipNb<<" CLPIS";
2038 kDebug() << "INSERT SPACE AT: "<<insertPos<<", DIFF: "<<diff<<", TK: "<<trackNb;
2039 for (int i = 0; i < clipNb; i++) {
2040 kDebug()<<"CLIP "<<i<<", START: "<<trackPlaylist.clip_start(i)<<", END: "<<trackPlaylist.clip_start(i) + trackPlaylist.clip_length(i);
2041 if (trackPlaylist.is_blank(i)) kDebug()<<"++ BLANK ++ ";
2042 kDebug()<<"-------------";
2044 kDebug()<<"END-------------";*/
2047 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
2049 trackPlaylist.insert_blank(clipIndex, diff - 1);
2051 if (!trackPlaylist.is_blank(clipIndex)) {
2054 if (!trackPlaylist.is_blank(clipIndex)) {
2055 kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
2057 int position = trackPlaylist.clip_start(clipIndex);
2058 int blankDuration = trackPlaylist.clip_length(clipIndex);
2059 if (diff + blankDuration == 0) {
2060 trackPlaylist.remove(clipIndex);
2061 } else trackPlaylist.remove_region(position, - diff);
2063 trackPlaylist.consolidate_blanks(0);
2066 // now move transitions
2067 mlt_service serv = m_mltProducer->parent().get_service();
2068 mlt_service nextservice = mlt_service_get_producer(serv);
2069 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2070 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2071 QString resource = mlt_properties_get(properties, "mlt_service");
2073 while (mlt_type == "transition") {
2074 mlt_transition tr = (mlt_transition) nextservice;
2075 int currentIn = (int) mlt_transition_get_in(tr);
2076 int currentOut = (int) mlt_transition_get_out(tr);
2077 int currentTrack = mlt_transition_get_b_track(tr);
2078 insertPos = trackTransitionStartList.value(currentTrack);
2079 if (insertPos != -1) {
2080 insertPos += offset;
2081 if (currentOut > insertPos && resource != "mix") {
2082 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
2085 nextservice = mlt_service_producer(nextservice);
2086 if (nextservice == NULL) break;
2087 properties = MLT_SERVICE_PROPERTIES(nextservice);
2088 mlt_type = mlt_properties_get(properties, "mlt_type");
2089 resource = mlt_properties_get(properties, "mlt_service");
2092 mlt_service_unlock(service.get_service());
2093 mltCheckLength(&tractor);
2094 m_mltConsumer->set("refresh", 1);
2098 void Render::mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest)
2100 if (source == dest) return;
2101 Mlt::Service sourceService(source->get_service());
2102 Mlt::Service destService(dest->get_service());
2104 // move all effects to the correct producer
2106 Mlt::Filter *filter = sourceService.filter(ct);
2108 if (filter->get_int("kdenlive_ix") != 0) {
2109 sourceService.detach(*filter);
2110 destService.attach(*filter);
2112 filter = sourceService.filter(ct);
2116 int Render::mltChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, double speed, double /*oldspeed*/, int strobe, Mlt::Producer *prod)
2120 Mlt::Service service(m_mltProducer->parent().get_service());
2121 if (service.type() != tractor_type) {
2122 kWarning() << "// TRACTOR PROBLEM";
2125 //kDebug() << "Changing clip speed, set in and out: " << info.cropStart.frames(m_fps) << " to " << (info.endPos - info.startPos).frames(m_fps) - 1;
2126 Mlt::Tractor tractor(service);
2127 Mlt::Producer trackProducer(tractor.track(info.track));
2128 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2129 int startPos = info.startPos.frames(m_fps);
2130 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
2131 int clipLength = trackPlaylist.clip_length(clipIndex);
2133 Mlt::Producer *original = trackPlaylist.get_clip(clipIndex);
2134 if (original == NULL) {
2137 if (!original->is_valid() || original->is_blank()) {
2142 Mlt::Producer clipparent = original->parent();
2143 if (!clipparent.is_valid() || clipparent.is_blank()) {
2149 QString serv = clipparent.get("mlt_service");
2150 QString id = clipparent.get("id");
2151 if (speed <= 0 && speed > -1) speed = 1.0;
2152 //kDebug() << "CLIP SERVICE: " << serv;
2153 if ((serv == "avformat" || serv == "avformat-novalidate") && (speed != 1.0 || strobe > 1)) {
2154 mlt_service_lock(service.get_service());
2155 QString url = QString::fromUtf8(clipparent.get("resource"));
2156 url.append('?' + QString::number(speed));
2157 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
2158 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
2159 if (!slowprod || slowprod->get_producer() == NULL) {
2160 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
2161 if (strobe > 1) slowprod->set("strobe", strobe);
2162 QString producerid = "slowmotion:" + id + ':' + QString::number(speed);
2163 if (strobe > 1) producerid.append(':' + QString::number(strobe));
2164 slowprod->set("id", producerid.toUtf8().constData());
2165 // copy producer props
2166 double ar = original->parent().get_double("force_aspect_ratio");
2167 if (ar != 0.0) slowprod->set("force_aspect_ratio", ar);
2168 double fps = original->parent().get_double("force_fps");
2169 if (fps != 0.0) slowprod->set("force_fps", fps);
2170 int threads = original->parent().get_int("threads");
2171 if (threads != 0) slowprod->set("threads", threads);
2172 if (original->parent().get("force_progressive"))
2173 slowprod->set("force_progressive", original->parent().get_int("force_progressive"));
2174 if (original->parent().get("force_tff"))
2175 slowprod->set("force_tff", original->parent().get_int("force_tff"));
2176 int ix = original->parent().get_int("video_index");
2177 if (ix != 0) slowprod->set("video_index", ix);
2178 int colorspace = original->parent().get_int("force_colorspace");
2179 if (colorspace != 0) slowprod->set("force_colorspace", colorspace);
2180 int full_luma = original->parent().get_int("set.force_full_luma");
2181 if (full_luma != 0) slowprod->set("set.force_full_luma", full_luma);
2182 m_slowmotionProducers.insert(url, slowprod);
2184 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2185 trackPlaylist.consolidate_blanks(0);
2187 // Check that the blank space is long enough for our new duration
2188 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2189 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2191 if (clipIndex + 1 < trackPlaylist.count() && (startPos + clipLength / speed > blankEnd)) {
2192 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2193 cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)(info.cropStart.frames(m_fps) / speed + maxLength.frames(m_fps) - 1));
2194 } else cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)((info.cropStart.frames(m_fps) + clipLength) / speed - 1));
2196 // move all effects to the correct producer
2197 mltPasteEffects(clip, cut);
2198 trackPlaylist.insert_at(startPos, cut, 1);
2201 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2202 newLength = trackPlaylist.clip_length(clipIndex);
2203 mlt_service_unlock(service.get_service());
2204 } else if (speed == 1.0 && strobe < 2) {
2205 mlt_service_lock(service.get_service());
2207 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2208 trackPlaylist.consolidate_blanks(0);
2210 // Check that the blank space is long enough for our new duration
2211 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2212 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2215 int originalStart = (int)(speedIndependantInfo.cropStart.frames(m_fps));
2216 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + speedIndependantInfo.cropDuration).frames(m_fps) > blankEnd) {
2217 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2218 cut = prod->cut(originalStart, (int)(originalStart + maxLength.frames(m_fps) - 1));
2219 } else cut = prod->cut(originalStart, (int)(originalStart + speedIndependantInfo.cropDuration.frames(m_fps)) - 1);
2221 // move all effects to the correct producer
2222 mltPasteEffects(clip, cut);
2224 trackPlaylist.insert_at(startPos, cut, 1);
2227 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2228 newLength = trackPlaylist.clip_length(clipIndex);
2229 mlt_service_unlock(service.get_service());
2231 } else if (serv == "framebuffer") {
2232 mlt_service_lock(service.get_service());
2233 QString url = QString::fromUtf8(clipparent.get("resource"));
2234 url = url.section('?', 0, 0);
2235 url.append('?' + QString::number(speed));
2236 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
2237 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
2238 if (!slowprod || slowprod->get_producer() == NULL) {
2239 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
2240 slowprod->set("strobe", strobe);
2241 QString producerid = "slowmotion:" + id.section(':', 1, 1) + ':' + QString::number(speed);
2242 if (strobe > 1) producerid.append(':' + QString::number(strobe));
2243 slowprod->set("id", producerid.toUtf8().constData());
2244 // copy producer props
2245 double ar = original->parent().get_double("force_aspect_ratio");
2246 if (ar != 0.0) slowprod->set("force_aspect_ratio", ar);
2247 double fps = original->parent().get_double("force_fps");
2248 if (fps != 0.0) slowprod->set("force_fps", fps);
2249 if (original->parent().get("force_progressive"))
2250 slowprod->set("force_progressive", original->parent().get_int("force_progressive"));
2251 if (original->parent().get("force_tff"))
2252 slowprod->set("force_tff", original->parent().get_int("force_tff"));
2253 int threads = original->parent().get_int("threads");
2254 if (threads != 0) slowprod->set("threads", threads);
2255 int ix = original->parent().get_int("video_index");
2256 if (ix != 0) slowprod->set("video_index", ix);
2257 int colorspace = original->parent().get_int("force_colorspace");
2258 if (colorspace != 0) slowprod->set("force_colorspace", colorspace);
2259 int full_luma = original->parent().get_int("set.force_full_luma");
2260 if (full_luma != 0) slowprod->set("set.force_full_luma", full_luma);
2261 m_slowmotionProducers.insert(url, slowprod);
2263 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2264 trackPlaylist.consolidate_blanks(0);
2266 GenTime duration = speedIndependantInfo.cropDuration / speed;
2267 int originalStart = (int)(speedIndependantInfo.cropStart.frames(m_fps) / speed);
2269 // Check that the blank space is long enough for our new duration
2270 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2271 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2274 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + duration).frames(m_fps) > blankEnd) {
2275 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2276 cut = slowprod->cut(originalStart, (int)(originalStart + maxLength.frames(m_fps) - 1));
2277 } else cut = slowprod->cut(originalStart, (int)(originalStart + duration.frames(m_fps)) - 1);
2279 // move all effects to the correct producer
2280 mltPasteEffects(clip, cut);
2282 trackPlaylist.insert_at(startPos, cut, 1);
2285 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2286 newLength = trackPlaylist.clip_length(clipIndex);
2288 mlt_service_unlock(service.get_service());
2291 if (clipIndex + 1 == trackPlaylist.count()) mltCheckLength(&tractor);
2292 m_isBlocked = false;
2296 bool Render::mltRemoveTrackEffect(int track, QString index, bool updateIndex)
2298 Mlt::Service service(m_mltProducer->parent().get_service());
2299 bool success = false;
2300 Mlt::Tractor tractor(service);
2301 Mlt::Producer trackProducer(tractor.track(track));
2302 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2303 Mlt::Service clipService(trackPlaylist.get_service());
2306 mlt_service_lock(service.get_service());
2308 Mlt::Filter *filter = clipService.filter(ct);
2310 if ((index == "-1" && strcmp(filter->get("kdenlive_id"), "")) || filter->get_int("kdenlive_ix") == index.toInt()) {
2311 if (clipService.detach(*filter) == 0) success = true;
2312 } else if (updateIndex) {
2313 // Adjust the other effects index
2314 if (filter->get_int("kdenlive_ix") > index.toInt()) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2317 filter = clipService.filter(ct);
2319 m_isBlocked = false;
2320 mlt_service_unlock(service.get_service());
2325 bool Render::mltRemoveEffect(int track, GenTime position, QString index, bool updateIndex, bool doRefresh)
2327 if (position < GenTime()) {
2328 // Remove track effect
2329 return mltRemoveTrackEffect(track, index, updateIndex);
2331 Mlt::Service service(m_mltProducer->parent().get_service());
2332 bool success = false;
2333 Mlt::Tractor tractor(service);
2334 Mlt::Producer trackProducer(tractor.track(track));
2335 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2337 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2338 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2340 kDebug() << " / / / CANNOT FIND CLIP TO REMOVE EFFECT";
2344 Mlt::Service clipService(clip->get_service());
2345 int duration = clip->get_playtime();
2347 // Check if clip is visible in monitor
2348 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2349 if (diff < 0 || diff > duration) doRefresh = false;
2353 // if (tag.startsWith("ladspa")) tag = "ladspa";
2355 mlt_service_lock(service.get_service());
2357 Mlt::Filter *filter = clipService.filter(ct);
2359 if ((index == "-1" && strcmp(filter->get("kdenlive_id"), "")) || filter->get_int("kdenlive_ix") == index.toInt()) {// && filter->get("kdenlive_id") == id) {
2360 if (clipService.detach(*filter) == 0) success = true;
2361 //kDebug()<<"Deleted filter id:"<<filter->get("kdenlive_id")<<", ix:"<<filter->get("kdenlive_ix")<<", SERVICE:"<<filter->get("mlt_service");
2362 } else if (updateIndex) {
2363 // Adjust the other effects index
2364 if (filter->get_int("kdenlive_ix") > index.toInt()) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2367 filter = clipService.filter(ct);
2369 m_isBlocked = false;
2370 mlt_service_unlock(service.get_service());
2371 if (doRefresh) refresh();
2375 bool Render::mltAddTrackEffect(int track, EffectsParameterList params)
2377 Mlt::Service service(m_mltProducer->parent().get_service());
2378 Mlt::Tractor tractor(service);
2379 Mlt::Producer trackProducer(tractor.track(track));
2380 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2381 Mlt::Service trackService(trackProducer.get_service()); //trackPlaylist
2382 return mltAddEffect(trackService, params, trackProducer.get_playtime() - 1, true);
2386 bool Render::mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh)
2389 Mlt::Service service(m_mltProducer->parent().get_service());
2391 Mlt::Tractor tractor(service);
2392 Mlt::Producer trackProducer(tractor.track(track));
2393 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2395 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2396 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2401 Mlt::Service clipService(clip->get_service());
2402 int duration = clip->get_playtime();
2404 // Check if clip is visible in monitor
2405 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2406 if (diff < 0 || diff > duration) doRefresh = false;
2409 return mltAddEffect(clipService, params, duration, doRefresh);
2412 bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int duration, bool doRefresh)
2414 bool updateIndex = false;
2415 const int filter_ix = params.paramValue("kdenlive_ix").toInt();
2416 const QString region = params.paramValue("region");
2419 mlt_service_lock(service.get_service());
2421 Mlt::Filter *filter = service.filter(ct);
2423 if (filter->get_int("kdenlive_ix") == filter_ix) {
2424 // A filter at that position already existed, so we will increase all indexes later
2429 filter = service.filter(ct);
2432 if (params.paramValue("id") == "speed") {
2433 // special case, speed effect is not really inserted, we just update the other effects index (kdenlive_ix)
2435 filter = service.filter(ct);
2437 if (filter->get_int("kdenlive_ix") >= filter_ix) {
2438 if (updateIndex) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") + 1);
2441 filter = service.filter(ct);
2443 m_isBlocked = false;
2444 mlt_service_unlock(service.get_service());
2445 if (doRefresh) refresh();
2450 // temporarily remove all effects after insert point
2451 QList <Mlt::Filter *> filtersList;
2453 filter = service.filter(ct);
2455 if (filter->get_int("kdenlive_ix") >= filter_ix) {
2456 filtersList.append(filter);
2457 service.detach(*filter);
2459 filter = service.filter(ct);
2463 QString tag = params.paramValue("tag");
2464 kDebug() << " / / INSERTING EFFECT: " << tag << ", REGI: " << region;
2465 if (tag.startsWith("ladspa")) tag = "ladspa";
2466 char *filterTag = qstrdup(tag.toUtf8().constData());
2467 char *filterId = qstrdup(params.paramValue("id").toUtf8().constData());
2468 QHash<QString, QString>::Iterator it;
2469 QString kfr = params.paramValue("keyframes");
2471 if (!kfr.isEmpty()) {
2472 QStringList keyFrames = kfr.split(';', QString::SkipEmptyParts);
2473 kDebug() << "// ADDING KEYFRAME EFFECT: " << params.paramValue("keyframes");
2474 char *starttag = qstrdup(params.paramValue("starttag", "start").toUtf8().constData());
2475 char *endtag = qstrdup(params.paramValue("endtag", "end").toUtf8().constData());
2476 kDebug() << "// ADDING KEYFRAME TAGS: " << starttag << ", " << endtag;
2477 //double max = params.paramValue("max").toDouble();
2478 double min = params.paramValue("min").toDouble();
2479 double factor = params.paramValue("factor", "1").toDouble();
2480 params.removeParam("starttag");
2481 params.removeParam("endtag");
2482 params.removeParam("keyframes");
2483 params.removeParam("min");
2484 params.removeParam("max");
2485 params.removeParam("factor");
2487 // Special case, only one keyframe, means we want a constant value
2488 if (keyFrames.count() == 1) {
2489 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2490 if (filter && filter->is_valid()) {
2491 filter->set("kdenlive_id", filterId);
2492 int x1 = keyFrames.at(0).section(':', 0, 0).toInt();
2493 double y1 = keyFrames.at(0).section(':', 1, 1).toDouble();
2494 for (int j = 0; j < params.count(); j++) {
2495 filter->set(params.at(j).name().toUtf8().constData(), params.at(j).value().toUtf8().constData());
2497 filter->set("in", x1);
2498 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2499 filter->set(starttag, QString::number((min + y1) / factor).toUtf8().data());
2500 service.attach(*filter);
2502 } else for (int i = 0; i < keyFrames.size() - 1; ++i) {
2503 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2504 if (filter && filter->is_valid()) {
2505 filter->set("kdenlive_id", filterId);
2506 int x1 = keyFrames.at(i).section(':', 0, 0).toInt() + offset;
2507 double y1 = keyFrames.at(i).section(':', 1, 1).toDouble();
2508 int x2 = keyFrames.at(i + 1).section(':', 0, 0).toInt();
2509 double y2 = keyFrames.at(i + 1).section(':', 1, 1).toDouble();
2510 if (x2 == -1) x2 = duration;
2512 for (int j = 0; j < params.count(); j++) {
2513 filter->set(params.at(j).name().toUtf8().constData(), params.at(j).value().toUtf8().constData());
2516 filter->set("in", x1);
2517 filter->set("out", x2);
2518 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2519 filter->set(starttag, QString::number((min + y1) / factor).toUtf8().data());
2520 filter->set(endtag, QString::number((min + y2) / factor).toUtf8().data());
2521 service.attach(*filter);
2528 Mlt::Filter *filter;
2530 if (!region.isEmpty()) {
2531 filter = new Mlt::Filter(*m_mltProfile, "region");
2532 } else filter = new Mlt::Filter(*m_mltProfile, filterTag);
2533 if (filter && filter->is_valid()) {
2534 filter->set("kdenlive_id", filterId);
2535 if (!region.isEmpty()) {
2536 filter->set("resource", region.toUtf8().constData());
2537 filter->set("kdenlive_ix", params.paramValue("kdenlive_ix").toUtf8().constData());
2538 filter->set("filter0", filterTag);
2539 prefix = "filter0.";
2540 params.removeParam("id");
2541 params.removeParam("region");
2542 params.removeParam("kdenlive_ix");
2545 kDebug() << "filter is NULL";
2546 m_isBlocked = false;
2547 mlt_service_unlock(service.get_service());
2550 params.removeParam("kdenlive_id");
2552 for (int j = 0; j < params.count(); j++) {
2553 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2557 QString effectArgs = params.paramValue("id").section('_', 1);
2559 params.removeParam("id");
2560 params.removeParam("kdenlive_ix");
2561 params.removeParam("tag");
2562 params.removeParam("disable");
2563 params.removeParam("region");
2565 for (int j = 0; j < params.count(); j++) {
2566 effectArgs.append(' ' + params.at(j).value());
2568 //kDebug() << "SOX EFFECTS: " << effectArgs.simplified();
2569 filter->set("effect", effectArgs.simplified().toUtf8().constData());
2572 if (params.paramValue("id") == "pan_zoom") {
2573 filter->set_in_and_out(service.get_int("in"), service.get_int("out") + 1);
2576 // attach filter to the clip
2577 service.attach(*filter);
2582 // re-add following filters
2583 for (int i = 0; i < filtersList.count(); i++) {
2584 Mlt::Filter *filter = filtersList.at(i);
2586 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") + 1);
2587 service.attach(*filter);
2589 m_isBlocked = false;
2590 mlt_service_unlock(service.get_service());
2591 if (doRefresh) refresh();
2595 bool Render::mltEditTrackEffect(int track, EffectsParameterList params)
2597 Mlt::Service service(m_mltProducer->parent().get_service());
2598 Mlt::Tractor tractor(service);
2599 Mlt::Producer trackProducer(tractor.track(track));
2600 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2601 Mlt::Service clipService(trackPlaylist.get_service());
2604 QString index = params.paramValue("kdenlive_ix");
2605 QString tag = params.paramValue("tag");
2607 Mlt::Filter *filter = clipService.filter(ct);
2609 if (filter->get_int("kdenlive_ix") == index.toInt()) {
2613 filter = clipService.filter(ct);
2617 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT! " << index << ", " << tag;
2618 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2620 bool success = false;//mltAddTrackEffect(track, params);
2621 m_isBlocked = false;
2625 QString ser = filter->get("mlt_service");
2626 if (ser == "region") prefix = "filter0.";
2627 mlt_service_lock(service.get_service());
2628 for (int j = 0; j < params.count(); j++) {
2629 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2631 mlt_service_unlock(service.get_service());
2633 m_isBlocked = false;
2639 bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList params)
2641 QString index = params.paramValue("kdenlive_ix");
2642 QString tag = params.paramValue("tag");
2644 if (!params.paramValue("keyframes").isEmpty() || /*it.key().startsWith("#") || */tag.startsWith("ladspa") || tag == "sox" || tag == "autotrack_rectangle" || params.hasParam("region")) {
2645 // This is a keyframe effect, to edit it, we remove it and re-add it.
2646 bool success = mltRemoveEffect(track, position, index, false);
2647 // if (!success) kDebug() << "// ERROR Removing effect : " << index;
2648 if (position < GenTime())
2649 success = mltAddTrackEffect(track, params);
2651 success = mltAddEffect(track, position, params);
2652 // if (!success) kDebug() << "// ERROR Adding effect : " << index;
2655 if (position < GenTime()) {
2656 return mltEditTrackEffect(track, params);
2659 Mlt::Service service(m_mltProducer->parent().get_service());
2660 Mlt::Tractor tractor(service);
2661 Mlt::Producer trackProducer(tractor.track(track));
2662 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2664 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2665 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2667 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2671 int duration = clip->get_playtime();
2672 bool doRefresh = true;
2673 // Check if clip is visible in monitor
2674 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2675 if (diff < 0 || diff > duration)
2680 Mlt::Filter *filter = clip->filter(ct);
2682 if (filter->get_int("kdenlive_ix") == index.toInt()) {
2686 filter = clip->filter(ct);
2690 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT! " << index << ", " << tag;
2691 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2693 bool success = mltAddEffect(track, position, params);
2694 m_isBlocked = false;
2698 QString ser = filter->get("mlt_service");
2699 if (ser == "region") prefix = "filter0.";
2700 mlt_service_lock(service.get_service());
2701 for (int j = 0; j < params.count(); j++) {
2702 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2705 if (params.paramValue("id") == "pan_zoom")
2706 filter->set_in_and_out(clip->get_in(), clip->get_out() + 1);
2709 mlt_service_unlock(service.get_service());
2711 m_isBlocked = false;
2712 if (doRefresh) refresh();
2716 void Render::mltUpdateEffectPosition(int track, GenTime position, int oldPos, int newPos)
2718 Mlt::Service service(m_mltProducer->parent().get_service());
2719 Mlt::Tractor tractor(service);
2720 Mlt::Producer trackProducer(tractor.track(track));
2721 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2723 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2724 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2726 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2730 Mlt::Service clipService(clip->get_service());
2731 int duration = clip->get_playtime();
2732 bool doRefresh = true;
2733 // Check if clip is visible in monitor
2734 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2735 if (diff < 0 || diff > duration) doRefresh = false;
2740 Mlt::Filter *filter = clipService.filter(ct);
2742 int pos = filter->get_int("kdenlive_ix");
2743 if (pos == oldPos) {
2744 filter->set("kdenlive_ix", newPos);
2746 filter = clipService.filter(ct);
2749 m_isBlocked = false;
2750 if (doRefresh) refresh();
2753 void Render::mltMoveEffect(int track, GenTime position, int oldPos, int newPos)
2755 if (position < GenTime()) {
2756 mltMoveTrackEffect(track, oldPos, newPos);
2759 Mlt::Service service(m_mltProducer->parent().get_service());
2760 Mlt::Tractor tractor(service);
2761 Mlt::Producer trackProducer(tractor.track(track));
2762 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2764 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2765 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2767 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2771 Mlt::Service clipService(clip->get_service());
2772 int duration = clip->get_playtime();
2773 bool doRefresh = true;
2774 // Check if clip is visible in monitor
2775 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2776 if (diff < 0 || diff > duration) doRefresh = false;
2781 QList <Mlt::Filter *> filtersList;
2782 Mlt::Filter *filter = clipService.filter(ct);
2784 if (newPos > oldPos) {
2786 if (!found && filter->get_int("kdenlive_ix") == oldPos) {
2787 filter->set("kdenlive_ix", newPos);
2788 filtersList.append(filter);
2789 clipService.detach(*filter);
2790 filter = clipService.filter(ct);
2791 while (filter && filter->get_int("kdenlive_ix") <= newPos) {
2792 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2794 filter = clipService.filter(ct);
2798 if (filter && filter->get_int("kdenlive_ix") > newPos) {
2799 filtersList.append(filter);
2800 clipService.detach(*filter);
2802 filter = clipService.filter(ct);
2806 if (filter->get_int("kdenlive_ix") == oldPos) {
2807 filter->set("kdenlive_ix", newPos);
2808 filtersList.append(filter);
2809 clipService.detach(*filter);
2811 filter = clipService.filter(ct);
2815 filter = clipService.filter(ct);
2817 int pos = filter->get_int("kdenlive_ix");
2818 if (pos >= newPos) {
2819 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
2820 filtersList.append(filter);
2821 clipService.detach(*filter);
2823 filter = clipService.filter(ct);
2827 for (int i = 0; i < filtersList.count(); i++) {
2828 clipService.attach(*(filtersList.at(i)));
2831 m_isBlocked = false;
2832 if (doRefresh) refresh();
2835 void Render::mltMoveTrackEffect(int track, int oldPos, int newPos)
2837 Mlt::Service service(m_mltProducer->parent().get_service());
2838 Mlt::Tractor tractor(service);
2839 Mlt::Producer trackProducer(tractor.track(track));
2840 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2842 Mlt::Service clipService(trackPlaylist.get_service());
2846 QList <Mlt::Filter *> filtersList;
2847 Mlt::Filter *filter = clipService.filter(ct);
2849 if (newPos > oldPos) {
2851 if (!found && filter->get_int("kdenlive_ix") == oldPos) {
2852 filter->set("kdenlive_ix", newPos);
2853 filtersList.append(filter);
2854 clipService.detach(*filter);
2855 filter = clipService.filter(ct);
2856 while (filter && filter->get_int("kdenlive_ix") <= newPos) {
2857 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2859 filter = clipService.filter(ct);
2863 if (filter && filter->get_int("kdenlive_ix") > newPos) {
2864 filtersList.append(filter);
2865 clipService.detach(*filter);
2867 filter = clipService.filter(ct);
2871 if (filter->get_int("kdenlive_ix") == oldPos) {
2872 filter->set("kdenlive_ix", newPos);
2873 filtersList.append(filter);
2874 clipService.detach(*filter);
2876 filter = clipService.filter(ct);
2880 filter = clipService.filter(ct);
2882 int pos = filter->get_int("kdenlive_ix");
2883 if (pos >= newPos) {
2884 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
2885 filtersList.append(filter);
2886 clipService.detach(*filter);
2888 filter = clipService.filter(ct);
2892 for (int i = 0; i < filtersList.count(); i++) {
2893 clipService.attach(*(filtersList.at(i)));
2895 m_isBlocked = false;
2899 bool Render::mltResizeClipEnd(ItemInfo info, GenTime clipDuration)
2902 Mlt::Service service(m_mltProducer->parent().get_service());
2903 Mlt::Tractor tractor(service);
2904 Mlt::Producer trackProducer(tractor.track(info.track));
2905 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2907 /* // Display playlist info
2908 kDebug()<<"//////////// BEFORE RESIZE";
2909 for (int i = 0; i < trackPlaylist.count(); i++) {
2910 int blankStart = trackPlaylist.clip_start(i);
2911 int blankDuration = trackPlaylist.clip_length(i) - 1;
2913 if (trackPlaylist.is_blank(i)) blk = "(blank)";
2914 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
2917 if (trackPlaylist.is_blank_at((int) info.startPos.frames(m_fps))) {
2918 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
2919 m_isBlocked = false;
2922 mlt_service_lock(service.get_service());
2923 int clipIndex = trackPlaylist.get_clip_index_at((int) info.startPos.frames(m_fps));
2924 //kDebug() << "// SELECTED CLIP START: " << trackPlaylist.clip_start(clipIndex);
2925 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2927 int previousStart = clip->get_in();
2928 int newDuration = (int) clipDuration.frames(m_fps) - 1;
2929 int diff = newDuration - (trackPlaylist.clip_length(clipIndex) - 1);
2931 int currentOut = newDuration + previousStart;
2932 if (currentOut > clip->get_length()) {
2933 clip->parent().set("length", currentOut + 1);
2934 clip->parent().set("out", currentOut);
2935 clip->set("length", currentOut + 1);
2938 /*if (newDuration > clip->get_out()) {
2939 clip->parent().set_in_and_out(0, newDuration + 1);
2940 clip->set_in_and_out(0, newDuration + 1);
2943 trackPlaylist.resize_clip(clipIndex, previousStart, newDuration + previousStart);
2944 trackPlaylist.consolidate_blanks(0);
2945 // skip to next clip
2947 //kDebug() << "//////// RESIZE CLIP: " << clipIndex << "( pos: " << info.startPos.frames(25) << "), DIFF: " << diff << ", CURRENT DUR: " << previousDuration << ", NEW DUR: " << newDuration << ", IX: " << clipIndex << ", MAX: " << trackPlaylist.count();
2949 // clip was made longer, trim next blank if there is one.
2950 if (clipIndex < trackPlaylist.count()) {
2951 // If this is not the last clip in playlist
2952 if (trackPlaylist.is_blank(clipIndex)) {
2953 int blankStart = trackPlaylist.clip_start(clipIndex);
2954 int blankDuration = trackPlaylist.clip_length(clipIndex);
2955 if (diff > blankDuration) {
2956 kDebug() << "// ERROR blank clip is not large enough to get back required space!!!";
2958 if (diff - blankDuration == 0) {
2959 trackPlaylist.remove(clipIndex);
2960 } else trackPlaylist.remove_region(blankStart, diff);
2962 kDebug() << "/// RESIZE ERROR, NXT CLIP IS NOT BLK: " << clipIndex;
2965 } else if (clipIndex != trackPlaylist.count()) trackPlaylist.insert_blank(clipIndex, 0 - diff - 1);
2966 trackPlaylist.consolidate_blanks(0);
2967 mlt_service_unlock(service.get_service());
2969 if (info.track != 0 && clipIndex == trackPlaylist.count()) mltCheckLength(&tractor);
2970 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
2971 //mltResizeTransparency(previousStart, previousStart, previousStart + newDuration, track, QString(clip->parent().get("id")).toInt());
2972 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
2973 ItemInfo transpinfo;
2974 transpinfo.startPos = info.startPos;
2975 transpinfo.endPos = info.startPos + clipDuration;
2976 transpinfo.track = info.track;
2977 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
2979 m_isBlocked = false;
2980 m_mltConsumer->set("refresh", 1);
2984 void Render::mltChangeTrackState(int track, bool mute, bool blind)
2986 Mlt::Service service(m_mltProducer->parent().get_service());
2987 Mlt::Tractor tractor(service);
2988 Mlt::Producer trackProducer(tractor.track(track));
2990 // Make sure muting will not produce problems with our audio mixing transition,
2991 // because audio mixing is done between each track and the lowest one
2992 bool audioMixingBroken = false;
2993 if (mute && trackProducer.get_int("hide") < 2 ) {
2994 // We mute a track with sound
2995 if (track == getLowestNonMutedAudioTrack(tractor)) audioMixingBroken = true;
2997 else if (!mute && trackProducer.get_int("hide") > 1 ) {
2998 // We un-mute a previously muted track
2999 if (track < getLowestNonMutedAudioTrack(tractor)) audioMixingBroken = true;
3003 if (blind) trackProducer.set("hide", 3);
3004 else trackProducer.set("hide", 2);
3006 trackProducer.set("hide", 1);
3008 trackProducer.set("hide", 0);
3010 if (audioMixingBroken) fixAudioMixing(tractor);
3012 tractor.multitrack()->refresh();
3017 int Render::getLowestNonMutedAudioTrack(Mlt::Tractor tractor)
3019 for (int i = 1; i < tractor.count(); i++) {
3020 Mlt::Producer trackProducer(tractor.track(i));
3021 if (trackProducer.get_int("hide") < 2) return i;
3023 return tractor.count() - 1;
3026 void Render::fixAudioMixing(Mlt::Tractor tractor)
3028 // Make sure the audio mixing transitions are applied to the lowest audible (non muted) track
3029 int lowestTrack = getLowestNonMutedAudioTrack(tractor);
3031 mlt_service serv = m_mltProducer->parent().get_service();
3032 Mlt::Field *field = tractor.field();
3033 mlt_service_lock(serv);
3036 mlt_service nextservice = mlt_service_get_producer(serv);
3037 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3038 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3039 QString resource = mlt_properties_get(properties, "mlt_service");
3041 mlt_service nextservicetodisconnect;
3042 // Delete all audio mixing transitions
3043 while (mlt_type == "transition") {
3044 if (resource == "mix") {
3045 nextservicetodisconnect = nextservice;
3046 nextservice = mlt_service_producer(nextservice);
3047 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
3049 else nextservice = mlt_service_producer(nextservice);
3050 if (nextservice == NULL) break;
3051 properties = MLT_SERVICE_PROPERTIES(nextservice);
3052 mlt_type = mlt_properties_get(properties, "mlt_type");
3053 resource = mlt_properties_get(properties, "mlt_service");
3056 // Re-add correct audio transitions
3057 for (int i = lowestTrack + 1; i < tractor.count(); i++) {
3058 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "mix");
3059 transition->set("always_active", 1);
3060 transition->set("combine", 1);
3061 transition->set("internal_added", 237);
3062 field->plant_transition(*transition, lowestTrack, i);
3064 mlt_service_unlock(serv);
3068 bool Render::mltResizeClipCrop(ItemInfo info, GenTime diff)
3070 Mlt::Service service(m_mltProducer->parent().get_service());
3071 int frameOffset = (int) diff.frames(m_fps);
3072 Mlt::Tractor tractor(service);
3073 Mlt::Producer trackProducer(tractor.track(info.track));
3074 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3075 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
3076 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
3079 mlt_service_lock(service.get_service());
3080 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
3081 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
3083 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
3084 mlt_service_unlock(service.get_service());
3087 int previousStart = clip->get_in();
3088 int previousOut = clip->get_out();
3091 trackPlaylist.resize_clip(clipIndex, previousStart + frameOffset, previousOut + frameOffset);
3092 m_isBlocked = false;
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();
3122 previousStart += moveFrame;
3124 if (previousStart < 0) {
3125 // this is possible for images and color clips
3126 previousOut -= previousStart;
3130 int length = previousOut + 1;
3131 if (length > clip->get_length()) {
3132 clip->parent().set("length", length + 1);
3133 clip->parent().set("out", length);
3134 clip->set("length", length + 1);
3138 // kDebug() << "RESIZE, new start: " << previousStart << ", " << previousOut;
3139 trackPlaylist.resize_clip(clipIndex, previousStart, previousOut);
3140 if (moveFrame > 0) {
3141 trackPlaylist.insert_blank(clipIndex, moveFrame - 1);
3143 //int midpos = info.startPos.frames(m_fps) + moveFrame - 1;
3144 int blankIndex = clipIndex - 1;
3145 int blankLength = trackPlaylist.clip_length(blankIndex);
3146 // kDebug() << " + resizing blank length " << blankLength << ", SIZE DIFF: " << moveFrame;
3147 if (! trackPlaylist.is_blank(blankIndex)) {
3148 kDebug() << "WARNING, CLIP TO RESIZE IS NOT BLANK";
3150 if (blankLength + moveFrame == 0)
3151 trackPlaylist.remove(blankIndex);
3153 trackPlaylist.resize_clip(blankIndex, 0, blankLength + moveFrame - 1);
3155 trackPlaylist.consolidate_blanks(0);
3156 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
3157 //mltResizeTransparency(previousStart, (int) moveEnd.frames(m_fps), (int) (moveEnd + out - in).frames(m_fps), track, QString(clip->parent().get("id")).toInt());
3158 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
3159 ItemInfo transpinfo;
3160 transpinfo.startPos = info.startPos + diff;
3161 transpinfo.endPos = info.startPos + diff + (info.endPos - info.startPos);
3162 transpinfo.track = info.track;
3163 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
3165 m_isBlocked = false;
3166 //m_mltConsumer->set("refresh", 1);
3167 mlt_service_unlock(service.get_service());
3168 m_mltConsumer->set("refresh", 1);
3172 bool Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd, Mlt::Producer *prod, bool overwrite, bool insert)
3174 return mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps), prod, overwrite, insert);
3178 bool Render::mltUpdateClipProducer(int track, int pos, Mlt::Producer *prod)
3180 if (prod == NULL || !prod->is_valid()) {
3181 kDebug() << "// Warning, CLIP on track " << track << ", at: " << pos << " is invalid, cannot update it!!!";
3184 kDebug() << "NEW PROD ID: " << prod->get("id");
3186 kDebug() << "// TRYING TO UPDATE CLIP at: " << pos << ", TK: " << track;
3187 Mlt::Service service(m_mltProducer->parent().get_service());
3188 if (service.type() != tractor_type) {
3189 kWarning() << "// TRACTOR PROBLEM";
3192 mlt_service_lock(service.get_service());
3193 Mlt::Tractor tractor(service);
3194 Mlt::Producer trackProducer(tractor.track(track));
3195 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3196 int clipIndex = trackPlaylist.get_clip_index_at(pos);
3197 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3198 if (clipProducer == NULL || clipProducer->is_blank()) {
3199 kDebug() << "// ERROR UPDATING CLIP PROD";
3200 delete clipProducer;
3201 mlt_service_unlock(service.get_service());
3205 Mlt::Producer *clip = prod->cut(clipProducer->get_in(), clipProducer->get_out());
3206 if (!clip || !clip->is_valid()) {
3207 if (clip) delete clip;
3208 delete clipProducer;
3209 mlt_service_unlock(service.get_service());
3213 // move all effects to the correct producer
3214 mltPasteEffects(clipProducer, clip);
3215 trackPlaylist.insert_at(pos, clip, 1);
3217 delete clipProducer;
3218 mlt_service_unlock(service.get_service());
3223 bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd, Mlt::Producer *prod, bool overwrite, bool /*insert*/)
3227 Mlt::Service service(m_mltProducer->parent().get_service());
3228 if (service.type() != tractor_type) {
3229 kWarning() << "// TRACTOR PROBLEM";
3233 Mlt::Tractor tractor(service);
3234 mlt_service_lock(service.get_service());
3235 Mlt::Producer trackProducer(tractor.track(startTrack));
3236 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3237 int clipIndex = trackPlaylist.get_clip_index_at(moveStart);
3238 kDebug() << "////// LOOKING FOR CLIP TO MOVE, INDEX: " << clipIndex;
3239 bool checkLength = false;
3240 if (endTrack == startTrack) {
3241 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3242 if ((!overwrite && !trackPlaylist.is_blank_at(moveEnd)) || !clipProducer || !clipProducer->is_valid() || clipProducer->is_blank()) {
3243 // error, destination is not empty
3245 if (!trackPlaylist.is_blank_at(moveEnd) && clipProducer->is_valid()) trackPlaylist.insert_at(moveStart, clipProducer, 1);
3246 delete clipProducer;
3248 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
3249 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
3250 mlt_service_unlock(service.get_service());
3254 trackPlaylist.consolidate_blanks(0);
3256 trackPlaylist.remove_region(moveEnd, clipProducer->get_playtime());
3257 int clipIndex = trackPlaylist.get_clip_index_at(moveEnd);
3258 trackPlaylist.insert_blank(clipIndex, clipProducer->get_playtime() - 1);
3260 int newIndex = trackPlaylist.insert_at(moveEnd, clipProducer, 1);
3261 trackPlaylist.consolidate_blanks(1);
3262 delete clipProducer;
3263 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
3264 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
3266 if (newIndex + 1 == trackPlaylist.count()) checkLength = true;
3268 //mlt_service_unlock(service.get_service());
3270 Mlt::Producer destTrackProducer(tractor.track(endTrack));
3271 Mlt::Playlist destTrackPlaylist((mlt_playlist) destTrackProducer.get_service());
3272 if (!overwrite && !destTrackPlaylist.is_blank_at(moveEnd)) {
3273 // error, destination is not empty
3274 mlt_service_unlock(service.get_service());
3278 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3279 if (!clipProducer || clipProducer->is_blank()) {
3280 // error, destination is not empty
3281 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
3282 if (clipProducer) delete clipProducer;
3283 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
3284 mlt_service_unlock(service.get_service());
3288 trackPlaylist.consolidate_blanks(0);
3289 destTrackPlaylist.consolidate_blanks(1);
3290 Mlt::Producer *clip;
3291 // check if we are moving a slowmotion producer
3292 QString serv = clipProducer->parent().get("mlt_service");
3293 QString currentid = clipProducer->parent().get("id");
3294 if (serv == "framebuffer" || currentid.endsWith("_video")) {
3295 clip = clipProducer;
3298 // Special case: prod is null when using placeholder clips.
3299 // in that case, use the producer existing in playlist. Note that
3300 // it will bypass the one producer per track logic and might cause
3301 // Sound cracks if clip is moved so that it overlaps another copy of itself
3302 clip = clipProducer->cut(clipProducer->get_in(), clipProducer->get_out());
3303 } else clip = prod->cut(clipProducer->get_in(), clipProducer->get_out());
3306 // move all effects to the correct producer
3307 mltPasteEffects(clipProducer, clip);
3310 destTrackPlaylist.remove_region(moveEnd, clip->get_playtime());
3311 int clipIndex = destTrackPlaylist.get_clip_index_at(moveEnd);
3312 destTrackPlaylist.insert_blank(clipIndex, clip->get_playtime() - 1);
3315 int newIndex = destTrackPlaylist.insert_at(moveEnd, clip, 1);
3317 if (clip == clipProducer) {
3322 delete clipProducer;
3324 destTrackPlaylist.consolidate_blanks(0);
3325 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
3326 kDebug() << "//////// moving clip transparency";
3327 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
3329 if (clipIndex > trackPlaylist.count()) checkLength = true;
3330 else if (newIndex + 1 == destTrackPlaylist.count()) checkLength = true;
3333 mlt_service_unlock(service.get_service());
3334 if (checkLength) mltCheckLength(&tractor);
3337 //m_mltConsumer->set("refresh", 1);
3342 QList <int> Render::checkTrackSequence(int track)
3345 Mlt::Service service(m_mltProducer->parent().get_service());
3346 if (service.type() != tractor_type) {
3347 kWarning() << "// TRACTOR PROBLEM";
3350 Mlt::Tractor tractor(service);
3351 mlt_service_lock(service.get_service());
3352 Mlt::Producer trackProducer(tractor.track(track));
3353 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3354 int clipNb = trackPlaylist.count();
3355 //kDebug() << "// PARSING SCENE TRACK: " << t << ", CLIPS: " << clipNb;
3356 for (int i = 0; i < clipNb; i++) {
3357 Mlt::Producer *c = trackPlaylist.get_clip(i);
3358 int pos = trackPlaylist.clip_start(i);
3359 if (!list.contains(pos)) list.append(pos);
3360 pos += c->get_playtime();
3361 if (!list.contains(pos)) list.append(pos);
3367 bool Render::mltMoveTransition(QString type, int startTrack, int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut)
3369 int new_in = (int)newIn.frames(m_fps);
3370 int new_out = (int)newOut.frames(m_fps) - 1;
3371 if (new_in >= new_out) return false;
3372 int old_in = (int)oldIn.frames(m_fps);
3373 int old_out = (int)oldOut.frames(m_fps) - 1;
3375 Mlt::Service service(m_mltProducer->parent().get_service());
3376 Mlt::Tractor tractor(service);
3377 Mlt::Field *field = tractor.field();
3379 bool doRefresh = true;
3380 // Check if clip is visible in monitor
3381 int diff = old_out - m_mltProducer->position();
3382 if (diff < 0 || diff > old_out - old_in) doRefresh = false;
3384 diff = new_out - m_mltProducer->position();
3385 if (diff < 0 || diff > new_out - new_in) doRefresh = false;
3389 mlt_service_lock(service.get_service());
3391 mlt_service nextservice = mlt_service_get_producer(service.get_service());
3392 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3393 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3394 QString resource = mlt_properties_get(properties, "mlt_service");
3395 int old_pos = (int)(old_in + old_out) / 2;
3398 while (mlt_type == "transition") {
3399 Mlt::Transition transition((mlt_transition) nextservice);
3400 int currentTrack = transition.get_b_track();
3401 int currentIn = (int) transition.get_in();
3402 int currentOut = (int) transition.get_out();
3404 if (resource == type && startTrack == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
3406 if (newTrack - startTrack != 0) {
3407 Mlt::Properties trans_props(transition.get_properties());
3408 Mlt::Transition new_transition(*m_mltProfile, transition.get("mlt_service"));
3409 Mlt::Properties new_trans_props(new_transition.get_properties());
3410 new_trans_props.inherit(trans_props);
3411 new_transition.set_in_and_out(new_in, new_out);
3412 field->disconnect_service(transition);
3413 mltPlantTransition(field, new_transition, newTransitionTrack, newTrack);
3414 //field->plant_transition(new_transition, newTransitionTrack, newTrack);
3415 } else transition.set_in_and_out(new_in, new_out);
3418 nextservice = mlt_service_producer(nextservice);
3419 if (nextservice == NULL) break;
3420 properties = MLT_SERVICE_PROPERTIES(nextservice);
3421 mlt_type = mlt_properties_get(properties, "mlt_type");
3422 resource = mlt_properties_get(properties, "mlt_service");
3424 mlt_service_unlock(service.get_service());
3426 if (doRefresh) refresh();
3427 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3432 void Render::mltPlantTransition(Mlt::Field *field, Mlt::Transition &tr, int a_track, int b_track)
3434 mlt_service nextservice = mlt_service_get_producer(field->get_service());
3435 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3436 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3437 QString resource = mlt_properties_get(properties, "mlt_service");
3438 QList <Mlt::Transition *> trList;
3440 while (mlt_type == "transition") {
3441 Mlt::Transition transition((mlt_transition) nextservice);
3442 int aTrack = transition.get_a_track();
3443 int bTrack = transition.get_b_track();
3444 if (resource != "mix" && (aTrack < a_track || (aTrack == a_track && bTrack > b_track))) {
3445 Mlt::Properties trans_props(transition.get_properties());
3446 Mlt::Transition *cp = new Mlt::Transition(*m_mltProfile, transition.get("mlt_service"));
3447 Mlt::Properties new_trans_props(cp->get_properties());
3448 new_trans_props.inherit(trans_props);
3450 field->disconnect_service(transition);
3452 //else kDebug() << "// FOUND TRANS OK, "<<resource<< ", A_: " << aTrack << ", B_ "<<bTrack;
3454 nextservice = mlt_service_producer(nextservice);
3455 if (nextservice == NULL) break;
3456 properties = MLT_SERVICE_PROPERTIES(nextservice);
3457 mlt_type = mlt_properties_get(properties, "mlt_type");
3458 resource = mlt_properties_get(properties, "mlt_service");
3460 field->plant_transition(tr, a_track, b_track);
3462 // re-add upper transitions
3463 for (int i = trList.count() - 1; i >= 0; i--) {
3464 //kDebug()<< "REPLANT ON TK: "<<trList.at(i)->get_a_track()<<", "<<trList.at(i)->get_b_track();
3465 field->plant_transition(*trList.at(i), trList.at(i)->get_a_track(), trList.at(i)->get_b_track());
3469 void Render::mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool force)
3471 if (oldTag == tag && !force) mltUpdateTransitionParams(tag, a_track, b_track, in, out, xml);
3473 kDebug()<<"// DELETING TRANS: "<<a_track<<"-"<<b_track;
3474 mltDeleteTransition(oldTag, a_track, b_track, in, out, xml, false);
3475 mltAddTransition(tag, a_track, b_track, in, out, xml, false);
3478 if (m_mltProducer->position() >= in.frames(m_fps) && m_mltProducer->position() <= out.frames(m_fps)) refresh();
3481 void Render::mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml)
3483 mlt_service serv = m_mltProducer->parent().get_service();
3484 mlt_service_lock(serv);
3487 mlt_service nextservice = mlt_service_get_producer(serv);
3488 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3489 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3490 QString resource = mlt_properties_get(properties, "mlt_service");
3491 int in_pos = (int) in.frames(m_fps);
3492 int out_pos = (int) out.frames(m_fps) - 1;
3494 while (mlt_type == "transition") {
3495 mlt_transition tr = (mlt_transition) nextservice;
3496 int currentTrack = mlt_transition_get_b_track(tr);
3497 int currentBTrack = mlt_transition_get_a_track(tr);
3498 int currentIn = (int) mlt_transition_get_in(tr);
3499 int currentOut = (int) mlt_transition_get_out(tr);
3501 // kDebug()<<"Looking for transition : " << currentIn <<'x'<<currentOut<< ", OLD oNE: "<<in_pos<<'x'<<out_pos;
3503 if (resource == type && b_track == currentTrack && currentIn == in_pos && currentOut == out_pos) {
3504 QMap<QString, QString> map = mltGetTransitionParamsFromXml(xml);
3505 QMap<QString, QString>::Iterator it;
3507 mlt_properties transproperties = MLT_TRANSITION_PROPERTIES(tr);
3509 QString currentId = mlt_properties_get(transproperties, "kdenlive_id");
3510 if (currentId != xml.attribute("id")) {
3511 // The transition ID is not the same, so reset all properties
3512 mlt_properties_set(transproperties, "kdenlive_id", xml.attribute("id").toUtf8().constData());
3513 // Cleanup previous properties
3514 QStringList permanentProps;
3515 permanentProps << "factory" << "kdenlive_id" << "mlt_service" << "mlt_type" << "in";
3516 permanentProps << "out" << "a_track" << "b_track";
3517 for (int i = 0; i < mlt_properties_count(transproperties); i++) {
3518 QString propName = mlt_properties_get_name(transproperties, i);
3519 if (!propName.startsWith('_') && ! permanentProps.contains(propName)) {
3520 mlt_properties_set(transproperties, propName.toUtf8().constData(), "");
3525 mlt_properties_set_int(transproperties, "force_track", xml.attribute("force_track").toInt());
3526 mlt_properties_set_int(transproperties, "automatic", xml.attribute("automatic", "0").toInt());
3528 if (currentBTrack != a_track) {
3529 mlt_properties_set_int(transproperties, "a_track", a_track);
3531 for (it = map.begin(); it != map.end(); ++it) {
3533 mlt_properties_set(transproperties, key.toUtf8().constData(), it.value().toUtf8().constData());
3534 //kDebug() << " ------ UPDATING TRANS PARAM: " << key.toUtf8().constData() << ": " << it.value().toUtf8().constData();
3535 //filter->set("kdenlive_id", id);
3539 nextservice = mlt_service_producer(nextservice);
3540 if (nextservice == NULL) break;
3541 properties = MLT_SERVICE_PROPERTIES(nextservice);
3542 mlt_type = mlt_properties_get(properties, "mlt_type");
3543 resource = mlt_properties_get(properties, "mlt_service");
3545 mlt_service_unlock(serv);
3548 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3551 void Render::mltDeleteTransition(QString tag, int /*a_track*/, int b_track, GenTime in, GenTime out, QDomElement /*xml*/, bool /*do_refresh*/)
3553 mlt_service serv = m_mltProducer->parent().get_service();
3555 mlt_service_lock(serv);
3557 Mlt::Service service(serv);
3558 Mlt::Tractor tractor(service);
3559 Mlt::Field *field = tractor.field();
3561 //if (do_refresh) m_mltConsumer->set("refresh", 0);
3563 mlt_service nextservice = mlt_service_get_producer(serv);
3564 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3565 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3566 QString resource = mlt_properties_get(properties, "mlt_service");
3568 const int old_pos = (int)((in + out).frames(m_fps) / 2);
3569 kDebug() << " del trans pos: " << in.frames(25) << "-" << out.frames(25);
3571 while (mlt_type == "transition") {
3572 mlt_transition tr = (mlt_transition) nextservice;
3573 int currentTrack = mlt_transition_get_b_track(tr);
3574 int currentIn = (int) mlt_transition_get_in(tr);
3575 int currentOut = (int) mlt_transition_get_out(tr);
3576 //kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
3578 if (resource == tag && b_track == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
3579 mlt_field_disconnect_service(field->get_field(), nextservice);
3582 nextservice = mlt_service_producer(nextservice);
3583 if (nextservice == NULL) break;
3584 properties = MLT_SERVICE_PROPERTIES(nextservice);
3585 mlt_type = mlt_properties_get(properties, "mlt_type");
3586 resource = mlt_properties_get(properties, "mlt_service");
3588 mlt_service_unlock(serv);
3591 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3594 QMap<QString, QString> Render::mltGetTransitionParamsFromXml(QDomElement xml)
3596 QDomNodeList attribs = xml.elementsByTagName("parameter");
3597 QMap<QString, QString> map;
3598 for (int i = 0; i < attribs.count(); i++) {
3599 QDomElement e = attribs.item(i).toElement();
3600 QString name = e.attribute("name");
3601 //kDebug()<<"-- TRANSITION PARAM: "<<name<<" = "<< e.attribute("name")<<" / " << e.attribute("value");
3602 map[name] = e.attribute("default");
3603 if (!e.attribute("value").isEmpty()) {
3604 map[name] = e.attribute("value");
3606 if (e.attribute("type") != "addedgeometry" && !e.attribute("factor").isEmpty() && e.attribute("factor").toDouble() > 0) {
3607 map[name] = QString::number(map.value(name).toDouble() / e.attribute("factor").toDouble());
3608 //map[name]=map[name].replace(".",","); //FIXME how to solve locale conversion of . ,
3611 if (e.attribute("namedesc").contains(';')) {
3612 QString format = e.attribute("format");
3613 QStringList separators = format.split("%d", QString::SkipEmptyParts);
3614 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
3616 QTextStream txtNeu(&neu);
3617 if (values.size() > 0)
3618 txtNeu << (int)values[0].toDouble();
3620 for (i = 0; i < separators.size() && i + 1 < values.size(); i++) {
3621 txtNeu << separators[i];
3622 txtNeu << (int)(values[i+1].toDouble());
3624 if (i < separators.size())
3625 txtNeu << separators[i];
3626 map[e.attribute("name")] = neu;
3633 void Render::mltAddClipTransparency(ItemInfo info, int transitiontrack, int id)
3635 kDebug() << "///////// ADDING CLIP TRANSPARENCY AT: " << info.startPos.frames(25);
3636 Mlt::Service service(m_mltProducer->parent().get_service());
3637 Mlt::Tractor tractor(service);
3638 Mlt::Field *field = tractor.field();
3640 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
3641 transition->set_in_and_out((int) info.startPos.frames(m_fps), (int) info.endPos.frames(m_fps) - 1);
3642 transition->set("transparency", id);
3643 transition->set("fill", 1);
3644 transition->set("internal_added", 237);
3645 field->plant_transition(*transition, transitiontrack, info.track);
3649 void Render::mltDeleteTransparency(int pos, int track, int id)
3651 Mlt::Service service(m_mltProducer->parent().get_service());
3652 Mlt::Tractor tractor(service);
3653 Mlt::Field *field = tractor.field();
3655 //if (do_refresh) m_mltConsumer->set("refresh", 0);
3656 mlt_service serv = m_mltProducer->parent().get_service();
3658 mlt_service nextservice = mlt_service_get_producer(serv);
3659 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3660 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3661 QString resource = mlt_properties_get(properties, "mlt_service");
3663 while (mlt_type == "transition") {
3664 mlt_transition tr = (mlt_transition) nextservice;
3665 int currentTrack = mlt_transition_get_b_track(tr);
3666 int currentIn = (int) mlt_transition_get_in(tr);
3667 int currentOut = (int) mlt_transition_get_out(tr);
3668 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3669 kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
3671 if (resource == "composite" && track == currentTrack && currentIn == pos && transitionId == id) {
3672 //kDebug() << " / / / / /DELETE TRANS DOOOMNE";
3673 mlt_field_disconnect_service(field->get_field(), nextservice);
3676 nextservice = mlt_service_producer(nextservice);
3677 if (nextservice == NULL) break;
3678 properties = MLT_SERVICE_PROPERTIES(nextservice);
3679 mlt_type = mlt_properties_get(properties, "mlt_type");
3680 resource = mlt_properties_get(properties, "mlt_service");
3682 //if (do_refresh) m_mltConsumer->set("refresh", 1);
3685 void Render::mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id)
3687 Mlt::Service service(m_mltProducer->parent().get_service());
3688 Mlt::Tractor tractor(service);
3690 mlt_service_lock(service.get_service());
3691 m_mltConsumer->set("refresh", 0);
3694 mlt_service serv = m_mltProducer->parent().get_service();
3695 mlt_service nextservice = mlt_service_get_producer(serv);
3696 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3697 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3698 QString resource = mlt_properties_get(properties, "mlt_service");
3699 kDebug() << "// resize transpar from: " << oldStart << ", TO: " << newStart << 'x' << newEnd << ", " << track << ", " << id;
3700 while (mlt_type == "transition") {
3701 mlt_transition tr = (mlt_transition) nextservice;
3702 int currentTrack = mlt_transition_get_b_track(tr);
3703 int currentIn = (int) mlt_transition_get_in(tr);
3704 //mlt_properties props = MLT_TRANSITION_PROPERTIES(tr);
3705 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3706 kDebug() << "// resize transpar current in: " << currentIn << ", Track: " << currentTrack << ", id: " << id << 'x' << transitionId ;
3707 if (resource == "composite" && track == currentTrack && currentIn == oldStart && transitionId == id) {
3708 kDebug() << " / / / / /RESIZE TRANS TO: " << newStart << 'x' << newEnd;
3709 mlt_transition_set_in_and_out(tr, newStart, newEnd);
3712 nextservice = mlt_service_producer(nextservice);
3713 if (nextservice == NULL) break;
3714 properties = MLT_SERVICE_PROPERTIES(nextservice);
3715 mlt_type = mlt_properties_get(properties, "mlt_type");
3716 resource = mlt_properties_get(properties, "mlt_service");
3718 mlt_service_unlock(service.get_service());
3720 if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3724 void Render::mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id)
3726 Mlt::Service service(m_mltProducer->parent().get_service());
3727 Mlt::Tractor tractor(service);
3729 mlt_service_lock(service.get_service());
3730 m_mltConsumer->set("refresh", 0);
3733 mlt_service serv = m_mltProducer->parent().get_service();
3734 mlt_service nextservice = mlt_service_get_producer(serv);
3735 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3736 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3737 QString resource = mlt_properties_get(properties, "mlt_service");
3739 while (mlt_type == "transition") {
3740 mlt_transition tr = (mlt_transition) nextservice;
3741 int currentTrack = mlt_transition_get_b_track(tr);
3742 int currentaTrack = mlt_transition_get_a_track(tr);
3743 int currentIn = (int) mlt_transition_get_in(tr);
3744 int currentOut = (int) mlt_transition_get_out(tr);
3745 //mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3746 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3747 //kDebug()<<" + TRANSITION "<<id<<" == "<<transitionId<<", START TMIE: "<<currentIn<<", LOOK FR: "<<startTime<<", TRACK: "<<currentTrack<<'x'<<startTrack;
3748 if (resource == "composite" && transitionId == id && startTime == currentIn && startTrack == currentTrack) {
3749 kDebug() << "//////MOVING";
3750 mlt_transition_set_in_and_out(tr, endTime, endTime + currentOut - currentIn);
3751 if (endTrack != startTrack) {
3752 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3753 mlt_properties_set_int(properties, "a_track", currentaTrack + endTrack - currentTrack);
3754 mlt_properties_set_int(properties, "b_track", endTrack);
3758 nextservice = mlt_service_producer(nextservice);
3759 if (nextservice == NULL) break;
3760 properties = MLT_SERVICE_PROPERTIES(nextservice);
3761 mlt_type = mlt_properties_get(properties, "mlt_type");
3762 resource = mlt_properties_get(properties, "mlt_service");
3765 mlt_service_unlock(service.get_service());
3766 m_mltConsumer->set("refresh", 1);
3770 bool Render::mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool do_refresh)
3772 if (in >= out) return false;
3773 QMap<QString, QString> args = mltGetTransitionParamsFromXml(xml);
3774 Mlt::Service service(m_mltProducer->parent().get_service());
3776 Mlt::Tractor tractor(service);
3777 Mlt::Field *field = tractor.field();
3779 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, tag.toUtf8().constData());
3780 if (out != GenTime())
3781 transition->set_in_and_out((int) in.frames(m_fps), (int) out.frames(m_fps) - 1);
3783 if (do_refresh && (m_mltProducer->position() < in.frames(m_fps) || m_mltProducer->position() > out.frames(m_fps))) do_refresh = false;
3784 QMap<QString, QString>::Iterator it;
3786 if (xml.attribute("automatic") == "1") transition->set("automatic", 1);
3787 //kDebug() << " ------ ADDING TRANSITION PARAMs: " << args.count();
3788 if (xml.hasAttribute("id"))
3789 transition->set("kdenlive_id", xml.attribute("id").toUtf8().constData());
3791 for (it = args.begin(); it != args.end(); ++it) {
3793 if (!it.value().isEmpty())
3794 transition->set(key.toUtf8().constData(), it.value().toUtf8().constData());
3795 //kDebug() << " ------ ADDING TRANS PARAM: " << key << ": " << it.value();
3797 // attach transition
3799 mlt_service_lock(service.get_service());
3800 mltPlantTransition(field, *transition, a_track, b_track);
3801 // field->plant_transition(*transition, a_track, b_track);
3802 mlt_service_unlock(service.get_service());
3804 if (do_refresh) refresh();
3808 void Render::mltSavePlaylist()
3810 kWarning() << "// UPDATING PLAYLIST TO DISK++++++++++++++++";
3811 Mlt::Consumer fileConsumer(*m_mltProfile, "xml");
3812 fileConsumer.set("resource", "/tmp/playlist.mlt");
3814 Mlt::Service service(m_mltProducer->get_service());
3816 fileConsumer.connect(service);
3817 fileConsumer.start();
3820 const QList <Mlt::Producer *> Render::producersList()
3822 QList <Mlt::Producer *> prods;
3823 if (m_mltProducer == NULL) return prods;
3824 Mlt::Service service(m_mltProducer->parent().get_service());
3825 if (service.type() != tractor_type) return prods;
3826 Mlt::Tractor tractor(service);
3829 int trackNb = tractor.count();
3830 for (int t = 1; t < trackNb; t++) {
3831 Mlt::Producer *tt = tractor.track(t);
3832 Mlt::Producer trackProducer(tt);
3834 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3835 int clipNb = trackPlaylist.count();
3836 //kDebug() << "// PARSING SCENE TRACK: " << t << ", CLIPS: " << clipNb;
3837 for (int i = 0; i < clipNb; i++) {
3838 Mlt::Producer *c = trackPlaylist.get_clip(i);
3839 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
3841 if (!nprod->is_blank() && !ids.contains(nprod->get("id"))) {
3842 ids.append(nprod->get("id"));
3843 prods.append(nprod);
3844 } else delete nprod;
3852 void Render::fillSlowMotionProducers()
3854 if (m_mltProducer == NULL) return;
3855 Mlt::Service service(m_mltProducer->parent().get_service());
3856 if (service.type() != tractor_type) return;
3858 Mlt::Tractor tractor(service);
3860 int trackNb = tractor.count();
3861 for (int t = 1; t < trackNb; t++) {
3862 Mlt::Producer *tt = tractor.track(t);
3863 Mlt::Producer trackProducer(tt);
3865 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3866 int clipNb = trackPlaylist.count();
3867 for (int i = 0; i < clipNb; i++) {
3868 Mlt::Producer *c = trackPlaylist.get_clip(i);
3869 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
3871 QString id = nprod->get("id");
3872 if (id.startsWith("slowmotion:") && !nprod->is_blank()) {
3873 // this is a slowmotion producer, add it to the list
3874 QString url = QString::fromUtf8(nprod->get("resource"));
3875 int strobe = nprod->get_int("strobe");
3876 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
3877 if (!m_slowmotionProducers.contains(url)) {
3878 m_slowmotionProducers.insert(url, nprod);
3880 } else delete nprod;
3887 void Render::mltInsertTrack(int ix, bool videoTrack)
3892 Mlt::Service service(m_mltProducer->parent().get_service());
3893 mlt_service_lock(service.get_service());
3894 if (service.type() != tractor_type) {
3895 kWarning() << "// TRACTOR PROBLEM";
3899 Mlt::Tractor tractor(service);
3901 Mlt::Playlist playlist;
3902 int ct = tractor.count();
3904 kDebug() << "// ERROR, TRYING TO insert TRACK " << ix << ", max: " << ct;
3910 Mlt::Producer *prodToMove = new Mlt::Producer(tractor.track(pos));
3911 tractor.set_track(playlist, pos);
3912 Mlt::Producer newProd(tractor.track(pos));
3913 if (!videoTrack) newProd.set("hide", 1);
3915 for (; pos <= ct; pos++) {
3916 Mlt::Producer *prodToMove2 = new Mlt::Producer(tractor.track(pos));
3917 tractor.set_track(*prodToMove, pos);
3918 prodToMove = prodToMove2;
3921 tractor.set_track(playlist, ix);
3922 Mlt::Producer newProd(tractor.track(ix));
3923 if (!videoTrack) newProd.set("hide", 1);
3927 mlt_service serv = m_mltProducer->parent().get_service();
3928 mlt_service nextservice = mlt_service_get_producer(serv);
3929 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3930 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3931 QString resource = mlt_properties_get(properties, "mlt_service");
3933 while (mlt_type == "transition") {
3934 if (resource != "mix") {
3935 mlt_transition tr = (mlt_transition) nextservice;
3936 int currentTrack = mlt_transition_get_b_track(tr);
3937 int currentaTrack = mlt_transition_get_a_track(tr);
3938 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3940 if (currentTrack >= ix) {
3941 mlt_properties_set_int(properties, "b_track", currentTrack + 1);
3942 mlt_properties_set_int(properties, "a_track", currentaTrack + 1);
3945 nextservice = mlt_service_producer(nextservice);
3946 if (nextservice == NULL) break;
3947 properties = MLT_SERVICE_PROPERTIES(nextservice);
3948 mlt_type = mlt_properties_get(properties, "mlt_type");
3949 resource = mlt_properties_get(properties, "mlt_service");
3952 // Add audio mix transition to last track
3953 Mlt::Field *field = tractor.field();
3954 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "mix");
3955 transition->set("a_track", 1);
3956 transition->set("b_track", ct);
3957 transition->set("always_active", 1);
3958 transition->set("internal_added", 237);
3959 transition->set("combine", 1);
3960 field->plant_transition(*transition, 1, ct);
3961 //mlt_service_unlock(m_mltConsumer->get_service());
3962 mlt_service_unlock(service.get_service());
3963 //tractor.multitrack()->refresh();
3964 //tractor.refresh();
3966 blockSignals(false);
3970 void Render::mltDeleteTrack(int ix)
3973 doc.setContent(sceneList(), false);
3974 int tracksCount = doc.elementsByTagName("track").count() - 1;
3975 QDomNode track = doc.elementsByTagName("track").at(ix);
3976 QDomNode tractor = doc.elementsByTagName("tractor").at(0);
3977 QDomNodeList transitions = doc.elementsByTagName("transition");
3978 for (int i = 0; i < transitions.count(); i++) {
3979 QDomElement e = transitions.at(i).toElement();
3980 QDomNodeList props = e.elementsByTagName("property");
3981 QMap <QString, QString> mappedProps;
3982 for (int j = 0; j < props.count(); j++) {
3983 QDomElement f = props.at(j).toElement();
3984 mappedProps.insert(f.attribute("name"), f.firstChild().nodeValue());
3986 if (mappedProps.value("mlt_service") == "mix" && mappedProps.value("b_track").toInt() == tracksCount) {
3987 tractor.removeChild(transitions.at(i));
3989 } else if (mappedProps.value("mlt_service") != "mix" && (mappedProps.value("b_track").toInt() >= ix || mappedProps.value("a_track").toInt() >= ix)) {
3990 // Transition needs to be moved
3991 int a_track = mappedProps.value("a_track").toInt();
3992 int b_track = mappedProps.value("b_track").toInt();
3993 if (a_track > 0 && a_track >= ix) a_track --;
3994 if (b_track == ix) {
3995 // transition was on the deleted track, so remove it
3996 tractor.removeChild(transitions.at(i));
4000 if (b_track > 0 && b_track > ix) b_track --;
4001 for (int j = 0; j < props.count(); j++) {
4002 QDomElement f = props.at(j).toElement();
4003 if (f.attribute("name") == "a_track") f.firstChild().setNodeValue(QString::number(a_track));
4004 else if (f.attribute("name") == "b_track") f.firstChild().setNodeValue(QString::number(b_track));
4009 tractor.removeChild(track);
4010 //kDebug() << "/////////// RESULT SCENE: \n" << doc.toString();
4011 setSceneList(doc.toString(), m_framePosition);
4013 /* if (m_mltProducer != NULL) {
4014 Mlt::Producer parentProd(m_mltProducer->parent());
4015 if (parentProd.get_producer() != NULL) {
4016 Mlt::Service service(parentProd.get_service());
4017 if (service.type() == tractor_type) {
4018 Mlt::Tractor tractor(service);
4019 mltCheckLength(&tractor);
4026 void Render::updatePreviewSettings()
4028 kDebug() << "////// RESTARTING CONSUMER";
4029 if (!m_mltConsumer || !m_mltProducer) return;
4030 if (m_mltProducer->get_playtime() == 0) return;
4031 Mlt::Service service(m_mltProducer->parent().get_service());
4032 if (service.type() != tractor_type) return;
4034 //m_mltConsumer->set("refresh", 0);
4035 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
4036 m_mltConsumer->purge();
4037 QString scene = sceneList();
4039 if (m_mltProducer) {
4040 pos = m_mltProducer->position();
4043 setSceneList(scene, pos);
4047 QString Render::updateSceneListFps(double current_fps, double new_fps, QString scene)
4049 // Update all frame positions to the new fps value
4050 //WARNING: there are probably some effects or other that hold a frame value
4051 // as parameter and will also need to be updated here!
4053 doc.setContent(scene);
4055 double factor = new_fps / current_fps;
4056 QDomNodeList producers = doc.elementsByTagName("producer");
4057 for (int i = 0; i < producers.count(); i++) {
4058 QDomElement prod = producers.at(i).toElement();
4059 prod.removeAttribute("in");
4060 prod.removeAttribute("out");
4062 QDomNodeList props = prod.childNodes();
4063 for (int j = 0; j < props.count(); j++) {
4064 QDomElement param = props.at(j).toElement();
4065 QString paramName = param.attribute("name");
4066 if (paramName.startsWith("meta.") || paramName == "length") {
4067 prod.removeChild(props.at(j));
4073 QDomNodeList entries = doc.elementsByTagName("entry");
4074 for (int i = 0; i < entries.count(); i++) {
4075 QDomElement entry = entries.at(i).toElement();
4076 int in = entry.attribute("in").toInt();
4077 int out = entry.attribute("out").toInt();
4078 in = factor * in + 0.5;
4079 out = factor * out + 0.5;
4080 entry.setAttribute("in", in);
4081 entry.setAttribute("out", out);
4084 QDomNodeList blanks = doc.elementsByTagName("blank");
4085 for (int i = 0; i < blanks.count(); i++) {
4086 QDomElement blank = blanks.at(i).toElement();
4087 int length = blank.attribute("length").toInt();
4088 length = factor * length + 0.5;
4089 blank.setAttribute("length", QString::number(length));
4092 QDomNodeList filters = doc.elementsByTagName("filter");
4093 for (int i = 0; i < filters.count(); i++) {
4094 QDomElement filter = filters.at(i).toElement();
4095 int in = filter.attribute("in").toInt();
4096 int out = filter.attribute("out").toInt();
4097 in = factor * in + 0.5;
4098 out = factor * out + 0.5;
4099 filter.setAttribute("in", in);
4100 filter.setAttribute("out", out);
4103 QDomNodeList transitions = doc.elementsByTagName("transition");
4104 for (int i = 0; i < transitions.count(); i++) {
4105 QDomElement transition = transitions.at(i).toElement();
4106 int in = transition.attribute("in").toInt();
4107 int out = transition.attribute("out").toInt();
4108 in = factor * in + 0.5;
4109 out = factor * out + 0.5;
4110 transition.setAttribute("in", in);
4111 transition.setAttribute("out", out);
4112 QDomNodeList props = transition.childNodes();
4113 for (int j = 0; j < props.count(); j++) {
4114 QDomElement param = props.at(j).toElement();
4115 QString paramName = param.attribute("name");
4116 if (paramName == "geometry") {
4117 QString geom = param.firstChild().nodeValue();
4118 QStringList keys = geom.split(';');
4119 QStringList newKeys;
4120 for (int k = 0; k < keys.size(); ++k) {
4121 if (keys.at(k).contains('=')) {
4122 int pos = keys.at(k).section('=', 0, 0).toInt();
4123 pos = factor * pos + 0.5;
4124 newKeys.append(QString::number(pos) + '=' + keys.at(k).section('=', 1));
4125 } else newKeys.append(keys.at(k));
4127 param.firstChild().setNodeValue(newKeys.join(";"));
4131 QDomElement tractor = doc.elementsByTagName("tractor").at(0).toElement();
4132 int out = tractor.attribute("out").toInt();
4133 out = factor * out + 0.5;
4134 tractor.setAttribute("out", out);
4135 emit durationChanged(out);
4137 //kDebug() << "///////////////////////////// " << out << " \n" << doc.toString() << "\n-------------------------";
4138 return doc.toString();
4142 void Render::sendFrameUpdate()
4144 if (m_mltProducer) {
4145 Mlt::Frame * frame = m_mltProducer->get_frame();
4146 emitFrameUpdated(*frame);
4152 #include "renderer.moc"