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 && mlt_frame_get_position(frame_ptr) <= 0) {
76 self->emitConsumerStopped();
81 static void consumer_paused(mlt_consumer, Render * self, mlt_frame /*frame_ptr*/)
83 // detect if the producer has finished playing. Is there a better way to do it?
84 if (self->m_isBlocked) return;
85 self->emitConsumerStopped();
89 static void consumer_gl_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr)
91 // detect if the producer has finished playing. Is there a better way to do it?
92 if (self->m_isBlocked) return;
93 Mlt::Frame frame(frame_ptr);
94 self->showFrame(frame);
95 if (frame.get_double("_speed") == 0.0) {
96 self->emitConsumerStopped();
97 } else if (frame.get_double("_speed") < 0.0 && mlt_frame_get_position(frame_ptr) <= 0) {
99 self->emitConsumerStopped();
103 Render::Render(const QString & rendererName, int winid, QString profile, QWidget *parent) :
104 AbstractRender(rendererName, parent),
106 analyseAudio(KdenliveSettings::monitor_audio()),
107 m_name(rendererName),
112 m_externalConsumer(false),
115 m_isSplitView(false),
119 if (profile.isEmpty()) profile = KdenliveSettings::current_profile();
120 buildConsumer(profile);
122 m_mltProducer = m_blackClip->cut(0, 50);
123 m_mltConsumer->connect(*m_mltProducer);
124 m_mltProducer->set_speed(0.0);
135 void Render::closeMlt()
139 Mlt::Service service(m_mltProducer->parent().get_service());
140 mlt_service_lock(service.get_service());
142 if (service.type() == tractor_type) {
143 Mlt::Tractor tractor(service);
144 Mlt::Field *field = tractor.field();
145 mlt_service nextservice = mlt_service_get_producer(service.get_service());
146 mlt_service nextservicetodisconnect;
147 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
148 QString mlt_type = mlt_properties_get(properties, "mlt_type");
149 QString resource = mlt_properties_get(properties, "mlt_service");
150 // Delete all transitions
151 while (mlt_type == "transition") {
152 nextservicetodisconnect = nextservice;
153 nextservice = mlt_service_producer(nextservice);
154 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
155 if (nextservice == NULL) break;
156 properties = MLT_SERVICE_PROPERTIES(nextservice);
157 mlt_type = mlt_properties_get(properties, "mlt_type");
158 resource = mlt_properties_get(properties, "mlt_service");
161 for (int trackNb = tractor.count() - 1; trackNb >= 0; --trackNb) {
162 Mlt::Producer trackProducer(tractor.track(trackNb));
163 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
164 if (trackPlaylist.type() == playlist_type) trackPlaylist.clear();
169 mlt_service_unlock(service.get_service());
172 kDebug() << "// // // CLOSE RENDERER " << m_name;
173 delete m_mltConsumer;
174 delete m_mltProducer;
179 void Render::slotSwitchFullscreen()
181 if (m_mltConsumer) m_mltConsumer->set("full_screen", 1);
184 void Render::buildConsumer(const QString profileName)
189 //TODO: uncomment following line when everything is clean
190 // uncommented Feb 2011 --Granjow
191 if (m_mltProfile) delete m_mltProfile;
192 m_activeProfile = profileName;
193 char *tmp = qstrdup(m_activeProfile.toUtf8().constData());
194 setenv("MLT_PROFILE", tmp, 1);
195 m_mltProfile = new Mlt::Profile(tmp);
196 m_mltProfile->set_explicit(true);
199 m_blackClip = new Mlt::Producer(*m_mltProfile, "colour", "black");
200 m_blackClip->set("id", "black");
201 m_blackClip->set("mlt_type", "producer");
203 if (KdenliveSettings::external_display() && m_name != "clip") {
204 // Use blackmagic card for video output
205 QMap< QString, QString > profileProperties = ProfilesDialog::getSettingsFromFile(profileName);
206 int device = KdenliveSettings::blackmagic_output_device();
208 if (BMInterface::isSupportedProfile(device, profileProperties)) {
209 QString decklink = "decklink:" + QString::number(KdenliveSettings::blackmagic_output_device());
210 tmp = qstrdup(decklink.toUtf8().constData());
211 m_mltConsumer = new Mlt::Consumer(*m_mltProfile, tmp);
213 if (m_mltConsumer->is_valid()) {
214 m_externalConsumer = true;
215 m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_show);
216 m_mltConsumer->set("terminate_on_pause", 0);
217 m_mltConsumer->set("buffer", 12);
218 m_mltConsumer->set("deinterlace_method", "onefield");
219 m_mltConsumer->set("real_time", KdenliveSettings::mltthreads());
220 mlt_log_set_callback(kdenlive_callback);
222 if (m_mltConsumer && m_mltConsumer->is_valid()) return;
223 } 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()));
226 m_externalConsumer = false;
227 QString videoDriver = KdenliveSettings::videodrivername();
228 if (!videoDriver.isEmpty()) {
229 if (videoDriver == "x11_noaccel") {
230 setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
233 unsetenv("SDL_VIDEO_YUV_HWACCEL");
236 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
238 //m_mltConsumer->set("fullscreen", 1);
241 m_mltConsumer = new Mlt::Consumer(*m_mltProfile, "sdl_audio");
242 m_mltConsumer->set("preview_off", 1);
243 m_mltConsumer->set("preview_format", mlt_image_rgb24a);
244 m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_gl_frame_show);
246 m_mltConsumer = new Mlt::Consumer(*m_mltProfile, "sdl_preview");
247 // FIXME: the event object returned by the listen gets leaked...
248 m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_show);
249 m_mltConsumer->listen("consumer-sdl-paused", this, (mlt_listener) consumer_paused);
250 m_mltConsumer->set("window_id", m_winid);
252 m_mltConsumer->set("resize", 1);
253 m_mltConsumer->set("terminate_on_pause", 1);
254 m_mltConsumer->set("window_background", KdenliveSettings::window_background().name().toUtf8().constData());
255 m_mltConsumer->set("rescale", "nearest");
256 mlt_log_set_callback(kdenlive_callback);
258 QString audioDevice = KdenliveSettings::audiodevicename();
259 if (!audioDevice.isEmpty())
260 m_mltConsumer->set("audio_device", audioDevice.toUtf8().constData());
262 if (!videoDriver.isEmpty())
263 m_mltConsumer->set("video_driver", videoDriver.toUtf8().constData());
265 QString audioDriver = KdenliveSettings::audiodrivername();
268 // Disabled because the "auto" detected driver was sometimes wrong
269 if (audioDriver.isEmpty())
270 audioDriver = KdenliveSettings::autoaudiodrivername();
273 if (!audioDriver.isEmpty())
274 m_mltConsumer->set("audio_driver", audioDriver.toUtf8().constData());
276 m_mltConsumer->set("progressive", 1);
277 m_mltConsumer->set("audio_buffer", 1024);
278 m_mltConsumer->set("frequency", 48000);
279 m_mltConsumer->set("real_time", KdenliveSettings::mltthreads());
282 Mlt::Producer *Render::invalidProducer(const QString &id)
285 QString txt = "+" + i18n("Missing clip") + ".txt";
286 char *tmp = qstrdup(txt.toUtf8().constData());
287 clip = new Mlt::Producer(*m_mltProfile, tmp);
289 if (clip == NULL) clip = new Mlt::Producer(*m_mltProfile, "colour", "red");
291 clip->set("bgcolour", "0xff0000ff");
292 clip->set("pad", "10");
294 clip->set("id", id.toUtf8().constData());
295 clip->set("mlt_type", "producer");
299 int Render::resetProfile(const QString profileName, bool dropSceneList)
302 if (!dropSceneList) scene = sceneList();
304 if (m_externalConsumer == KdenliveSettings::external_display()) {
305 if (KdenliveSettings::external_display() && m_activeProfile == profileName) return 1;
306 QString videoDriver = KdenliveSettings::videodrivername();
307 QString currentDriver = m_mltConsumer->get("video_driver");
308 if (getenv("SDL_VIDEO_YUV_HWACCEL") != NULL && currentDriver == "x11") currentDriver = "x11_noaccel";
309 QString background = KdenliveSettings::window_background().name();
310 QString currentBackground = m_mltConsumer->get("window_background");
311 if (m_activeProfile == profileName && currentDriver == videoDriver && background == currentBackground) {
312 kDebug() << "reset to same profile, nothing to do";
317 if (m_isSplitView) slotSplitView(false);
318 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
319 m_mltConsumer->purge();
320 delete m_mltConsumer;
321 m_mltConsumer = NULL;
324 double current_fps = m_mltProfile->fps();
325 double current_dar = m_mltProfile->dar();
330 pos = m_mltProducer->position();
332 Mlt::Service service(m_mltProducer->get_service());
333 if (service.type() == tractor_type) {
334 Mlt::Tractor tractor(service);
335 for (int trackNb = tractor.count() - 1; trackNb >= 0; --trackNb) {
336 Mlt::Producer trackProducer(tractor.track(trackNb));
337 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
338 trackPlaylist.clear();
342 delete m_mltProducer;
344 m_mltProducer = NULL;
345 buildConsumer(profileName);
346 double new_fps = m_mltProfile->fps();
347 double new_dar = m_mltProfile->dar();
349 if (!dropSceneList) {
350 // We need to recover our playlist
351 if (current_fps != new_fps) {
352 // fps changed, we must update the scenelist positions
353 scene = updateSceneListFps(current_fps, new_fps, scene);
355 setSceneList(scene, pos);
356 // producers have changed (different profile), so reset them...
357 emit refreshDocumentProducers(new_dar != current_dar, current_fps != new_fps);
362 void Render::seek(GenTime time)
367 m_mltProducer->seek((int)(time.frames(m_fps)));
371 void Render::seek(int time)
376 m_mltProducer->seek(time);
381 /*QPixmap Render::frameThumbnail(Mlt::Frame *frame, int width, int height, bool border) {
382 QPixmap pix(width, height);
384 mlt_image_format format = mlt_image_rgb24a;
385 uint8_t *thumb = frame->get_image(format, width, height);
386 QImage image(thumb, width, height, QImage::Format_ARGB32);
388 if (!image.isNull()) {
389 pix = pix.fromImage(image);
391 QPainter painter(&pix);
392 painter.drawRect(0, 0, width - 1, height - 1);
394 } else pix.fill(Qt::black);
398 int Render::frameRenderWidth() const
400 return m_mltProfile->width();
403 int Render::renderWidth() const
405 return (int)(m_mltProfile->height() * m_mltProfile->dar() + 0.5);
408 int Render::renderHeight() const
410 return m_mltProfile->height();
413 QImage Render::extractFrame(int frame_position, QString path, int width, int height)
416 width = renderWidth();
417 height = renderHeight();
418 } else if (width % 2 == 1) width++;
420 if (!path.isEmpty()) {
421 Mlt::Producer *producer = new Mlt::Producer(*m_mltProfile, path.toUtf8().constData());
423 if (producer->is_valid()) {
424 QImage img = KThumb::getFrame(producer, frame_position, width, height);
428 else delete producer;
432 if (!m_mltProducer || !path.isEmpty()) {
433 QImage pix(width, height, QImage::Format_RGB32);
437 return KThumb::getFrame(m_mltProducer, frame_position, width, height);
440 QPixmap Render::getImageThumbnail(KUrl url, int /*width*/, int /*height*/)
444 if (url.fileName().startsWith(".all.")) { // check for slideshow
445 QString fileType = url.fileName().right(3);
447 QStringList::Iterator it;
449 QDir dir(url.directory());
451 filter << "*." + fileType;
452 filter << "*." + fileType.toUpper();
453 more = dir.entryList(filter, QDir::Files);
454 im.load(url.directory() + '/' + more.at(0));
455 } else im.load(url.path());
456 //pixmap = im.scaled(width, height);
460 double Render::consumerRatio() const
462 if (!m_mltConsumer) return 1.0;
463 return (m_mltConsumer->get_double("aspect_ratio_num") / m_mltConsumer->get_double("aspect_ratio_den"));
467 int Render::getLength()
471 // kDebug()<<"////// LENGTH: "<<mlt_producer_get_playtime(m_mltProducer->get_producer());
472 return mlt_producer_get_playtime(m_mltProducer->get_producer());
477 bool Render::isValid(KUrl url)
479 Mlt::Producer producer(*m_mltProfile, url.path().toUtf8().constData());
480 if (producer.is_blank())
486 double Render::dar() const
488 return m_mltProfile->dar();
491 double Render::sar() const
493 return m_mltProfile->sar();
496 void Render::slotSplitView(bool doit)
498 m_isSplitView = doit;
499 Mlt::Service service(m_mltProducer->parent().get_service());
500 Mlt::Tractor tractor(service);
501 if (service.type() != tractor_type || tractor.count() < 2) return;
502 Mlt::Field *field = tractor.field();
504 for (int i = 1, screen = 0; i < tractor.count() && screen < 4; i++) {
505 Mlt::Producer trackProducer(tractor.track(i));
506 kDebug() << "// TRACK: " << i << ", HIDE: " << trackProducer.get("hide");
507 if (QString(trackProducer.get("hide")).toInt() != 1) {
508 kDebug() << "// ADIDNG TRACK: " << i;
509 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
510 transition->set("mlt_service", "composite");
511 transition->set("a_track", 0);
512 transition->set("b_track", i);
513 transition->set("distort", 1);
514 transition->set("internal_added", "200");
521 tmp = "50%,0:50%x50%";
524 tmp = "0,50%:50%x50%";
528 tmp = "50%,50%:50%x50%";
531 transition->set("geometry", tmp);
532 transition->set("always_active", "1");
533 field->plant_transition(*transition, 0, i);
538 m_mltConsumer->set("refresh", 1);
540 mlt_service serv = m_mltProducer->parent().get_service();
541 mlt_service nextservice = mlt_service_get_producer(serv);
542 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
543 QString mlt_type = mlt_properties_get(properties, "mlt_type");
544 QString resource = mlt_properties_get(properties, "mlt_service");
546 while (mlt_type == "transition") {
547 QString added = mlt_properties_get(MLT_SERVICE_PROPERTIES(nextservice), "internal_added");
548 if (added == "200") {
549 mlt_field_disconnect_service(field->get_field(), nextservice);
551 nextservice = mlt_service_producer(nextservice);
552 if (nextservice == NULL) break;
553 properties = MLT_SERVICE_PROPERTIES(nextservice);
554 mlt_type = mlt_properties_get(properties, "mlt_type");
555 resource = mlt_properties_get(properties, "mlt_service");
556 m_mltConsumer->set("refresh", 1);
561 void Render::getFileProperties(const QDomElement xml, const QString &clipId, int imageHeight, bool replaceProducer, bool selectClip)
565 if (xml.hasAttribute("proxy") && xml.attribute("proxy") != "-") {
566 path = xml.attribute("proxy");
567 proxyProducer = true;
570 path = xml.attribute("resource");
571 proxyProducer = false;
574 KUrl url = KUrl(path);
575 Mlt::Producer *producer = NULL;
576 CLIPTYPE type = (CLIPTYPE)xml.attribute("type").toInt();
577 //kDebug() << "PROFILE WIDT: "<< xml.attribute("mlt_service") << ": "<< m_mltProfile->width() << "\n...................\n\n";
578 /*if (xml.attribute("type").toInt() == TEXT && !QFile::exists(url.path())) {
579 emit replyGetFileProperties(clipId, producer, QMap < QString, QString >(), QMap < QString, QString >(), replaceProducer);
584 producer = new Mlt::Producer(*m_mltProfile, 0, ("colour:" + xml.attribute("colour")).toUtf8().constData());
585 } else if (type == TEXT) {
586 producer = new Mlt::Producer(*m_mltProfile, 0, ("kdenlivetitle:" + xml.attribute("resource")).toUtf8().constData());
587 if (producer && producer->is_valid() && xml.hasAttribute("xmldata"))
588 producer->set("xmldata", xml.attribute("xmldata").toUtf8().constData());
589 } else if (url.isEmpty()) {
591 QDomElement mlt = doc.createElement("mlt");
592 QDomElement play = doc.createElement("playlist");
593 doc.appendChild(mlt);
594 mlt.appendChild(play);
595 play.appendChild(doc.importNode(xml, true));
596 producer = new Mlt::Producer(*m_mltProfile, "xml-string", doc.toString().toUtf8().constData());
598 producer = new Mlt::Producer(*m_mltProfile, url.path().toUtf8().constData());
602 if (producer == NULL || producer->is_blank() || !producer->is_valid()) {
603 kDebug() << " / / / / / / / / ERROR / / / / // CANNOT LOAD PRODUCER: ";
605 // Proxy file is corrupted
606 emit removeInvalidProxy(clipId, false);
608 else emit removeInvalidClip(clipId, replaceProducer);
613 if (proxyProducer && xml.hasAttribute("proxy_out")) {
614 producer->set("length", xml.attribute("proxy_out").toInt() + 1);
615 producer->set("out", xml.attribute("proxy_out").toInt());
616 if (producer->get_out() != xml.attribute("proxy_out").toInt()) {
617 // Proxy file length is different than original clip length, this will corrupt project so disable this proxy clip
618 emit removeInvalidProxy(clipId, true);
624 if (xml.hasAttribute("force_aspect_ratio")) {
625 double aspect = xml.attribute("force_aspect_ratio").toDouble();
626 if (aspect > 0) producer->set("force_aspect_ratio", aspect);
629 if (xml.hasAttribute("force_aspect_num") && xml.hasAttribute("force_aspect_den")) {
630 int width = xml.attribute("frame_size").section('x', 0, 0).toInt();
631 int height = xml.attribute("frame_size").section('x', 1, 1).toInt();
632 int aspectNumerator = xml.attribute("force_aspect_num").toInt();
633 int aspectDenominator = xml.attribute("force_aspect_den").toInt();
634 if (aspectDenominator != 0 && width != 0)
635 producer->set("force_aspect_ratio", double(height) * aspectNumerator / aspectDenominator / width);
638 if (xml.hasAttribute("force_fps")) {
639 double fps = xml.attribute("force_fps").toDouble();
640 if (fps > 0) producer->set("force_fps", fps);
643 if (xml.hasAttribute("force_progressive")) {
645 int progressive = xml.attribute("force_progressive").toInt(&ok);
646 if (ok) producer->set("force_progressive", progressive);
648 if (xml.hasAttribute("force_tff")) {
650 int fieldOrder = xml.attribute("force_tff").toInt(&ok);
651 if (ok) producer->set("force_tff", fieldOrder);
653 if (xml.hasAttribute("threads")) {
654 int threads = xml.attribute("threads").toInt();
655 if (threads != 1) producer->set("threads", threads);
657 if (xml.hasAttribute("video_index")) {
658 int vindex = xml.attribute("video_index").toInt();
659 if (vindex != 0) producer->set("video_index", vindex);
661 if (xml.hasAttribute("audio_index")) {
662 int aindex = xml.attribute("audio_index").toInt();
663 if (aindex != 0) producer->set("audio_index", aindex);
665 if (xml.hasAttribute("force_colorspace")) {
666 int colorspace = xml.attribute("force_colorspace").toInt();
667 if (colorspace != 0) producer->set("force_colorspace", colorspace);
669 if (xml.hasAttribute("full_luma")) {
670 int full_luma = xml.attribute("full_luma").toInt();
671 if (full_luma != 0) producer->set("set.force_full_luma", full_luma);
676 if (xml.hasAttribute("out")) clipOut = xml.attribute("out").toInt();
678 // setup length here as otherwise default length (currently 15000 frames in MLT) will be taken even if outpoint is larger
679 if (type == COLOR || type == TEXT || type == IMAGE || type == SLIDESHOW) {
681 if (xml.hasAttribute("length")) {
682 if (clipOut > 0) duration = clipOut + 1;
683 length = xml.attribute("length").toInt();
684 clipOut = length - 1;
686 else length = xml.attribute("out").toInt() - xml.attribute("in").toInt();
687 producer->set("length", length);
690 if (clipOut > 0) producer->set_in_and_out(xml.attribute("in").toInt(), clipOut);
692 producer->set("id", clipId.toUtf8().constData());
694 if (xml.hasAttribute("templatetext"))
695 producer->set("templatetext", xml.attribute("templatetext").toUtf8().constData());
697 if ((!replaceProducer && xml.hasAttribute("file_hash")) || xml.hasAttribute("proxy")) {
698 // Clip already has all properties
699 emit replyGetFileProperties(clipId, producer, QMap < QString, QString >(), QMap < QString, QString >(), replaceProducer, selectClip);
703 int width = (int)(imageHeight * m_mltProfile->dar() + 0.5);
704 QMap < QString, QString > filePropertyMap;
705 QMap < QString, QString > metadataPropertyMap;
707 int frameNumber = xml.attribute("thumbnail", "0").toInt();
708 if (frameNumber != 0) producer->seek(frameNumber);
710 filePropertyMap["duration"] = QString::number(duration > 0 ? duration : producer->get_playtime());
711 //kDebug() << "/////// PRODUCER: " << url.path() << " IS: " << producer->get_playtime();
713 Mlt::Frame *frame = producer->get_frame();
715 if (type == SLIDESHOW) {
716 int ttl = xml.hasAttribute("ttl") ? xml.attribute("ttl").toInt() : 0;
717 if (ttl) producer->set("ttl", ttl);
718 if (!xml.attribute("animation").isEmpty()) {
719 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "affine");
720 if (filter && filter->is_valid()) {
722 QString geometry = SlideshowClip::animationToGeometry(xml.attribute("animation"), cycle);
723 if (!geometry.isEmpty()) {
724 if (xml.attribute("animation").contains("low-pass")) {
725 Mlt::Filter *blur = new Mlt::Filter(*m_mltProfile, "boxblur");
726 if (blur && blur->is_valid())
727 producer->attach(*blur);
729 filter->set("transition.geometry", geometry.toUtf8().data());
730 filter->set("transition.cycle", cycle);
731 producer->attach(*filter);
735 if (xml.attribute("fade") == "1") {
736 // user wants a fade effect to slideshow
737 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "luma");
738 if (filter && filter->is_valid()) {
739 if (ttl) filter->set("cycle", ttl);
740 if (xml.hasAttribute("luma_duration") && !xml.attribute("luma_duration").isEmpty()) filter->set("duration", xml.attribute("luma_duration").toInt());
741 if (xml.hasAttribute("luma_file") && !xml.attribute("luma_file").isEmpty()) {
742 filter->set("luma.resource", xml.attribute("luma_file").toUtf8().constData());
743 if (xml.hasAttribute("softness")) {
744 int soft = xml.attribute("softness").toInt();
745 filter->set("luma.softness", (double) soft / 100.0);
748 producer->attach(*filter);
751 if (xml.attribute("crop") == "1") {
752 // user wants to center crop the slides
753 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "crop");
754 if (filter && filter->is_valid()) {
755 filter->set("center", 1);
756 producer->attach(*filter);
761 if (producer->get_double("meta.media.frame_rate_den") > 0) {
762 filePropertyMap["fps"] = QString::number(producer->get_double("meta.media.frame_rate_num") / producer->get_double("meta.media.frame_rate_den"));
763 } else filePropertyMap["fps"] = producer->get("source_fps");
765 if (frame && frame->is_valid()) {
766 filePropertyMap["frame_size"] = QString::number(frame->get_int("width")) + 'x' + QString::number(frame->get_int("height"));
767 filePropertyMap["frequency"] = QString::number(frame->get_int("frequency"));
768 filePropertyMap["channels"] = QString::number(frame->get_int("channels"));
769 filePropertyMap["aspect_ratio"] = frame->get("aspect_ratio");
771 if (frame->get_int("test_image") == 0) {
772 if (url.path().endsWith(".mlt") || url.path().endsWith(".westley") || url.path().endsWith(".kdenlive")) {
773 filePropertyMap["type"] = "playlist";
774 metadataPropertyMap["comment"] = QString::fromUtf8(producer->get("title"));
775 } else if (frame->get_int("test_audio") == 0)
776 filePropertyMap["type"] = "av";
778 filePropertyMap["type"] = "video";
781 mlt_image_format format = mlt_image_rgb24a;
782 int frame_width = width;
783 int frame_height = imageHeight;
787 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
788 QImage image((uchar *)data, frame_width, frame_height, QImage::Format_ARGB32_Premultiplied);
790 if (!image.isNull()) {
791 if (frame_width > (2 * width)) {
792 // there was a scaling problem, do it manually
793 QImage scaled = image.scaled(width, imageHeight);
794 pix = QPixmap::fromImage(scaled.rgbSwapped());
795 } else pix = QPixmap::fromImage(image.rgbSwapped());
796 variance = KThumb::imageVariance(image);
800 if (frameNumber == 0 && variance < 6) {
801 // Thumbnail is not interesting (for example all black, seek to fetch better thumb
803 producer->seek(frameNumber);
805 frame = producer->get_frame();
808 } while (variance == -1);
809 emit replyGetImage(clipId, pix);
811 } else if (frame->get_int("test_audio") == 0) {
812 QPixmap pixmap = KIcon("audio-x-generic").pixmap(QSize(width, imageHeight));
813 emit replyGetImage(clipId, pixmap);
814 filePropertyMap["type"] = "audio";
818 // Retrieve audio / video codec name
822 if (producer->get_int("video_index") > -1) {
823 /*if (context->duration == AV_NOPTS_VALUE) {
824 kDebug() << " / / / / / / / /ERROR / / / CLIP HAS UNKNOWN DURATION";
825 emit removeInvalidClip(clipId);
829 // Get the video_index
830 int default_video = producer->get_int("video_index");
832 int default_audio = producer->get_int("audio_index");
835 // Find maximum stream index values
836 for (int ix = 0; ix < producer->get_int("meta.media.nb_streams"); ix++) {
837 snprintf(property, sizeof(property), "meta.media.%d.stream.type", ix);
838 QString type = producer->get(property);
841 else if (type == "audio")
844 filePropertyMap["default_video"] = QString::number(default_video);
845 filePropertyMap["video_max"] = QString::number(video_max);
846 filePropertyMap["default_audio"] = QString::number(default_audio);
847 filePropertyMap["audio_max"] = QString::number(audio_max);
849 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", default_video);
850 if (producer->get(property)) {
851 filePropertyMap["videocodec"] = producer->get(property);
853 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
854 if (producer->get(property))
855 filePropertyMap["videocodec"] = producer->get(property);
858 query = QString("meta.media.%1.codec.pix_fmt").arg(default_video);
859 filePropertyMap["pix_fmt"] = producer->get(query.toUtf8().constData());
860 filePropertyMap["colorspace"] = producer->get("meta.media.colorspace");
862 } else kDebug() << " / / / / /WARNING, VIDEO CONTEXT IS NULL!!!!!!!!!!!!!!";
863 if (producer->get_int("audio_index") > -1) {
864 // Get the audio_index
865 int index = producer->get_int("audio_index");
867 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", index);
868 if (producer->get(property)) {
869 filePropertyMap["audiocodec"] = producer->get(property);
871 snprintf(property, sizeof(property), "meta.media.%d.codec.name", index);
872 if (producer->get(property))
873 filePropertyMap["audiocodec"] = producer->get(property);
878 Mlt::Properties metadata;
879 metadata.pass_values(*producer, "meta.attr.");
880 int count = metadata.count();
881 for (int i = 0; i < count; i ++) {
882 QString name = metadata.get_name(i);
883 QString value = QString::fromUtf8(metadata.get(i));
884 if (name.endsWith("markup") && !value.isEmpty())
885 metadataPropertyMap[ name.section('.', 0, -2)] = value;
888 emit replyGetFileProperties(clipId, producer, filePropertyMap, metadataPropertyMap, replaceProducer, selectClip);
889 // FIXME: should delete this to avoid a leak...
895 /** Create the producer from the MLT XML QDomDocument */
896 void Render::initSceneList()
898 kDebug() << "-------- INIT SCENE LIST ------_";
900 QDomElement mlt = doc.createElement("mlt");
901 doc.appendChild(mlt);
902 QDomElement prod = doc.createElement("producer");
903 prod.setAttribute("resource", "colour");
904 prod.setAttribute("colour", "red");
905 prod.setAttribute("id", "black");
906 prod.setAttribute("in", "0");
907 prod.setAttribute("out", "0");
909 QDomElement tractor = doc.createElement("tractor");
910 QDomElement multitrack = doc.createElement("multitrack");
912 QDomElement playlist1 = doc.createElement("playlist");
913 playlist1.appendChild(prod);
914 multitrack.appendChild(playlist1);
915 QDomElement playlist2 = doc.createElement("playlist");
916 multitrack.appendChild(playlist2);
917 QDomElement playlist3 = doc.createElement("playlist");
918 multitrack.appendChild(playlist3);
919 QDomElement playlist4 = doc.createElement("playlist");
920 multitrack.appendChild(playlist4);
921 QDomElement playlist5 = doc.createElement("playlist");
922 multitrack.appendChild(playlist5);
923 tractor.appendChild(multitrack);
924 mlt.appendChild(tractor);
925 // kDebug()<<doc.toString();
927 QString tmp = QString("<mlt><producer resource=\"colour\" colour=\"red\" id=\"red\" /><tractor><multitrack><playlist></playlist><playlist></playlist><playlist /><playlist /><playlist></playlist></multitrack></tractor></mlt>");*/
928 setSceneList(doc, 0);
932 int Render::setProducer(Mlt::Producer *producer, int position)
934 if (m_winid == -1) return -1;
937 m_mltConsumer->stop();
940 m_mltConsumer->purge();
943 m_mltProducer->set_speed(0);
944 delete m_mltProducer;
945 m_mltProducer = NULL;
949 m_mltProducer = new Mlt::Producer(producer->get_producer());
950 } else m_mltProducer = m_blackClip->cut(0, 50);
952 if (!m_mltProducer || !m_mltProducer->is_valid()) {
953 kDebug() << " WARNING - - - - -INVALID PLAYLIST: ";
956 int volume = KdenliveSettings::volume();
957 m_mltProducer->set("meta.volume", (double)volume / 100);
958 m_fps = m_mltProducer->get_fps();
959 int error = connectPlaylist();
961 if (position != -1) {
962 m_mltProducer->seek(position);
963 emit rendererPosition(position);
964 } else emit rendererPosition((int) m_mltProducer->position());
969 int Render::setSceneList(QDomDocument list, int position)
971 return setSceneList(list.toString(), position);
974 int Render::setSceneList(QString playlist, int position)
976 if (m_winid == -1) return -1;
980 //kDebug() << "////// RENDER, SET SCENE LIST: " << playlist;
982 // Remove previous profile info
984 doc.setContent(playlist);
985 QDomElement profile = doc.documentElement().firstChildElement("profile");
986 doc.documentElement().removeChild(profile);
987 playlist = doc.toString();
990 if (!m_mltConsumer->is_stopped()) {
991 m_mltConsumer->stop();
993 m_mltConsumer->set("refresh", 0);
995 kWarning() << "/////// ERROR, TRYING TO USE NULL MLT CONSUMER";
1000 m_mltProducer->set_speed(0);
1001 //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
1003 Mlt::Service service(m_mltProducer->parent().get_service());
1004 mlt_service_lock(service.get_service());
1006 if (service.type() == tractor_type) {
1007 Mlt::Tractor tractor(service);
1008 Mlt::Field *field = tractor.field();
1009 mlt_service nextservice = mlt_service_get_producer(service.get_service());
1010 mlt_service nextservicetodisconnect;
1011 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
1012 QString mlt_type = mlt_properties_get(properties, "mlt_type");
1013 QString resource = mlt_properties_get(properties, "mlt_service");
1014 // Delete all transitions
1015 while (mlt_type == "transition") {
1016 nextservicetodisconnect = nextservice;
1017 nextservice = mlt_service_producer(nextservice);
1018 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
1019 if (nextservice == NULL) break;
1020 properties = MLT_SERVICE_PROPERTIES(nextservice);
1021 mlt_type = mlt_properties_get(properties, "mlt_type");
1022 resource = mlt_properties_get(properties, "mlt_service");
1026 for (int trackNb = tractor.count() - 1; trackNb >= 0; --trackNb) {
1027 Mlt::Producer trackProducer(tractor.track(trackNb));
1028 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1029 if (trackPlaylist.type() == playlist_type) trackPlaylist.clear();
1033 mlt_service_unlock(service.get_service());
1035 qDeleteAll(m_slowmotionProducers.values());
1036 m_slowmotionProducers.clear();
1038 delete m_mltProducer;
1039 m_mltProducer = NULL;
1044 // TODO: Better way to do this
1045 if (KdenliveSettings::projectloading_avformatnovalidate())
1046 playlist.replace(">avformat</property>", ">avformat-novalidate</property>");
1048 playlist.replace(">avformat-novalidate</property>", ">avformat</property>");
1049 m_mltProducer = new Mlt::Producer(*m_mltProfile, "xml-string", playlist.toUtf8().constData());
1050 if (!m_mltProducer || !m_mltProducer->is_valid()) {
1051 kDebug() << " WARNING - - - - -INVALID PLAYLIST: " << playlist.toUtf8().constData();
1052 m_mltProducer = m_blackClip->cut(0, 50);
1055 int volume = KdenliveSettings::volume();
1056 m_mltProducer->set("meta.volume", (double)volume / 100);
1057 m_mltProducer->optimise();
1059 /*if (KdenliveSettings::osdtimecode()) {
1060 // Attach filter for on screen display of timecode
1062 QString attr = "attr_check";
1063 mlt_filter filter = mlt_factory_filter( "data_feed", (char*) attr.ascii() );
1064 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
1065 mlt_producer_attach( m_mltProducer->get_producer(), filter );
1066 mlt_filter_close( filter );
1068 m_osdInfo = new Mlt::Filter("data_show");
1069 m_osdInfo->set("resource", m_osdProfile.toUtf8().constData());
1070 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1071 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
1072 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
1073 m_osdInfo->set("dynamic", "1");
1075 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1077 m_osdInfo->set("dynamic", "0");
1080 m_fps = m_mltProducer->get_fps();
1081 if (position != 0) {
1082 // Seek to correct place after opening project.
1083 m_mltProducer->seek(position);
1086 kDebug() << "// NEW SCENE LIST DURATION SET TO: " << m_mltProducer->get_playtime();
1087 if (error == 0) error = connectPlaylist();
1088 else connectPlaylist();
1089 fillSlowMotionProducers();
1091 m_isBlocked = false;
1092 blockSignals(false);
1095 //kDebug()<<"// SETSCN LST, POS: "<<position;
1096 //if (position != 0) emit rendererPosition(position);
1099 const QString Render::sceneList()
1102 Mlt::Profile profile((mlt_profile) 0);
1103 Mlt::Consumer xmlConsumer(profile, "xml:kdenlive_playlist");
1104 m_mltProducer->optimise();
1105 xmlConsumer.set("terminate_on_pause", 1);
1106 Mlt::Producer prod(m_mltProducer->get_producer());
1107 bool split = m_isSplitView;
1108 if (split) slotSplitView(false);
1109 xmlConsumer.connect(prod);
1110 xmlConsumer.start();
1111 while (!xmlConsumer.is_stopped()) {}
1112 playlist = QString::fromUtf8(xmlConsumer.get("kdenlive_playlist"));
1113 if (split) slotSplitView(true);
1117 bool Render::saveSceneList(QString path, QDomElement kdenliveData)
1121 doc.setContent(sceneList(), false);
1122 if (!kdenliveData.isNull()) {
1123 // add Kdenlive specific tags
1124 QDomNode mlt = doc.elementsByTagName("mlt").at(0);
1125 mlt.appendChild(doc.importNode(kdenliveData, true));
1127 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
1128 kWarning() << "////// ERROR writing to file: " << path;
1131 file.write(doc.toString().toUtf8());
1132 if (file.error() != QFile::NoError) {
1140 void Render::saveZone(KUrl url, QString desc, QPoint zone)
1142 kDebug() << "// SAVING CLIP ZONE, RENDER: " << m_name;
1143 Mlt::Consumer xmlConsumer(*m_mltProfile, ("xml:" + url.path()).toUtf8().constData());
1144 m_mltProducer->optimise();
1145 xmlConsumer.set("terminate_on_pause", 1);
1146 if (m_name == "clip") {
1147 Mlt::Producer *prod = m_mltProducer->cut(zone.x(), zone.y());
1149 list.insert_at(0, prod, 0);
1151 list.set("title", desc.toUtf8().constData());
1152 xmlConsumer.connect(list);
1155 //TODO: not working yet, save zone from timeline
1156 Mlt::Producer *p1 = new Mlt::Producer(m_mltProducer->get_producer());
1157 /* Mlt::Service service(p1->parent().get_service());
1158 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";*/
1160 //Mlt::Producer *prod = p1->cut(zone.x(), zone.y());
1161 //prod->set("title", desc.toUtf8().constData());
1162 xmlConsumer.connect(*p1); //list);
1165 xmlConsumer.start();
1168 double Render::fps() const
1173 int Render::connectPlaylist()
1175 if (!m_mltConsumer) return -1;
1176 //m_mltConsumer->set("refresh", "0");
1177 m_mltConsumer->connect(*m_mltProducer);
1178 m_mltProducer->set_speed(0);
1179 if (m_mltConsumer->start() == -1) {
1180 // ARGH CONSUMER BROKEN!!!!
1181 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."));
1182 delete m_mltConsumer;
1183 m_mltConsumer = NULL;
1186 emit durationChanged(m_mltProducer->get_playtime());
1191 void Render::refreshDisplay()
1194 if (!m_mltProducer) return;
1195 //m_mltConsumer->set("refresh", 0);
1197 //mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1198 /*if (KdenliveSettings::osdtimecode()) {
1199 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
1200 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
1201 m_osdInfo->set("dynamic", "1");
1202 m_mltProducer->attach(*m_osdInfo);
1205 m_mltProducer->detach(*m_osdInfo);
1206 m_osdInfo->set("dynamic", "0");
1211 int Render::volume() const
1213 if (!m_mltConsumer || !m_mltProducer) return -1;
1214 return ((int) 100 * m_mltProducer->get_double("meta.volume"));
1217 void Render::slotSetVolume(int volume)
1219 if (!m_mltConsumer || !m_mltProducer) return;
1220 m_mltProducer->set("meta.volume", (double)volume / 100.0);
1223 m_mltConsumer->set("refresh", 0);
1224 // Attach filter for on screen display of timecode
1225 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1226 mlt_properties_set_double( properties, "meta.volume", volume );
1227 mlt_properties_set_int( properties, "meta.attr.osdvolume", 1);
1228 mlt_properties_set( properties, "meta.attr.osdvolume.markup", i18n("Volume: ") + QString::number(volume * 100));
1230 if (!KdenliveSettings::osdtimecode()) {
1231 m_mltProducer->detach(*m_osdInfo);
1232 mlt_properties_set_int( properties, "meta.attr.timecode", 0);
1233 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1236 //m_osdTimer->setSingleShot(2500);
1239 void Render::slotOsdTimeout()
1241 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1242 mlt_properties_set_int(properties, "meta.attr.osdvolume", 0);
1243 mlt_properties_set(properties, "meta.attr.osdvolume.markup", NULL);
1244 //if (!KdenliveSettings::osdtimecode()) m_mltProducer->detach(*m_osdInfo);
1248 void Render::start()
1250 kDebug() << "----- STARTING MONITOR: " << m_name;
1251 if (m_winid == -1) {
1252 kDebug() << "----- BROKEN MONITOR: " << m_name << ", RESTART";
1255 if (m_mltConsumer && m_mltConsumer->is_stopped()) {
1256 kDebug() << "----- MONITOR: " << m_name << " WAS STOPPED";
1257 if (m_mltConsumer->start() == -1) {
1258 //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."));
1259 kDebug(QtWarningMsg) << "/ / / / CANNOT START MONITOR";
1261 kDebug() << "----- MONITOR: " << m_name << " REFRESH";
1262 m_isBlocked = false;
1266 m_isBlocked = false;
1271 if (m_mltProducer == NULL) return;
1272 if (m_mltConsumer && !m_mltConsumer->is_stopped()) {
1273 kDebug() << "///////////// RENDER STOPPED: " << m_name;
1274 //m_mltConsumer->set("refresh", 0);
1275 m_mltConsumer->stop();
1276 // delete m_mltConsumer;
1277 // m_mltConsumer = NULL;
1279 kDebug() << "///////////// RENDER STOP2-------";
1282 if (m_mltProducer) {
1283 if (m_isZoneMode) resetZoneMode();
1284 m_mltProducer->set_speed(0.0);
1285 //m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1286 //kDebug() << m_mltProducer->get_length();
1288 kDebug() << "///////////// RENDER STOP3-------";
1291 void Render::stop(const GenTime & startTime)
1294 kDebug() << "///////////// RENDER STOP-------2";
1295 if (m_mltProducer) {
1296 if (m_isZoneMode) resetZoneMode();
1297 m_mltProducer->set_speed(0.0);
1298 m_mltProducer->seek((int) startTime.frames(m_fps));
1300 m_mltConsumer->purge();
1303 void Render::pause()
1305 if (!m_mltProducer || !m_mltConsumer)
1307 if (m_mltProducer->get_speed() == 0.0) return;
1308 if (m_isZoneMode) resetZoneMode();
1310 m_mltConsumer->set("refresh", 0);
1311 m_mltProducer->set_speed(0.0);
1313 The 2 lines below create a flicker loop
1314 emit rendererPosition(m_framePosition);
1315 m_mltProducer->seek(m_framePosition);*/
1316 m_mltConsumer->purge();
1319 void Render::switchPlay(bool play)
1321 if (!m_mltProducer || !m_mltConsumer)
1323 if (m_isZoneMode) resetZoneMode();
1324 if (play && m_mltProducer->get_speed() == 0.0) {
1325 m_isBlocked = false;
1326 if (m_name == "clip" && m_framePosition == (int) m_mltProducer->get_out()) m_mltProducer->seek(0);
1327 m_mltProducer->set_speed(1.0);
1328 m_mltConsumer->set("refresh", 1);
1331 m_mltConsumer->set("refresh", 0);
1332 m_mltProducer->set_speed(0.0);
1333 //emit rendererPosition(m_framePosition);
1334 m_mltProducer->seek(m_framePosition);
1335 //m_mltConsumer->purge();
1339 void Render::play(double speed)
1343 // if (speed == 0.0) m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1344 m_isBlocked = false;
1345 m_mltProducer->set_speed(speed);
1346 /*if (speed == 0.0) {
1347 m_mltProducer->seek((int) m_framePosition + 1);
1348 m_mltConsumer->purge();
1353 void Render::play(const GenTime & startTime)
1355 if (!m_mltProducer || !m_mltConsumer)
1357 m_isBlocked = false;
1358 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1359 m_mltProducer->set_speed(1.0);
1360 m_mltConsumer->set("refresh", 1);
1363 void Render::loopZone(const GenTime & startTime, const GenTime & stopTime)
1365 if (!m_mltProducer || !m_mltConsumer)
1367 //m_mltProducer->set("eof", "loop");
1368 m_isLoopMode = true;
1369 m_loopStart = startTime;
1370 playZone(startTime, stopTime);
1373 void Render::playZone(const GenTime & startTime, const GenTime & stopTime)
1375 if (!m_mltProducer || !m_mltConsumer)
1377 m_isBlocked = false;
1378 if (!m_isZoneMode) m_originalOut = m_mltProducer->get_playtime() - 1;
1379 m_mltProducer->set("out", (int)(stopTime.frames(m_fps)));
1380 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1381 m_mltProducer->set_speed(1.0);
1382 m_mltConsumer->set("refresh", 1);
1383 m_isZoneMode = true;
1386 void Render::resetZoneMode()
1388 if (!m_isZoneMode && !m_isLoopMode) return;
1389 m_mltProducer->set("out", m_originalOut);
1390 //m_mltProducer->set("eof", "pause");
1391 m_isZoneMode = false;
1392 m_isLoopMode = false;
1395 void Render::seekToFrame(int pos)
1397 //kDebug()<<" ********* RENDER SEEK TO POS";
1400 m_isBlocked = false;
1402 m_mltProducer->seek(pos);
1406 void Render::seekToFrameDiff(int diff)
1408 //kDebug()<<" ********* RENDER SEEK TO POS";
1411 m_isBlocked = false;
1413 m_mltProducer->seek(m_mltProducer->position() + diff);
1417 void Render::doRefresh()
1419 // Use a Timer so that we don't refresh too much
1420 if (!m_isBlocked && m_mltConsumer) m_mltConsumer->set("refresh", 1);
1423 void Render::refresh()
1425 if (!m_mltProducer || m_isBlocked)
1427 if (m_mltConsumer) {
1428 m_mltConsumer->set("refresh", 1);
1432 void Render::setDropFrames(bool show)
1434 if (m_mltConsumer) {
1435 int dropFrames = KdenliveSettings::mltthreads();
1436 if (show == false) dropFrames = -dropFrames;
1437 m_mltConsumer->stop();
1439 m_mltConsumer->set("real_time", dropFrames);
1441 m_mltConsumer->set("play.real_time", dropFrames);
1443 if (m_mltConsumer->start() == -1) {
1444 kDebug(QtWarningMsg) << "ERROR, Cannot start monitor";
1450 double Render::playSpeed()
1452 if (m_mltProducer) return m_mltProducer->get_speed();
1456 GenTime Render::seekPosition() const
1458 if (m_mltProducer) return GenTime((int) m_mltProducer->position(), m_fps);
1459 else return GenTime();
1462 int Render::seekFramePosition() const
1464 if (m_mltProducer) return (int) m_mltProducer->position();
1468 const QString & Render::rendererName() const
1473 void Render::emitFrameUpdated(Mlt::Frame& frame)
1475 mlt_image_format format = mlt_image_rgb24a;
1478 const uchar* image = frame.get_image(format, width, height);
1479 QImage qimage(width, height, QImage::Format_ARGB32);
1480 memcpy(qimage.bits(), image, width * height * 4);
1481 emit frameUpdated(qimage.rgbSwapped());
1484 void Render::emitFrameNumber(double position)
1486 m_framePosition = position;
1487 emit rendererPosition((int) position);
1490 void Render::emitConsumerStopped()
1492 // This is used to know when the playing stopped
1493 if (m_mltProducer) {
1494 double pos = m_mltProducer->position();
1495 if (m_isLoopMode) play(m_loopStart);
1496 else if (m_isZoneMode) resetZoneMode();
1497 emit rendererStopped((int) pos);
1501 void Render::exportFileToFirewire(QString /*srcFileName*/, int /*port*/, GenTime /*startTime*/, GenTime /*endTime*/)
1503 KMessageBox::sorry(0, i18n("Firewire is not enabled on your system.\n Please install Libiec61883 and recompile Kdenlive"));
1506 void Render::exportCurrentFrame(KUrl url, bool /*notify*/)
1508 if (!m_mltProducer) {
1509 KMessageBox::sorry(qApp->activeWindow(), i18n("There is no clip, cannot extract frame."));
1513 //int height = 1080;//KdenliveSettings::defaultheight();
1514 //int width = 1940; //KdenliveSettings::displaywidth();
1516 QPixmap pix; // = KThumb::getFrame(m_mltProducer, -1, width, height);
1518 QPixmap pix(width, height);
1519 Mlt::Filter m_convert(*m_mltProfile, "avcolour_space");
1520 m_convert.set("forced", mlt_image_rgb24a);
1521 m_mltProducer->attach(m_convert);
1522 Mlt::Frame * frame = m_mltProducer->get_frame();
1523 m_mltProducer->detach(m_convert);
1525 pix = frameThumbnail(frame, width, height);
1528 pix.save(url.path(), "PNG");
1529 //if (notify) QApplication::postEvent(qApp->activeWindow(), new UrlEvent(url, 10003));
1533 void Render::showFrame(Mlt::Frame& frame)
1535 m_framePosition = qMax(frame.get_int("_position"), 0);
1536 emit rendererPosition((int) m_framePosition);
1537 mlt_image_format format = mlt_image_rgb24a;
1540 const uchar* image = frame.get_image(format, width, height);
1541 QImage qimage(width, height, QImage::Format_ARGB32_Premultiplied);
1542 memcpy(qimage.scanLine(0), image, width * height * 4);
1543 emit showImageSignal(qimage);
1544 if (analyseAudio) showAudio(frame);
1545 if (sendFrameForAnalysis && frame.get_frame()->convert_image) {
1546 emit frameUpdated(qimage.rgbSwapped());
1550 void Render::showAudio(Mlt::Frame& frame)
1552 if (!frame.is_valid() || frame.get_int("test_audio") != 0) {
1555 mlt_audio_format audio_format = mlt_audio_s16;
1557 int num_channels = 0;
1559 int16_t* data = (int16_t*)frame.get_audio(audio_format, freq, num_channels, samples);
1565 // Data format: [ c00 c10 c01 c11 c02 c12 c03 c13 ... c0{samples-1} c1{samples-1} for 2 channels.
1566 // So the vector is of size samples*channels.
1567 QVector<int16_t> sampleVector(samples*num_channels);
1568 memcpy(sampleVector.data(), data, samples*num_channels*sizeof(int16_t));
1571 emit audioSamplesSignal(sampleVector, freq, num_channels, samples);
1576 * MLT playlist direct manipulation.
1579 void Render::mltCheckLength(Mlt::Tractor *tractor)
1581 //kDebug()<<"checking track length: "<<track<<"..........";
1583 int trackNb = tractor->count();
1587 Mlt::Producer trackProducer(tractor->track(0));
1588 duration = trackProducer.get_playtime() - 1;
1589 m_mltProducer->set("out", duration);
1590 emit durationChanged(duration);
1593 while (trackNb > 1) {
1594 Mlt::Producer trackProducer(tractor->track(trackNb - 1));
1595 trackDuration = trackProducer.get_playtime() - 1;
1596 // kDebug() << " / / /DURATON FOR TRACK " << trackNb - 1 << " = " << trackDuration;
1597 if (trackDuration > duration) duration = trackDuration;
1601 Mlt::Producer blackTrackProducer(tractor->track(0));
1603 if (blackTrackProducer.get_playtime() - 1 != duration) {
1604 Mlt::Playlist blackTrackPlaylist((mlt_playlist) blackTrackProducer.get_service());
1605 Mlt::Producer *blackclip = blackTrackPlaylist.get_clip(0);
1606 if (blackclip && blackclip->is_blank()) {
1611 if (blackclip == NULL || blackTrackPlaylist.count() != 1) {
1612 blackTrackPlaylist.clear();
1613 m_blackClip->set("length", duration + 1);
1614 m_blackClip->set("out", duration);
1615 blackclip = m_blackClip->cut(0, duration);
1616 blackTrackPlaylist.insert_at(0, blackclip, 1);
1618 if (duration > blackclip->parent().get_length()) {
1619 blackclip->parent().set("length", duration + 1);
1620 blackclip->parent().set("out", duration);
1621 blackclip->set("length", duration + 1);
1623 blackTrackPlaylist.resize_clip(0, 0, duration);
1627 m_mltProducer->set("out", duration);
1628 emit durationChanged(duration);
1632 Mlt::Producer *Render::checkSlowMotionProducer(Mlt::Producer *prod, QDomElement element)
1634 if (element.attribute("speed", "1.0").toDouble() == 1.0 && element.attribute("strobe", "1").toInt() == 1) return prod;
1636 // We want a slowmotion producer
1637 double speed = element.attribute("speed", "1.0").toDouble();
1638 int strobe = element.attribute("strobe", "1").toInt();
1639 QString url = QString::fromUtf8(prod->get("resource"));
1640 url.append('?' + QString::number(speed));
1641 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
1642 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1643 if (!slowprod || slowprod->get_producer() == NULL) {
1644 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
1645 if (strobe > 1) slowprod->set("strobe", strobe);
1646 QString id = prod->get("id");
1647 if (id.contains('_')) id = id.section('_', 0, 0);
1648 QString producerid = "slowmotion:" + id + ':' + QString::number(speed);
1649 if (strobe > 1) producerid.append(':' + QString::number(strobe));
1650 slowprod->set("id", producerid.toUtf8().constData());
1651 m_slowmotionProducers.insert(url, slowprod);
1656 int Render::mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod, bool overwrite, bool push)
1658 if (m_mltProducer == NULL) {
1659 kDebug() << "PLAYLIST NOT INITIALISED //////";
1663 kDebug() << "Cannot insert clip without producer //////";
1666 Mlt::Producer parentProd(m_mltProducer->parent());
1667 if (parentProd.get_producer() == NULL) {
1668 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1672 Mlt::Service service(parentProd.get_service());
1673 if (service.type() != tractor_type) {
1674 kWarning() << "// TRACTOR PROBLEM";
1677 Mlt::Tractor tractor(service);
1678 if (info.track > tractor.count() - 1) {
1679 kDebug() << "ERROR TRYING TO INSERT CLIP ON TRACK " << info.track << ", at POS: " << info.startPos.frames(25);
1682 mlt_service_lock(service.get_service());
1683 Mlt::Producer trackProducer(tractor.track(info.track));
1684 int trackDuration = trackProducer.get_playtime() - 1;
1685 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1686 //kDebug()<<"/// INSERT cLIP: "<<info.cropStart.frames(m_fps)<<", "<<info.startPos.frames(m_fps)<<"-"<<info.endPos.frames(m_fps);
1687 prod = checkSlowMotionProducer(prod, element);
1688 if (prod == NULL || !prod->is_valid()) {
1689 mlt_service_unlock(service.get_service());
1693 int cutPos = (int) info.cropStart.frames(m_fps);
1694 if (cutPos < 0) cutPos = 0;
1695 int insertPos = (int) info.startPos.frames(m_fps);
1696 int cutDuration = (int)(info.endPos - info.startPos).frames(m_fps) - 1;
1697 Mlt::Producer *clip = prod->cut(cutPos, cutDuration + cutPos);
1698 if (overwrite && (insertPos < trackDuration)) {
1699 // Replace zone with blanks
1700 //trackPlaylist.split_at(insertPos, true);
1701 trackPlaylist.remove_region(insertPos, cutDuration + 1);
1702 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1703 trackPlaylist.insert_blank(clipIndex, cutDuration);
1705 trackPlaylist.split_at(insertPos, true);
1706 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1707 trackPlaylist.insert_blank(clipIndex, cutDuration);
1709 int newIndex = trackPlaylist.insert_at(insertPos, clip, 1);
1711 /*if (QString(prod->get("transparency")).toInt() == 1)
1712 mltAddClipTransparency(info, info.track - 1, QString(prod->get("id")).toInt());*/
1714 if (info.track != 0 && (newIndex + 1 == trackPlaylist.count())) mltCheckLength(&tractor);
1715 mlt_service_unlock(service.get_service());
1716 /*tractor.multitrack()->refresh();
1717 tractor.refresh();*/
1722 void Render::mltCutClip(int track, GenTime position)
1726 Mlt::Service service(m_mltProducer->parent().get_service());
1727 if (service.type() != tractor_type) {
1728 kWarning() << "// TRACTOR PROBLEM";
1732 Mlt::Tractor tractor(service);
1733 Mlt::Producer trackProducer(tractor.track(track));
1734 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1737 /* // Display playlist info
1738 kDebug()<<"//////////// BEFORE";
1739 for (int i = 0; i < trackPlaylist.count(); i++) {
1740 int blankStart = trackPlaylist.clip_start(i);
1741 int blankDuration = trackPlaylist.clip_length(i) - 1;
1743 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1744 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1747 int cutPos = (int) position.frames(m_fps);
1749 int clipIndex = trackPlaylist.get_clip_index_at(cutPos);
1750 if (trackPlaylist.is_blank(clipIndex)) {
1751 kDebug() << "// WARNING, TRYING TO CUT A BLANK";
1752 m_isBlocked = false;
1755 mlt_service_lock(service.get_service());
1756 int clipStart = trackPlaylist.clip_start(clipIndex);
1757 trackPlaylist.split(clipIndex, cutPos - clipStart - 1);
1758 mlt_service_unlock(service.get_service());
1760 // duplicate effects
1761 Mlt::Producer *original = trackPlaylist.get_clip_at(clipStart);
1762 Mlt::Producer *clip = trackPlaylist.get_clip_at(cutPos);
1764 if (original == NULL || clip == NULL) {
1765 kDebug() << "// ERROR GRABBING CLIP AFTER SPLIT";
1767 Mlt::Service clipService(original->get_service());
1768 Mlt::Service dupService(clip->get_service());
1772 Mlt::Filter *filter = clipService.filter(ct);
1774 // Only duplicate Kdenlive filters, and skip the fade in effects
1775 if (filter->is_valid() && strcmp(filter->get("kdenlive_id"), "") && strcmp(filter->get("kdenlive_id"), "fadein") && strcmp(filter->get("kdenlive_id"), "fade_from_black")) {
1776 // looks like there is no easy way to duplicate a filter,
1777 // so we will create a new one and duplicate its properties
1778 Mlt::Filter *dup = new Mlt::Filter(*m_mltProfile, filter->get("mlt_service"));
1779 if (dup && dup->is_valid()) {
1780 Mlt::Properties entries(filter->get_properties());
1781 for (int i = 0; i < entries.count(); i++) {
1782 dup->set(entries.get_name(i), entries.get(i));
1784 dupService.attach(*dup);
1788 filter = clipService.filter(ct);
1791 /* // Display playlist info
1792 kDebug()<<"//////////// AFTER";
1793 for (int i = 0; i < trackPlaylist.count(); i++) {
1794 int blankStart = trackPlaylist.clip_start(i);
1795 int blankDuration = trackPlaylist.clip_length(i) - 1;
1797 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1798 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1801 m_isBlocked = false;
1804 bool Render::mltUpdateClip(ItemInfo info, QDomElement element, Mlt::Producer *prod)
1808 kDebug() << "Cannot update clip with null producer //////";
1811 Mlt::Service service(m_mltProducer->parent().get_service());
1812 if (service.type() != tractor_type) {
1813 kWarning() << "// TRACTOR PROBLEM";
1816 Mlt::Tractor tractor(service);
1817 Mlt::Producer trackProducer(tractor.track(tractor.count() - 1 - info.track));
1818 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1819 int startPos = info.startPos.frames(m_fps);
1820 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
1821 if (trackPlaylist.is_blank(clipIndex)) {
1822 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << startPos;
1825 mlt_service_lock(service.get_service());
1826 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
1828 QList <Mlt::Filter *> filtersList;
1829 Mlt::Service sourceService(clip->get_service());
1831 Mlt::Filter *filter = sourceService.filter(ct);
1833 if (filter->get_int("kdenlive_ix") != 0) {
1834 filtersList.append(filter);
1837 filter = sourceService.filter(ct);
1840 trackPlaylist.replace_with_blank(clipIndex);
1842 //if (!mltRemoveClip(info.track, info.startPos)) return false;
1843 prod = checkSlowMotionProducer(prod, element);
1844 if (prod == NULL || !prod->is_valid()) {
1845 mlt_service_unlock(service.get_service());
1849 Mlt::Producer *clip2 = prod->cut(info.cropStart.frames(m_fps), (info.cropDuration + info.cropStart).frames(m_fps) - 1);
1850 trackPlaylist.insert_at(info.startPos.frames(m_fps), clip2, 1);
1854 //if (mltInsertClip(info, element, prod) == -1) return false;
1855 if (!filtersList.isEmpty()) {
1856 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1857 Mlt::Producer *destclip = trackPlaylist.get_clip(clipIndex);
1858 Mlt::Service destService(destclip->get_service());
1860 for (int i = 0; i < filtersList.count(); i++)
1861 destService.attach(*(filtersList.at(i)));
1863 mlt_service_unlock(service.get_service());
1868 bool Render::mltRemoveClip(int track, GenTime position)
1870 Mlt::Service service(m_mltProducer->parent().get_service());
1871 if (service.type() != tractor_type) {
1872 kWarning() << "// TRACTOR PROBLEM";
1876 Mlt::Tractor tractor(service);
1877 mlt_service_lock(service.get_service());
1878 Mlt::Producer trackProducer(tractor.track(track));
1879 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1880 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
1882 // Display playlist info
1883 //kDebug() << "//// BEFORE -( " << position.frames(m_fps) << " )-------------------------------";
1884 /*for (int i = 0; i < trackPlaylist.count(); i++) {
1885 int blankStart = trackPlaylist.clip_start(i);
1886 int blankDuration = trackPlaylist.clip_length(i) - 1;
1888 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1889 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1891 if (trackPlaylist.is_blank(clipIndex)) {
1892 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << position.frames(25);
1893 mlt_service_unlock(service.get_service());
1896 //kDebug()<<"//// Deleting at: "<< (int) position.frames(m_fps) <<" --------------------------------------";
1898 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
1899 if (clip) delete clip;
1900 trackPlaylist.consolidate_blanks(0);
1901 /*if (QString(clip.parent().get("transparency")).toInt() == 1)
1902 mltDeleteTransparency((int) position.frames(m_fps), track, QString(clip.parent().get("id")).toInt());*/
1904 /* // Display playlist info
1905 kDebug()<<"//// AFTER";
1906 for (int i = 0; i < trackPlaylist.count(); i++) {
1907 int blankStart = trackPlaylist.clip_start(i);
1908 int blankDuration = trackPlaylist.clip_length(i) - 1;
1910 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1911 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1913 mlt_service_unlock(service.get_service());
1914 if (track != 0 && trackPlaylist.count() <= clipIndex) mltCheckLength(&tractor);
1915 m_isBlocked = false;
1919 int Render::mltGetSpaceLength(const GenTime pos, int track, bool fromBlankStart)
1921 if (!m_mltProducer) {
1922 kDebug() << "PLAYLIST NOT INITIALISED //////";
1925 Mlt::Producer parentProd(m_mltProducer->parent());
1926 if (parentProd.get_producer() == NULL) {
1927 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1931 Mlt::Service service(parentProd.get_service());
1932 Mlt::Tractor tractor(service);
1933 int insertPos = pos.frames(m_fps);
1935 Mlt::Producer trackProducer(tractor.track(track));
1936 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1937 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1938 if (clipIndex == trackPlaylist.count()) {
1939 // We are after the end of the playlist
1942 if (!trackPlaylist.is_blank(clipIndex)) return 0;
1943 if (fromBlankStart) return trackPlaylist.clip_length(clipIndex);
1944 return trackPlaylist.clip_length(clipIndex) + trackPlaylist.clip_start(clipIndex) - insertPos;
1947 int Render::mltTrackDuration(int track)
1949 if (!m_mltProducer) {
1950 kDebug() << "PLAYLIST NOT INITIALISED //////";
1953 Mlt::Producer parentProd(m_mltProducer->parent());
1954 if (parentProd.get_producer() == NULL) {
1955 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1959 Mlt::Service service(parentProd.get_service());
1960 Mlt::Tractor tractor(service);
1962 Mlt::Producer trackProducer(tractor.track(track));
1963 return trackProducer.get_playtime() - 1;
1966 void Render::mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime duration, const GenTime timeOffset)
1968 if (!m_mltProducer) {
1969 kDebug() << "PLAYLIST NOT INITIALISED //////";
1972 Mlt::Producer parentProd(m_mltProducer->parent());
1973 if (parentProd.get_producer() == NULL) {
1974 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1977 //kDebug()<<"// CLP STRT LST: "<<trackClipStartList;
1978 //kDebug()<<"// TRA STRT LST: "<<trackTransitionStartList;
1980 Mlt::Service service(parentProd.get_service());
1981 Mlt::Tractor tractor(service);
1982 mlt_service_lock(service.get_service());
1983 int diff = duration.frames(m_fps);
1984 int offset = timeOffset.frames(m_fps);
1988 // insert space in one track only
1989 Mlt::Producer trackProducer(tractor.track(track));
1990 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1991 insertPos = trackClipStartList.value(track);
1992 if (insertPos != -1) {
1993 insertPos += offset;
1994 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1996 trackPlaylist.insert_blank(clipIndex, diff - 1);
1998 if (!trackPlaylist.is_blank(clipIndex)) clipIndex --;
1999 if (!trackPlaylist.is_blank(clipIndex)) {
2000 kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
2002 int position = trackPlaylist.clip_start(clipIndex);
2003 int blankDuration = trackPlaylist.clip_length(clipIndex);
2004 if (blankDuration + diff == 0) {
2005 trackPlaylist.remove(clipIndex);
2006 } else trackPlaylist.remove_region(position, -diff);
2008 trackPlaylist.consolidate_blanks(0);
2010 // now move transitions
2011 mlt_service serv = m_mltProducer->parent().get_service();
2012 mlt_service nextservice = mlt_service_get_producer(serv);
2013 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2014 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2015 QString resource = mlt_properties_get(properties, "mlt_service");
2017 while (mlt_type == "transition") {
2018 mlt_transition tr = (mlt_transition) nextservice;
2019 int currentTrack = mlt_transition_get_b_track(tr);
2020 int currentIn = (int) mlt_transition_get_in(tr);
2021 int currentOut = (int) mlt_transition_get_out(tr);
2022 insertPos = trackTransitionStartList.value(track);
2023 if (insertPos != -1) {
2024 insertPos += offset;
2025 if (track == currentTrack && currentOut > insertPos && resource != "mix") {
2026 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
2029 nextservice = mlt_service_producer(nextservice);
2030 if (nextservice == NULL) break;
2031 properties = MLT_SERVICE_PROPERTIES(nextservice);
2032 mlt_type = mlt_properties_get(properties, "mlt_type");
2033 resource = mlt_properties_get(properties, "mlt_service");
2036 for (int trackNb = tractor.count() - 1; trackNb >= 1; --trackNb) {
2037 Mlt::Producer trackProducer(tractor.track(trackNb));
2038 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2040 //int clipNb = trackPlaylist.count();
2041 insertPos = trackClipStartList.value(trackNb);
2042 if (insertPos != -1) {
2043 insertPos += offset;
2045 /* kDebug()<<"-------------\nTRACK "<<trackNb<<" HAS "<<clipNb<<" CLPIS";
2046 kDebug() << "INSERT SPACE AT: "<<insertPos<<", DIFF: "<<diff<<", TK: "<<trackNb;
2047 for (int i = 0; i < clipNb; i++) {
2048 kDebug()<<"CLIP "<<i<<", START: "<<trackPlaylist.clip_start(i)<<", END: "<<trackPlaylist.clip_start(i) + trackPlaylist.clip_length(i);
2049 if (trackPlaylist.is_blank(i)) kDebug()<<"++ BLANK ++ ";
2050 kDebug()<<"-------------";
2052 kDebug()<<"END-------------";*/
2055 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
2057 trackPlaylist.insert_blank(clipIndex, diff - 1);
2059 if (!trackPlaylist.is_blank(clipIndex)) {
2062 if (!trackPlaylist.is_blank(clipIndex)) {
2063 kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
2065 int position = trackPlaylist.clip_start(clipIndex);
2066 int blankDuration = trackPlaylist.clip_length(clipIndex);
2067 if (diff + blankDuration == 0) {
2068 trackPlaylist.remove(clipIndex);
2069 } else trackPlaylist.remove_region(position, - diff);
2071 trackPlaylist.consolidate_blanks(0);
2074 // now move transitions
2075 mlt_service serv = m_mltProducer->parent().get_service();
2076 mlt_service nextservice = mlt_service_get_producer(serv);
2077 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2078 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2079 QString resource = mlt_properties_get(properties, "mlt_service");
2081 while (mlt_type == "transition") {
2082 mlt_transition tr = (mlt_transition) nextservice;
2083 int currentIn = (int) mlt_transition_get_in(tr);
2084 int currentOut = (int) mlt_transition_get_out(tr);
2085 int currentTrack = mlt_transition_get_b_track(tr);
2086 insertPos = trackTransitionStartList.value(currentTrack);
2087 if (insertPos != -1) {
2088 insertPos += offset;
2089 if (currentOut > insertPos && resource != "mix") {
2090 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
2093 nextservice = mlt_service_producer(nextservice);
2094 if (nextservice == NULL) break;
2095 properties = MLT_SERVICE_PROPERTIES(nextservice);
2096 mlt_type = mlt_properties_get(properties, "mlt_type");
2097 resource = mlt_properties_get(properties, "mlt_service");
2100 mlt_service_unlock(service.get_service());
2101 mltCheckLength(&tractor);
2102 m_mltConsumer->set("refresh", 1);
2106 void Render::mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest)
2108 if (source == dest) return;
2109 Mlt::Service sourceService(source->get_service());
2110 Mlt::Service destService(dest->get_service());
2112 // move all effects to the correct producer
2114 Mlt::Filter *filter = sourceService.filter(ct);
2116 if (filter->get_int("kdenlive_ix") != 0) {
2117 sourceService.detach(*filter);
2118 destService.attach(*filter);
2120 filter = sourceService.filter(ct);
2124 int Render::mltChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, double speed, double /*oldspeed*/, int strobe, Mlt::Producer *prod)
2128 Mlt::Service service(m_mltProducer->parent().get_service());
2129 if (service.type() != tractor_type) {
2130 kWarning() << "// TRACTOR PROBLEM";
2133 //kDebug() << "Changing clip speed, set in and out: " << info.cropStart.frames(m_fps) << " to " << (info.endPos - info.startPos).frames(m_fps) - 1;
2134 Mlt::Tractor tractor(service);
2135 Mlt::Producer trackProducer(tractor.track(info.track));
2136 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2137 int startPos = info.startPos.frames(m_fps);
2138 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
2139 int clipLength = trackPlaylist.clip_length(clipIndex);
2141 Mlt::Producer *original = trackPlaylist.get_clip(clipIndex);
2142 if (original == NULL) {
2145 if (!original->is_valid() || original->is_blank()) {
2150 Mlt::Producer clipparent = original->parent();
2151 if (!clipparent.is_valid() || clipparent.is_blank()) {
2157 QString serv = clipparent.get("mlt_service");
2158 QString id = clipparent.get("id");
2159 if (speed <= 0 && speed > -1) speed = 1.0;
2160 //kDebug() << "CLIP SERVICE: " << serv;
2161 if ((serv == "avformat" || serv == "avformat-novalidate") && (speed != 1.0 || strobe > 1)) {
2162 mlt_service_lock(service.get_service());
2163 QString url = QString::fromUtf8(clipparent.get("resource"));
2164 url.append('?' + QString::number(speed));
2165 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
2166 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
2167 if (!slowprod || slowprod->get_producer() == NULL) {
2168 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
2169 if (strobe > 1) slowprod->set("strobe", strobe);
2170 QString producerid = "slowmotion:" + id + ':' + QString::number(speed);
2171 if (strobe > 1) producerid.append(':' + QString::number(strobe));
2172 slowprod->set("id", producerid.toUtf8().constData());
2173 // copy producer props
2174 double ar = original->parent().get_double("force_aspect_ratio");
2175 if (ar != 0.0) slowprod->set("force_aspect_ratio", ar);
2176 double fps = original->parent().get_double("force_fps");
2177 if (fps != 0.0) slowprod->set("force_fps", fps);
2178 int threads = original->parent().get_int("threads");
2179 if (threads != 0) slowprod->set("threads", threads);
2180 if (original->parent().get("force_progressive"))
2181 slowprod->set("force_progressive", original->parent().get_int("force_progressive"));
2182 if (original->parent().get("force_tff"))
2183 slowprod->set("force_tff", original->parent().get_int("force_tff"));
2184 int ix = original->parent().get_int("video_index");
2185 if (ix != 0) slowprod->set("video_index", ix);
2186 int colorspace = original->parent().get_int("force_colorspace");
2187 if (colorspace != 0) slowprod->set("force_colorspace", colorspace);
2188 int full_luma = original->parent().get_int("set.force_full_luma");
2189 if (full_luma != 0) slowprod->set("set.force_full_luma", full_luma);
2190 m_slowmotionProducers.insert(url, slowprod);
2192 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2193 trackPlaylist.consolidate_blanks(0);
2195 // Check that the blank space is long enough for our new duration
2196 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2197 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2199 if (clipIndex + 1 < trackPlaylist.count() && (startPos + clipLength / speed > blankEnd)) {
2200 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2201 cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)(info.cropStart.frames(m_fps) / speed + maxLength.frames(m_fps) - 1));
2202 } else cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)((info.cropStart.frames(m_fps) + clipLength) / speed - 1));
2204 // move all effects to the correct producer
2205 mltPasteEffects(clip, cut);
2206 trackPlaylist.insert_at(startPos, cut, 1);
2209 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2210 newLength = trackPlaylist.clip_length(clipIndex);
2211 mlt_service_unlock(service.get_service());
2212 } else if (speed == 1.0 && strobe < 2) {
2213 mlt_service_lock(service.get_service());
2215 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2216 trackPlaylist.consolidate_blanks(0);
2218 // Check that the blank space is long enough for our new duration
2219 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2220 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2223 int originalStart = (int)(speedIndependantInfo.cropStart.frames(m_fps));
2224 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + speedIndependantInfo.cropDuration).frames(m_fps) > blankEnd) {
2225 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2226 cut = prod->cut(originalStart, (int)(originalStart + maxLength.frames(m_fps) - 1));
2227 } else cut = prod->cut(originalStart, (int)(originalStart + speedIndependantInfo.cropDuration.frames(m_fps)) - 1);
2229 // move all effects to the correct producer
2230 mltPasteEffects(clip, cut);
2232 trackPlaylist.insert_at(startPos, cut, 1);
2235 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2236 newLength = trackPlaylist.clip_length(clipIndex);
2237 mlt_service_unlock(service.get_service());
2239 } else if (serv == "framebuffer") {
2240 mlt_service_lock(service.get_service());
2241 QString url = QString::fromUtf8(clipparent.get("resource"));
2242 url = url.section('?', 0, 0);
2243 url.append('?' + QString::number(speed));
2244 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
2245 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
2246 if (!slowprod || slowprod->get_producer() == NULL) {
2247 slowprod = new Mlt::Producer(*m_mltProfile, 0, ("framebuffer:" + url).toUtf8().constData());
2248 slowprod->set("strobe", strobe);
2249 QString producerid = "slowmotion:" + id.section(':', 1, 1) + ':' + QString::number(speed);
2250 if (strobe > 1) producerid.append(':' + QString::number(strobe));
2251 slowprod->set("id", producerid.toUtf8().constData());
2252 // copy producer props
2253 double ar = original->parent().get_double("force_aspect_ratio");
2254 if (ar != 0.0) slowprod->set("force_aspect_ratio", ar);
2255 double fps = original->parent().get_double("force_fps");
2256 if (fps != 0.0) slowprod->set("force_fps", fps);
2257 if (original->parent().get("force_progressive"))
2258 slowprod->set("force_progressive", original->parent().get_int("force_progressive"));
2259 if (original->parent().get("force_tff"))
2260 slowprod->set("force_tff", original->parent().get_int("force_tff"));
2261 int threads = original->parent().get_int("threads");
2262 if (threads != 0) slowprod->set("threads", threads);
2263 int ix = original->parent().get_int("video_index");
2264 if (ix != 0) slowprod->set("video_index", ix);
2265 int colorspace = original->parent().get_int("force_colorspace");
2266 if (colorspace != 0) slowprod->set("force_colorspace", colorspace);
2267 int full_luma = original->parent().get_int("set.force_full_luma");
2268 if (full_luma != 0) slowprod->set("set.force_full_luma", full_luma);
2269 m_slowmotionProducers.insert(url, slowprod);
2271 Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
2272 trackPlaylist.consolidate_blanks(0);
2274 GenTime duration = speedIndependantInfo.cropDuration / speed;
2275 int originalStart = (int)(speedIndependantInfo.cropStart.frames(m_fps) / speed);
2277 // Check that the blank space is long enough for our new duration
2278 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2279 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
2282 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + duration).frames(m_fps) > blankEnd) {
2283 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
2284 cut = slowprod->cut(originalStart, (int)(originalStart + maxLength.frames(m_fps) - 1));
2285 } else cut = slowprod->cut(originalStart, (int)(originalStart + duration.frames(m_fps)) - 1);
2287 // move all effects to the correct producer
2288 mltPasteEffects(clip, cut);
2290 trackPlaylist.insert_at(startPos, cut, 1);
2293 clipIndex = trackPlaylist.get_clip_index_at(startPos);
2294 newLength = trackPlaylist.clip_length(clipIndex);
2296 mlt_service_unlock(service.get_service());
2299 if (clipIndex + 1 == trackPlaylist.count()) mltCheckLength(&tractor);
2300 m_isBlocked = false;
2304 bool Render::mltRemoveTrackEffect(int track, QString index, bool updateIndex)
2306 Mlt::Service service(m_mltProducer->parent().get_service());
2307 bool success = false;
2308 Mlt::Tractor tractor(service);
2309 Mlt::Producer trackProducer(tractor.track(track));
2310 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2311 Mlt::Service clipService(trackPlaylist.get_service());
2314 mlt_service_lock(service.get_service());
2316 Mlt::Filter *filter = clipService.filter(ct);
2318 if ((index == "-1" && strcmp(filter->get("kdenlive_id"), "")) || filter->get_int("kdenlive_ix") == index.toInt()) {
2319 if (clipService.detach(*filter) == 0) success = true;
2320 } else if (updateIndex) {
2321 // Adjust the other effects index
2322 if (filter->get_int("kdenlive_ix") > index.toInt()) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2325 filter = clipService.filter(ct);
2327 m_isBlocked = false;
2328 mlt_service_unlock(service.get_service());
2333 bool Render::mltRemoveEffect(int track, GenTime position, QString index, bool updateIndex, bool doRefresh)
2335 if (position < GenTime()) {
2336 // Remove track effect
2337 return mltRemoveTrackEffect(track, index, updateIndex);
2339 Mlt::Service service(m_mltProducer->parent().get_service());
2340 bool success = false;
2341 Mlt::Tractor tractor(service);
2342 Mlt::Producer trackProducer(tractor.track(track));
2343 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2345 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2346 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2348 kDebug() << " / / / CANNOT FIND CLIP TO REMOVE EFFECT";
2352 Mlt::Service clipService(clip->get_service());
2353 int duration = clip->get_playtime();
2355 // Check if clip is visible in monitor
2356 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2357 if (diff < 0 || diff > duration) doRefresh = false;
2361 // if (tag.startsWith("ladspa")) tag = "ladspa";
2363 mlt_service_lock(service.get_service());
2365 Mlt::Filter *filter = clipService.filter(ct);
2367 if ((index == "-1" && strcmp(filter->get("kdenlive_id"), "")) || filter->get_int("kdenlive_ix") == index.toInt()) {// && filter->get("kdenlive_id") == id) {
2368 if (clipService.detach(*filter) == 0) success = true;
2369 //kDebug()<<"Deleted filter id:"<<filter->get("kdenlive_id")<<", ix:"<<filter->get("kdenlive_ix")<<", SERVICE:"<<filter->get("mlt_service");
2370 } else if (updateIndex) {
2371 // Adjust the other effects index
2372 if (filter->get_int("kdenlive_ix") > index.toInt()) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2375 filter = clipService.filter(ct);
2377 m_isBlocked = false;
2378 mlt_service_unlock(service.get_service());
2379 if (doRefresh) refresh();
2383 bool Render::mltAddTrackEffect(int track, EffectsParameterList params)
2385 Mlt::Service service(m_mltProducer->parent().get_service());
2386 Mlt::Tractor tractor(service);
2387 Mlt::Producer trackProducer(tractor.track(track));
2388 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2389 Mlt::Service trackService(trackProducer.get_service()); //trackPlaylist
2390 return mltAddEffect(trackService, params, trackProducer.get_playtime() - 1, true);
2394 bool Render::mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh)
2397 Mlt::Service service(m_mltProducer->parent().get_service());
2399 Mlt::Tractor tractor(service);
2400 Mlt::Producer trackProducer(tractor.track(track));
2401 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2403 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2404 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2409 Mlt::Service clipService(clip->get_service());
2410 int duration = clip->get_playtime();
2412 // Check if clip is visible in monitor
2413 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2414 if (diff < 0 || diff > duration) doRefresh = false;
2417 return mltAddEffect(clipService, params, duration, doRefresh);
2420 bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int duration, bool doRefresh)
2422 bool updateIndex = false;
2423 const int filter_ix = params.paramValue("kdenlive_ix").toInt();
2424 const QString region = params.paramValue("region");
2427 mlt_service_lock(service.get_service());
2429 Mlt::Filter *filter = service.filter(ct);
2431 if (filter->get_int("kdenlive_ix") == filter_ix) {
2432 // A filter at that position already existed, so we will increase all indexes later
2437 filter = service.filter(ct);
2440 if (params.paramValue("id") == "speed") {
2441 // special case, speed effect is not really inserted, we just update the other effects index (kdenlive_ix)
2443 filter = service.filter(ct);
2445 if (filter->get_int("kdenlive_ix") >= filter_ix) {
2446 if (updateIndex) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") + 1);
2449 filter = service.filter(ct);
2451 m_isBlocked = false;
2452 mlt_service_unlock(service.get_service());
2453 if (doRefresh) refresh();
2458 // temporarily remove all effects after insert point
2459 QList <Mlt::Filter *> filtersList;
2461 filter = service.filter(ct);
2463 if (filter->get_int("kdenlive_ix") >= filter_ix) {
2464 filtersList.append(filter);
2465 service.detach(*filter);
2467 filter = service.filter(ct);
2471 QString tag = params.paramValue("tag");
2472 kDebug() << " / / INSERTING EFFECT: " << tag << ", REGI: " << region;
2473 char *filterTag = qstrdup(tag.toUtf8().constData());
2474 char *filterId = qstrdup(params.paramValue("id").toUtf8().constData());
2475 QHash<QString, QString>::Iterator it;
2476 QString kfr = params.paramValue("keyframes");
2478 if (!kfr.isEmpty()) {
2479 QStringList keyFrames = kfr.split(';', QString::SkipEmptyParts);
2480 kDebug() << "// ADDING KEYFRAME EFFECT: " << params.paramValue("keyframes");
2481 char *starttag = qstrdup(params.paramValue("starttag", "start").toUtf8().constData());
2482 char *endtag = qstrdup(params.paramValue("endtag", "end").toUtf8().constData());
2483 kDebug() << "// ADDING KEYFRAME TAGS: " << starttag << ", " << endtag;
2484 //double max = params.paramValue("max").toDouble();
2485 double min = params.paramValue("min").toDouble();
2486 double factor = params.paramValue("factor", "1").toDouble();
2487 params.removeParam("starttag");
2488 params.removeParam("endtag");
2489 params.removeParam("keyframes");
2490 params.removeParam("min");
2491 params.removeParam("max");
2492 params.removeParam("factor");
2494 // Special case, only one keyframe, means we want a constant value
2495 if (keyFrames.count() == 1) {
2496 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2497 if (filter && filter->is_valid()) {
2498 filter->set("kdenlive_id", filterId);
2499 int x1 = keyFrames.at(0).section(':', 0, 0).toInt();
2500 double y1 = keyFrames.at(0).section(':', 1, 1).toDouble();
2501 for (int j = 0; j < params.count(); j++) {
2502 filter->set(params.at(j).name().toUtf8().constData(), params.at(j).value().toUtf8().constData());
2504 filter->set("in", x1);
2505 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2506 filter->set(starttag, QString::number((min + y1) / factor).toUtf8().data());
2507 service.attach(*filter);
2509 } else for (int i = 0; i < keyFrames.size() - 1; ++i) {
2510 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2511 if (filter && filter->is_valid()) {
2512 filter->set("kdenlive_id", filterId);
2513 int x1 = keyFrames.at(i).section(':', 0, 0).toInt() + offset;
2514 double y1 = keyFrames.at(i).section(':', 1, 1).toDouble();
2515 int x2 = keyFrames.at(i + 1).section(':', 0, 0).toInt();
2516 double y2 = keyFrames.at(i + 1).section(':', 1, 1).toDouble();
2517 if (x2 == -1) x2 = duration;
2519 for (int j = 0; j < params.count(); j++) {
2520 filter->set(params.at(j).name().toUtf8().constData(), params.at(j).value().toUtf8().constData());
2523 filter->set("in", x1);
2524 filter->set("out", x2);
2525 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2526 filter->set(starttag, QString::number((min + y1) / factor).toUtf8().data());
2527 filter->set(endtag, QString::number((min + y2) / factor).toUtf8().data());
2528 service.attach(*filter);
2535 Mlt::Filter *filter;
2537 if (!region.isEmpty()) {
2538 filter = new Mlt::Filter(*m_mltProfile, "region");
2539 } else filter = new Mlt::Filter(*m_mltProfile, filterTag);
2540 if (filter && filter->is_valid()) {
2541 filter->set("kdenlive_id", filterId);
2542 if (!region.isEmpty()) {
2543 filter->set("resource", region.toUtf8().constData());
2544 filter->set("kdenlive_ix", params.paramValue("kdenlive_ix").toUtf8().constData());
2545 filter->set("filter0", filterTag);
2546 prefix = "filter0.";
2547 params.removeParam("id");
2548 params.removeParam("region");
2549 params.removeParam("kdenlive_ix");
2552 kDebug() << "filter is NULL";
2553 m_isBlocked = false;
2554 mlt_service_unlock(service.get_service());
2557 params.removeParam("kdenlive_id");
2559 for (int j = 0; j < params.count(); j++) {
2560 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2564 QString effectArgs = params.paramValue("id").section('_', 1);
2566 params.removeParam("id");
2567 params.removeParam("kdenlive_ix");
2568 params.removeParam("tag");
2569 params.removeParam("disable");
2570 params.removeParam("region");
2572 for (int j = 0; j < params.count(); j++) {
2573 effectArgs.append(' ' + params.at(j).value());
2575 //kDebug() << "SOX EFFECTS: " << effectArgs.simplified();
2576 filter->set("effect", effectArgs.simplified().toUtf8().constData());
2579 if (params.paramValue("id") == "pan_zoom") {
2580 filter->set_in_and_out(service.get_int("in"), service.get_int("out") + 1);
2583 // attach filter to the clip
2584 service.attach(*filter);
2589 // re-add following filters
2590 for (int i = 0; i < filtersList.count(); i++) {
2591 Mlt::Filter *filter = filtersList.at(i);
2593 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") + 1);
2594 service.attach(*filter);
2596 m_isBlocked = false;
2597 mlt_service_unlock(service.get_service());
2598 if (doRefresh) refresh();
2602 bool Render::mltEditTrackEffect(int track, EffectsParameterList params)
2604 Mlt::Service service(m_mltProducer->parent().get_service());
2605 Mlt::Tractor tractor(service);
2606 Mlt::Producer trackProducer(tractor.track(track));
2607 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2608 Mlt::Service clipService(trackPlaylist.get_service());
2611 QString index = params.paramValue("kdenlive_ix");
2612 QString tag = params.paramValue("tag");
2614 Mlt::Filter *filter = clipService.filter(ct);
2616 if (filter->get_int("kdenlive_ix") == index.toInt()) {
2620 filter = clipService.filter(ct);
2624 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT! " << index << ", " << tag;
2625 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2627 bool success = false;//mltAddTrackEffect(track, params);
2628 m_isBlocked = false;
2632 QString ser = filter->get("mlt_service");
2633 if (ser == "region") prefix = "filter0.";
2634 mlt_service_lock(service.get_service());
2635 for (int j = 0; j < params.count(); j++) {
2636 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2638 mlt_service_unlock(service.get_service());
2640 m_isBlocked = false;
2646 bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList params)
2648 QString index = params.paramValue("kdenlive_ix");
2649 QString tag = params.paramValue("tag");
2651 if (!params.paramValue("keyframes").isEmpty() || /*it.key().startsWith("#") || */tag.startsWith("ladspa") || tag == "sox" || tag == "autotrack_rectangle" || params.hasParam("region")) {
2652 // This is a keyframe effect, to edit it, we remove it and re-add it.
2653 bool success = mltRemoveEffect(track, position, index, false);
2654 // if (!success) kDebug() << "// ERROR Removing effect : " << index;
2655 if (position < GenTime())
2656 success = mltAddTrackEffect(track, params);
2658 success = mltAddEffect(track, position, params);
2659 // if (!success) kDebug() << "// ERROR Adding effect : " << index;
2662 if (position < GenTime()) {
2663 return mltEditTrackEffect(track, params);
2666 Mlt::Service service(m_mltProducer->parent().get_service());
2667 Mlt::Tractor tractor(service);
2668 Mlt::Producer trackProducer(tractor.track(track));
2669 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2671 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2672 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2674 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2678 int duration = clip->get_playtime();
2679 bool doRefresh = true;
2680 // Check if clip is visible in monitor
2681 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2682 if (diff < 0 || diff > duration)
2687 Mlt::Filter *filter = clip->filter(ct);
2689 if (filter->get_int("kdenlive_ix") == index.toInt()) {
2693 filter = clip->filter(ct);
2697 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT! " << index << ", " << tag;
2698 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2700 bool success = mltAddEffect(track, position, params);
2701 m_isBlocked = false;
2705 QString ser = filter->get("mlt_service");
2706 if (ser == "region") prefix = "filter0.";
2707 mlt_service_lock(service.get_service());
2708 for (int j = 0; j < params.count(); j++) {
2709 filter->set((prefix + params.at(j).name()).toUtf8().constData(), params.at(j).value().toUtf8().constData());
2712 if (params.paramValue("id") == "pan_zoom")
2713 filter->set_in_and_out(clip->get_in(), clip->get_out() + 1);
2716 mlt_service_unlock(service.get_service());
2718 m_isBlocked = false;
2719 if (doRefresh) refresh();
2723 void Render::mltUpdateEffectPosition(int track, GenTime position, int oldPos, int newPos)
2725 Mlt::Service service(m_mltProducer->parent().get_service());
2726 Mlt::Tractor tractor(service);
2727 Mlt::Producer trackProducer(tractor.track(track));
2728 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2730 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2731 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2733 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2737 Mlt::Service clipService(clip->get_service());
2738 int duration = clip->get_playtime();
2739 bool doRefresh = true;
2740 // Check if clip is visible in monitor
2741 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2742 if (diff < 0 || diff > duration) doRefresh = false;
2747 Mlt::Filter *filter = clipService.filter(ct);
2749 int pos = filter->get_int("kdenlive_ix");
2750 if (pos == oldPos) {
2751 filter->set("kdenlive_ix", newPos);
2753 filter = clipService.filter(ct);
2756 m_isBlocked = false;
2757 if (doRefresh) refresh();
2760 void Render::mltMoveEffect(int track, GenTime position, int oldPos, int newPos)
2762 if (position < GenTime()) {
2763 mltMoveTrackEffect(track, oldPos, newPos);
2766 Mlt::Service service(m_mltProducer->parent().get_service());
2767 Mlt::Tractor tractor(service);
2768 Mlt::Producer trackProducer(tractor.track(track));
2769 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2771 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
2772 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2774 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2778 Mlt::Service clipService(clip->get_service());
2779 int duration = clip->get_playtime();
2780 bool doRefresh = true;
2781 // Check if clip is visible in monitor
2782 int diff = trackPlaylist.clip_start(clipIndex) + duration - m_mltProducer->position();
2783 if (diff < 0 || diff > duration) doRefresh = false;
2788 QList <Mlt::Filter *> filtersList;
2789 Mlt::Filter *filter = clipService.filter(ct);
2791 if (newPos > oldPos) {
2793 if (!found && filter->get_int("kdenlive_ix") == oldPos) {
2794 filter->set("kdenlive_ix", newPos);
2795 filtersList.append(filter);
2796 clipService.detach(*filter);
2797 filter = clipService.filter(ct);
2798 while (filter && filter->get_int("kdenlive_ix") <= newPos) {
2799 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2801 filter = clipService.filter(ct);
2805 if (filter && filter->get_int("kdenlive_ix") > newPos) {
2806 filtersList.append(filter);
2807 clipService.detach(*filter);
2809 filter = clipService.filter(ct);
2813 if (filter->get_int("kdenlive_ix") == oldPos) {
2814 filter->set("kdenlive_ix", newPos);
2815 filtersList.append(filter);
2816 clipService.detach(*filter);
2818 filter = clipService.filter(ct);
2822 filter = clipService.filter(ct);
2824 int pos = filter->get_int("kdenlive_ix");
2825 if (pos >= newPos) {
2826 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
2827 filtersList.append(filter);
2828 clipService.detach(*filter);
2830 filter = clipService.filter(ct);
2834 for (int i = 0; i < filtersList.count(); i++) {
2835 clipService.attach(*(filtersList.at(i)));
2838 m_isBlocked = false;
2839 if (doRefresh) refresh();
2842 void Render::mltMoveTrackEffect(int track, int oldPos, int newPos)
2844 Mlt::Service service(m_mltProducer->parent().get_service());
2845 Mlt::Tractor tractor(service);
2846 Mlt::Producer trackProducer(tractor.track(track));
2847 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2849 Mlt::Service clipService(trackPlaylist.get_service());
2853 QList <Mlt::Filter *> filtersList;
2854 Mlt::Filter *filter = clipService.filter(ct);
2856 if (newPos > oldPos) {
2858 if (!found && filter->get_int("kdenlive_ix") == oldPos) {
2859 filter->set("kdenlive_ix", newPos);
2860 filtersList.append(filter);
2861 clipService.detach(*filter);
2862 filter = clipService.filter(ct);
2863 while (filter && filter->get_int("kdenlive_ix") <= newPos) {
2864 filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
2866 filter = clipService.filter(ct);
2870 if (filter && filter->get_int("kdenlive_ix") > newPos) {
2871 filtersList.append(filter);
2872 clipService.detach(*filter);
2874 filter = clipService.filter(ct);
2878 if (filter->get_int("kdenlive_ix") == oldPos) {
2879 filter->set("kdenlive_ix", newPos);
2880 filtersList.append(filter);
2881 clipService.detach(*filter);
2883 filter = clipService.filter(ct);
2887 filter = clipService.filter(ct);
2889 int pos = filter->get_int("kdenlive_ix");
2890 if (pos >= newPos) {
2891 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
2892 filtersList.append(filter);
2893 clipService.detach(*filter);
2895 filter = clipService.filter(ct);
2899 for (int i = 0; i < filtersList.count(); i++) {
2900 clipService.attach(*(filtersList.at(i)));
2902 m_isBlocked = false;
2906 bool Render::mltResizeClipEnd(ItemInfo info, GenTime clipDuration)
2909 Mlt::Service service(m_mltProducer->parent().get_service());
2910 Mlt::Tractor tractor(service);
2911 Mlt::Producer trackProducer(tractor.track(info.track));
2912 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2914 /* // Display playlist info
2915 kDebug()<<"//////////// BEFORE RESIZE";
2916 for (int i = 0; i < trackPlaylist.count(); i++) {
2917 int blankStart = trackPlaylist.clip_start(i);
2918 int blankDuration = trackPlaylist.clip_length(i) - 1;
2920 if (trackPlaylist.is_blank(i)) blk = "(blank)";
2921 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
2924 if (trackPlaylist.is_blank_at((int) info.startPos.frames(m_fps))) {
2925 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
2926 m_isBlocked = false;
2929 mlt_service_lock(service.get_service());
2930 int clipIndex = trackPlaylist.get_clip_index_at((int) info.startPos.frames(m_fps));
2931 //kDebug() << "// SELECTED CLIP START: " << trackPlaylist.clip_start(clipIndex);
2932 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2934 int previousStart = clip->get_in();
2935 int newDuration = (int) clipDuration.frames(m_fps) - 1;
2936 int diff = newDuration - (trackPlaylist.clip_length(clipIndex) - 1);
2938 int currentOut = newDuration + previousStart;
2939 if (currentOut > clip->get_length()) {
2940 clip->parent().set("length", currentOut + 1);
2941 clip->parent().set("out", currentOut);
2942 clip->set("length", currentOut + 1);
2945 /*if (newDuration > clip->get_out()) {
2946 clip->parent().set_in_and_out(0, newDuration + 1);
2947 clip->set_in_and_out(0, newDuration + 1);
2950 trackPlaylist.resize_clip(clipIndex, previousStart, newDuration + previousStart);
2951 trackPlaylist.consolidate_blanks(0);
2952 // skip to next clip
2954 //kDebug() << "//////// RESIZE CLIP: " << clipIndex << "( pos: " << info.startPos.frames(25) << "), DIFF: " << diff << ", CURRENT DUR: " << previousDuration << ", NEW DUR: " << newDuration << ", IX: " << clipIndex << ", MAX: " << trackPlaylist.count();
2956 // clip was made longer, trim next blank if there is one.
2957 if (clipIndex < trackPlaylist.count()) {
2958 // If this is not the last clip in playlist
2959 if (trackPlaylist.is_blank(clipIndex)) {
2960 int blankStart = trackPlaylist.clip_start(clipIndex);
2961 int blankDuration = trackPlaylist.clip_length(clipIndex);
2962 if (diff > blankDuration) {
2963 kDebug() << "// ERROR blank clip is not large enough to get back required space!!!";
2965 if (diff - blankDuration == 0) {
2966 trackPlaylist.remove(clipIndex);
2967 } else trackPlaylist.remove_region(blankStart, diff);
2969 kDebug() << "/// RESIZE ERROR, NXT CLIP IS NOT BLK: " << clipIndex;
2972 } else if (clipIndex != trackPlaylist.count()) trackPlaylist.insert_blank(clipIndex, 0 - diff - 1);
2973 trackPlaylist.consolidate_blanks(0);
2974 mlt_service_unlock(service.get_service());
2976 if (info.track != 0 && clipIndex == trackPlaylist.count()) mltCheckLength(&tractor);
2977 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
2978 //mltResizeTransparency(previousStart, previousStart, previousStart + newDuration, track, QString(clip->parent().get("id")).toInt());
2979 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
2980 ItemInfo transpinfo;
2981 transpinfo.startPos = info.startPos;
2982 transpinfo.endPos = info.startPos + clipDuration;
2983 transpinfo.track = info.track;
2984 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
2986 m_isBlocked = false;
2987 m_mltConsumer->set("refresh", 1);
2991 void Render::mltChangeTrackState(int track, bool mute, bool blind)
2993 Mlt::Service service(m_mltProducer->parent().get_service());
2994 Mlt::Tractor tractor(service);
2995 Mlt::Producer trackProducer(tractor.track(track));
2997 // Make sure muting will not produce problems with our audio mixing transition,
2998 // because audio mixing is done between each track and the lowest one
2999 bool audioMixingBroken = false;
3000 if (mute && trackProducer.get_int("hide") < 2 ) {
3001 // We mute a track with sound
3002 if (track == getLowestNonMutedAudioTrack(tractor)) audioMixingBroken = true;
3004 else if (!mute && trackProducer.get_int("hide") > 1 ) {
3005 // We un-mute a previously muted track
3006 if (track < getLowestNonMutedAudioTrack(tractor)) audioMixingBroken = true;
3010 if (blind) trackProducer.set("hide", 3);
3011 else trackProducer.set("hide", 2);
3013 trackProducer.set("hide", 1);
3015 trackProducer.set("hide", 0);
3017 if (audioMixingBroken) fixAudioMixing(tractor);
3019 tractor.multitrack()->refresh();
3024 int Render::getLowestNonMutedAudioTrack(Mlt::Tractor tractor)
3026 for (int i = 1; i < tractor.count(); i++) {
3027 Mlt::Producer trackProducer(tractor.track(i));
3028 if (trackProducer.get_int("hide") < 2) return i;
3030 return tractor.count() - 1;
3033 void Render::fixAudioMixing(Mlt::Tractor tractor)
3035 // Make sure the audio mixing transitions are applied to the lowest audible (non muted) track
3036 int lowestTrack = getLowestNonMutedAudioTrack(tractor);
3038 mlt_service serv = m_mltProducer->parent().get_service();
3039 Mlt::Field *field = tractor.field();
3040 mlt_service_lock(serv);
3043 mlt_service nextservice = mlt_service_get_producer(serv);
3044 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3045 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3046 QString resource = mlt_properties_get(properties, "mlt_service");
3048 mlt_service nextservicetodisconnect;
3049 // Delete all audio mixing transitions
3050 while (mlt_type == "transition") {
3051 if (resource == "mix") {
3052 nextservicetodisconnect = nextservice;
3053 nextservice = mlt_service_producer(nextservice);
3054 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
3056 else nextservice = mlt_service_producer(nextservice);
3057 if (nextservice == NULL) break;
3058 properties = MLT_SERVICE_PROPERTIES(nextservice);
3059 mlt_type = mlt_properties_get(properties, "mlt_type");
3060 resource = mlt_properties_get(properties, "mlt_service");
3063 // Re-add correct audio transitions
3064 for (int i = lowestTrack + 1; i < tractor.count(); i++) {
3065 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "mix");
3066 transition->set("always_active", 1);
3067 transition->set("combine", 1);
3068 transition->set("internal_added", 237);
3069 field->plant_transition(*transition, lowestTrack, i);
3071 mlt_service_unlock(serv);
3075 bool Render::mltResizeClipCrop(ItemInfo info, GenTime diff)
3077 Mlt::Service service(m_mltProducer->parent().get_service());
3078 int frameOffset = (int) diff.frames(m_fps);
3079 Mlt::Tractor tractor(service);
3080 Mlt::Producer trackProducer(tractor.track(info.track));
3081 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3082 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
3083 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
3086 mlt_service_lock(service.get_service());
3087 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
3088 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
3090 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
3091 mlt_service_unlock(service.get_service());
3094 int previousStart = clip->get_in();
3095 int previousOut = clip->get_out();
3098 trackPlaylist.resize_clip(clipIndex, previousStart + frameOffset, previousOut + frameOffset);
3099 m_isBlocked = false;
3100 mlt_service_unlock(service.get_service());
3101 m_mltConsumer->set("refresh", 1);
3105 bool Render::mltResizeClipStart(ItemInfo info, GenTime diff)
3107 //kDebug() << "//////// RSIZING CLIP from: "<<info.startPos.frames(25)<<" to "<<diff.frames(25);
3108 Mlt::Service service(m_mltProducer->parent().get_service());
3109 int moveFrame = (int) diff.frames(m_fps);
3110 Mlt::Tractor tractor(service);
3111 Mlt::Producer trackProducer(tractor.track(info.track));
3112 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3113 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
3114 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
3117 mlt_service_lock(service.get_service());
3118 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
3119 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
3120 if (clip == NULL || clip->is_blank()) {
3121 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
3122 mlt_service_unlock(service.get_service());
3125 int previousStart = clip->get_in();
3126 int previousOut = clip->get_out();
3129 previousStart += moveFrame;
3131 if (previousStart < 0) {
3132 // this is possible for images and color clips
3133 previousOut -= previousStart;
3137 int length = previousOut + 1;
3138 if (length > clip->get_length()) {
3139 clip->parent().set("length", length + 1);
3140 clip->parent().set("out", length);
3141 clip->set("length", length + 1);
3145 // kDebug() << "RESIZE, new start: " << previousStart << ", " << previousOut;
3146 trackPlaylist.resize_clip(clipIndex, previousStart, previousOut);
3147 if (moveFrame > 0) {
3148 trackPlaylist.insert_blank(clipIndex, moveFrame - 1);
3150 //int midpos = info.startPos.frames(m_fps) + moveFrame - 1;
3151 int blankIndex = clipIndex - 1;
3152 int blankLength = trackPlaylist.clip_length(blankIndex);
3153 // kDebug() << " + resizing blank length " << blankLength << ", SIZE DIFF: " << moveFrame;
3154 if (! trackPlaylist.is_blank(blankIndex)) {
3155 kDebug() << "WARNING, CLIP TO RESIZE IS NOT BLANK";
3157 if (blankLength + moveFrame == 0)
3158 trackPlaylist.remove(blankIndex);
3160 trackPlaylist.resize_clip(blankIndex, 0, blankLength + moveFrame - 1);
3162 trackPlaylist.consolidate_blanks(0);
3163 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
3164 //mltResizeTransparency(previousStart, (int) moveEnd.frames(m_fps), (int) (moveEnd + out - in).frames(m_fps), track, QString(clip->parent().get("id")).toInt());
3165 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
3166 ItemInfo transpinfo;
3167 transpinfo.startPos = info.startPos + diff;
3168 transpinfo.endPos = info.startPos + diff + (info.endPos - info.startPos);
3169 transpinfo.track = info.track;
3170 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
3172 m_isBlocked = false;
3173 //m_mltConsumer->set("refresh", 1);
3174 mlt_service_unlock(service.get_service());
3175 m_mltConsumer->set("refresh", 1);
3179 bool Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd, Mlt::Producer *prod, bool overwrite, bool insert)
3181 return mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps), prod, overwrite, insert);
3185 bool Render::mltUpdateClipProducer(int track, int pos, Mlt::Producer *prod)
3187 if (prod == NULL || !prod->is_valid()) {
3188 kDebug() << "// Warning, CLIP on track " << track << ", at: " << pos << " is invalid, cannot update it!!!";
3191 kDebug() << "NEW PROD ID: " << prod->get("id");
3193 kDebug() << "// TRYING TO UPDATE CLIP at: " << pos << ", TK: " << track;
3194 Mlt::Service service(m_mltProducer->parent().get_service());
3195 if (service.type() != tractor_type) {
3196 kWarning() << "// TRACTOR PROBLEM";
3199 mlt_service_lock(service.get_service());
3200 Mlt::Tractor tractor(service);
3201 Mlt::Producer trackProducer(tractor.track(track));
3202 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3203 int clipIndex = trackPlaylist.get_clip_index_at(pos);
3204 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3205 if (clipProducer == NULL || clipProducer->is_blank()) {
3206 kDebug() << "// ERROR UPDATING CLIP PROD";
3207 delete clipProducer;
3208 mlt_service_unlock(service.get_service());
3212 Mlt::Producer *clip = prod->cut(clipProducer->get_in(), clipProducer->get_out());
3213 if (!clip || !clip->is_valid()) {
3214 if (clip) delete clip;
3215 delete clipProducer;
3216 mlt_service_unlock(service.get_service());
3220 // move all effects to the correct producer
3221 mltPasteEffects(clipProducer, clip);
3222 trackPlaylist.insert_at(pos, clip, 1);
3224 delete clipProducer;
3225 mlt_service_unlock(service.get_service());
3230 bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd, Mlt::Producer *prod, bool overwrite, bool /*insert*/)
3234 Mlt::Service service(m_mltProducer->parent().get_service());
3235 if (service.type() != tractor_type) {
3236 kWarning() << "// TRACTOR PROBLEM";
3240 Mlt::Tractor tractor(service);
3241 mlt_service_lock(service.get_service());
3242 Mlt::Producer trackProducer(tractor.track(startTrack));
3243 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3244 int clipIndex = trackPlaylist.get_clip_index_at(moveStart);
3245 kDebug() << "////// LOOKING FOR CLIP TO MOVE, INDEX: " << clipIndex;
3246 bool checkLength = false;
3247 if (endTrack == startTrack) {
3248 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3249 if ((!overwrite && !trackPlaylist.is_blank_at(moveEnd)) || !clipProducer || !clipProducer->is_valid() || clipProducer->is_blank()) {
3250 // error, destination is not empty
3252 if (!trackPlaylist.is_blank_at(moveEnd) && clipProducer->is_valid()) trackPlaylist.insert_at(moveStart, clipProducer, 1);
3253 delete clipProducer;
3255 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
3256 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
3257 mlt_service_unlock(service.get_service());
3261 trackPlaylist.consolidate_blanks(0);
3263 trackPlaylist.remove_region(moveEnd, clipProducer->get_playtime());
3264 int clipIndex = trackPlaylist.get_clip_index_at(moveEnd);
3265 trackPlaylist.insert_blank(clipIndex, clipProducer->get_playtime() - 1);
3267 int newIndex = trackPlaylist.insert_at(moveEnd, clipProducer, 1);
3268 trackPlaylist.consolidate_blanks(1);
3269 delete clipProducer;
3270 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
3271 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
3273 if (newIndex + 1 == trackPlaylist.count()) checkLength = true;
3275 //mlt_service_unlock(service.get_service());
3277 Mlt::Producer destTrackProducer(tractor.track(endTrack));
3278 Mlt::Playlist destTrackPlaylist((mlt_playlist) destTrackProducer.get_service());
3279 if (!overwrite && !destTrackPlaylist.is_blank_at(moveEnd)) {
3280 // error, destination is not empty
3281 mlt_service_unlock(service.get_service());
3285 Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
3286 if (!clipProducer || clipProducer->is_blank()) {
3287 // error, destination is not empty
3288 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
3289 if (clipProducer) delete clipProducer;
3290 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
3291 mlt_service_unlock(service.get_service());
3295 trackPlaylist.consolidate_blanks(0);
3296 destTrackPlaylist.consolidate_blanks(1);
3297 Mlt::Producer *clip;
3298 // check if we are moving a slowmotion producer
3299 QString serv = clipProducer->parent().get("mlt_service");
3300 QString currentid = clipProducer->parent().get("id");
3301 if (serv == "framebuffer" || currentid.endsWith("_video")) {
3302 clip = clipProducer;
3305 // Special case: prod is null when using placeholder clips.
3306 // in that case, use the producer existing in playlist. Note that
3307 // it will bypass the one producer per track logic and might cause
3308 // Sound cracks if clip is moved so that it overlaps another copy of itself
3309 clip = clipProducer->cut(clipProducer->get_in(), clipProducer->get_out());
3310 } else clip = prod->cut(clipProducer->get_in(), clipProducer->get_out());
3313 // move all effects to the correct producer
3314 mltPasteEffects(clipProducer, clip);
3317 destTrackPlaylist.remove_region(moveEnd, clip->get_playtime());
3318 int clipIndex = destTrackPlaylist.get_clip_index_at(moveEnd);
3319 destTrackPlaylist.insert_blank(clipIndex, clip->get_playtime() - 1);
3322 int newIndex = destTrackPlaylist.insert_at(moveEnd, clip, 1);
3324 if (clip == clipProducer) {
3329 delete clipProducer;
3331 destTrackPlaylist.consolidate_blanks(0);
3332 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
3333 kDebug() << "//////// moving clip transparency";
3334 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
3336 if (clipIndex > trackPlaylist.count()) checkLength = true;
3337 else if (newIndex + 1 == destTrackPlaylist.count()) checkLength = true;
3340 mlt_service_unlock(service.get_service());
3341 if (checkLength) mltCheckLength(&tractor);
3344 //m_mltConsumer->set("refresh", 1);
3349 QList <int> Render::checkTrackSequence(int track)
3352 Mlt::Service service(m_mltProducer->parent().get_service());
3353 if (service.type() != tractor_type) {
3354 kWarning() << "// TRACTOR PROBLEM";
3357 Mlt::Tractor tractor(service);
3358 mlt_service_lock(service.get_service());
3359 Mlt::Producer trackProducer(tractor.track(track));
3360 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3361 int clipNb = trackPlaylist.count();
3362 //kDebug() << "// PARSING SCENE TRACK: " << t << ", CLIPS: " << clipNb;
3363 for (int i = 0; i < clipNb; i++) {
3364 Mlt::Producer *c = trackPlaylist.get_clip(i);
3365 int pos = trackPlaylist.clip_start(i);
3366 if (!list.contains(pos)) list.append(pos);
3367 pos += c->get_playtime();
3368 if (!list.contains(pos)) list.append(pos);
3374 bool Render::mltMoveTransition(QString type, int startTrack, int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut)
3376 int new_in = (int)newIn.frames(m_fps);
3377 int new_out = (int)newOut.frames(m_fps) - 1;
3378 if (new_in >= new_out) return false;
3379 int old_in = (int)oldIn.frames(m_fps);
3380 int old_out = (int)oldOut.frames(m_fps) - 1;
3382 Mlt::Service service(m_mltProducer->parent().get_service());
3383 Mlt::Tractor tractor(service);
3384 Mlt::Field *field = tractor.field();
3386 bool doRefresh = true;
3387 // Check if clip is visible in monitor
3388 int diff = old_out - m_mltProducer->position();
3389 if (diff < 0 || diff > old_out - old_in) doRefresh = false;
3391 diff = new_out - m_mltProducer->position();
3392 if (diff < 0 || diff > new_out - new_in) doRefresh = false;
3396 mlt_service_lock(service.get_service());
3398 mlt_service nextservice = mlt_service_get_producer(service.get_service());
3399 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3400 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3401 QString resource = mlt_properties_get(properties, "mlt_service");
3402 int old_pos = (int)(old_in + old_out) / 2;
3405 while (mlt_type == "transition") {
3406 Mlt::Transition transition((mlt_transition) nextservice);
3407 int currentTrack = transition.get_b_track();
3408 int currentIn = (int) transition.get_in();
3409 int currentOut = (int) transition.get_out();
3411 if (resource == type && startTrack == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
3413 if (newTrack - startTrack != 0) {
3414 Mlt::Properties trans_props(transition.get_properties());
3415 Mlt::Transition new_transition(*m_mltProfile, transition.get("mlt_service"));
3416 Mlt::Properties new_trans_props(new_transition.get_properties());
3417 new_trans_props.inherit(trans_props);
3418 new_transition.set_in_and_out(new_in, new_out);
3419 field->disconnect_service(transition);
3420 mltPlantTransition(field, new_transition, newTransitionTrack, newTrack);
3421 //field->plant_transition(new_transition, newTransitionTrack, newTrack);
3422 } else transition.set_in_and_out(new_in, new_out);
3425 nextservice = mlt_service_producer(nextservice);
3426 if (nextservice == NULL) break;
3427 properties = MLT_SERVICE_PROPERTIES(nextservice);
3428 mlt_type = mlt_properties_get(properties, "mlt_type");
3429 resource = mlt_properties_get(properties, "mlt_service");
3431 mlt_service_unlock(service.get_service());
3433 if (doRefresh) refresh();
3434 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3439 void Render::mltPlantTransition(Mlt::Field *field, Mlt::Transition &tr, int a_track, int b_track)
3441 mlt_service nextservice = mlt_service_get_producer(field->get_service());
3442 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3443 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3444 QString resource = mlt_properties_get(properties, "mlt_service");
3445 QList <Mlt::Transition *> trList;
3447 while (mlt_type == "transition") {
3448 Mlt::Transition transition((mlt_transition) nextservice);
3449 int aTrack = transition.get_a_track();
3450 int bTrack = transition.get_b_track();
3451 if (resource != "mix" && (aTrack < a_track || (aTrack == a_track && bTrack > b_track))) {
3452 Mlt::Properties trans_props(transition.get_properties());
3453 Mlt::Transition *cp = new Mlt::Transition(*m_mltProfile, transition.get("mlt_service"));
3454 Mlt::Properties new_trans_props(cp->get_properties());
3455 new_trans_props.inherit(trans_props);
3457 field->disconnect_service(transition);
3459 //else kDebug() << "// FOUND TRANS OK, "<<resource<< ", A_: " << aTrack << ", B_ "<<bTrack;
3461 nextservice = mlt_service_producer(nextservice);
3462 if (nextservice == NULL) break;
3463 properties = MLT_SERVICE_PROPERTIES(nextservice);
3464 mlt_type = mlt_properties_get(properties, "mlt_type");
3465 resource = mlt_properties_get(properties, "mlt_service");
3467 field->plant_transition(tr, a_track, b_track);
3469 // re-add upper transitions
3470 for (int i = trList.count() - 1; i >= 0; i--) {
3471 //kDebug()<< "REPLANT ON TK: "<<trList.at(i)->get_a_track()<<", "<<trList.at(i)->get_b_track();
3472 field->plant_transition(*trList.at(i), trList.at(i)->get_a_track(), trList.at(i)->get_b_track());
3476 void Render::mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool force)
3478 if (oldTag == tag && !force) mltUpdateTransitionParams(tag, a_track, b_track, in, out, xml);
3480 kDebug()<<"// DELETING TRANS: "<<a_track<<"-"<<b_track;
3481 mltDeleteTransition(oldTag, a_track, b_track, in, out, xml, false);
3482 mltAddTransition(tag, a_track, b_track, in, out, xml, false);
3485 if (m_mltProducer->position() >= in.frames(m_fps) && m_mltProducer->position() <= out.frames(m_fps)) refresh();
3488 void Render::mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml)
3490 mlt_service serv = m_mltProducer->parent().get_service();
3491 mlt_service_lock(serv);
3494 mlt_service nextservice = mlt_service_get_producer(serv);
3495 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3496 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3497 QString resource = mlt_properties_get(properties, "mlt_service");
3498 int in_pos = (int) in.frames(m_fps);
3499 int out_pos = (int) out.frames(m_fps) - 1;
3501 while (mlt_type == "transition") {
3502 mlt_transition tr = (mlt_transition) nextservice;
3503 int currentTrack = mlt_transition_get_b_track(tr);
3504 int currentBTrack = mlt_transition_get_a_track(tr);
3505 int currentIn = (int) mlt_transition_get_in(tr);
3506 int currentOut = (int) mlt_transition_get_out(tr);
3508 // kDebug()<<"Looking for transition : " << currentIn <<'x'<<currentOut<< ", OLD oNE: "<<in_pos<<'x'<<out_pos;
3509 if (resource == type && b_track == currentTrack && currentIn == in_pos && currentOut == out_pos) {
3510 QMap<QString, QString> map = mltGetTransitionParamsFromXml(xml);
3511 QMap<QString, QString>::Iterator it;
3513 mlt_properties transproperties = MLT_TRANSITION_PROPERTIES(tr);
3515 QString currentId = mlt_properties_get(transproperties, "kdenlive_id");
3516 if (currentId != xml.attribute("id")) {
3517 // The transition ID is not the same, so reset all properties
3518 mlt_properties_set(transproperties, "kdenlive_id", xml.attribute("id").toUtf8().constData());
3519 // Cleanup previous properties
3520 QStringList permanentProps;
3521 permanentProps << "factory" << "kdenlive_id" << "mlt_service" << "mlt_type" << "in";
3522 permanentProps << "out" << "a_track" << "b_track";
3523 for (int i = 0; i < mlt_properties_count(transproperties); i++) {
3524 QString propName = mlt_properties_get_name(transproperties, i);
3525 if (!propName.startsWith('_') && ! permanentProps.contains(propName)) {
3526 mlt_properties_set(transproperties, propName.toUtf8().constData(), "");
3531 mlt_properties_set_int(transproperties, "force_track", xml.attribute("force_track").toInt());
3532 mlt_properties_set_int(transproperties, "automatic", xml.attribute("automatic", "0").toInt());
3534 if (currentBTrack != a_track) {
3535 mlt_properties_set_int(transproperties, "a_track", a_track);
3537 for (it = map.begin(); it != map.end(); ++it) {
3539 mlt_properties_set(transproperties, key.toUtf8().constData(), it.value().toUtf8().constData());
3540 //kDebug() << " ------ UPDATING TRANS PARAM: " << key.toUtf8().constData() << ": " << it.value().toUtf8().constData();
3541 //filter->set("kdenlive_id", id);
3545 nextservice = mlt_service_producer(nextservice);
3546 if (nextservice == NULL) break;
3547 properties = MLT_SERVICE_PROPERTIES(nextservice);
3548 mlt_type = mlt_properties_get(properties, "mlt_type");
3549 resource = mlt_properties_get(properties, "mlt_service");
3551 mlt_service_unlock(serv);
3554 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3557 void Render::mltDeleteTransition(QString tag, int /*a_track*/, int b_track, GenTime in, GenTime out, QDomElement /*xml*/, bool /*do_refresh*/)
3559 mlt_service serv = m_mltProducer->parent().get_service();
3561 mlt_service_lock(serv);
3563 Mlt::Service service(serv);
3564 Mlt::Tractor tractor(service);
3565 Mlt::Field *field = tractor.field();
3567 //if (do_refresh) m_mltConsumer->set("refresh", 0);
3569 mlt_service nextservice = mlt_service_get_producer(serv);
3570 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3571 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3572 QString resource = mlt_properties_get(properties, "mlt_service");
3574 const int old_pos = (int)((in + out).frames(m_fps) / 2);
3575 kDebug() << " del trans pos: " << in.frames(25) << "-" << out.frames(25);
3577 while (mlt_type == "transition") {
3578 mlt_transition tr = (mlt_transition) nextservice;
3579 int currentTrack = mlt_transition_get_b_track(tr);
3580 int currentIn = (int) mlt_transition_get_in(tr);
3581 int currentOut = (int) mlt_transition_get_out(tr);
3582 //kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
3584 if (resource == tag && b_track == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
3585 mlt_field_disconnect_service(field->get_field(), nextservice);
3588 nextservice = mlt_service_producer(nextservice);
3589 if (nextservice == NULL) break;
3590 properties = MLT_SERVICE_PROPERTIES(nextservice);
3591 mlt_type = mlt_properties_get(properties, "mlt_type");
3592 resource = mlt_properties_get(properties, "mlt_service");
3594 mlt_service_unlock(serv);
3597 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3600 QMap<QString, QString> Render::mltGetTransitionParamsFromXml(QDomElement xml)
3602 QDomNodeList attribs = xml.elementsByTagName("parameter");
3603 QMap<QString, QString> map;
3604 for (int i = 0; i < attribs.count(); i++) {
3605 QDomElement e = attribs.item(i).toElement();
3606 QString name = e.attribute("name");
3607 //kDebug()<<"-- TRANSITION PARAM: "<<name<<" = "<< e.attribute("name")<<" / " << e.attribute("value");
3608 map[name] = e.attribute("default");
3609 if (!e.attribute("value").isEmpty()) {
3610 map[name] = e.attribute("value");
3612 if (e.attribute("type") != "addedgeometry" && !e.attribute("factor").isEmpty() && e.attribute("factor").toDouble() > 0) {
3613 map[name] = QString::number(map.value(name).toDouble() / e.attribute("factor").toDouble());
3614 //map[name]=map[name].replace(".",","); //FIXME how to solve locale conversion of . ,
3617 if (e.attribute("namedesc").contains(';')) {
3618 QString format = e.attribute("format");
3619 QStringList separators = format.split("%d", QString::SkipEmptyParts);
3620 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
3622 QTextStream txtNeu(&neu);
3623 if (values.size() > 0)
3624 txtNeu << (int)values[0].toDouble();
3626 for (i = 0; i < separators.size() && i + 1 < values.size(); i++) {
3627 txtNeu << separators[i];
3628 txtNeu << (int)(values[i+1].toDouble());
3630 if (i < separators.size())
3631 txtNeu << separators[i];
3632 map[e.attribute("name")] = neu;
3639 void Render::mltAddClipTransparency(ItemInfo info, int transitiontrack, int id)
3641 kDebug() << "///////// ADDING CLIP TRANSPARENCY AT: " << info.startPos.frames(25);
3642 Mlt::Service service(m_mltProducer->parent().get_service());
3643 Mlt::Tractor tractor(service);
3644 Mlt::Field *field = tractor.field();
3646 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
3647 transition->set_in_and_out((int) info.startPos.frames(m_fps), (int) info.endPos.frames(m_fps) - 1);
3648 transition->set("transparency", id);
3649 transition->set("fill", 1);
3650 transition->set("internal_added", 237);
3651 field->plant_transition(*transition, transitiontrack, info.track);
3655 void Render::mltDeleteTransparency(int pos, int track, int id)
3657 Mlt::Service service(m_mltProducer->parent().get_service());
3658 Mlt::Tractor tractor(service);
3659 Mlt::Field *field = tractor.field();
3661 //if (do_refresh) m_mltConsumer->set("refresh", 0);
3662 mlt_service serv = m_mltProducer->parent().get_service();
3664 mlt_service nextservice = mlt_service_get_producer(serv);
3665 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3666 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3667 QString resource = mlt_properties_get(properties, "mlt_service");
3669 while (mlt_type == "transition") {
3670 mlt_transition tr = (mlt_transition) nextservice;
3671 int currentTrack = mlt_transition_get_b_track(tr);
3672 int currentIn = (int) mlt_transition_get_in(tr);
3673 int currentOut = (int) mlt_transition_get_out(tr);
3674 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3675 kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
3677 if (resource == "composite" && track == currentTrack && currentIn == pos && transitionId == id) {
3678 //kDebug() << " / / / / /DELETE TRANS DOOOMNE";
3679 mlt_field_disconnect_service(field->get_field(), nextservice);
3682 nextservice = mlt_service_producer(nextservice);
3683 if (nextservice == NULL) break;
3684 properties = MLT_SERVICE_PROPERTIES(nextservice);
3685 mlt_type = mlt_properties_get(properties, "mlt_type");
3686 resource = mlt_properties_get(properties, "mlt_service");
3688 //if (do_refresh) m_mltConsumer->set("refresh", 1);
3691 void Render::mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id)
3693 Mlt::Service service(m_mltProducer->parent().get_service());
3694 Mlt::Tractor tractor(service);
3696 mlt_service_lock(service.get_service());
3697 m_mltConsumer->set("refresh", 0);
3700 mlt_service serv = m_mltProducer->parent().get_service();
3701 mlt_service nextservice = mlt_service_get_producer(serv);
3702 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3703 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3704 QString resource = mlt_properties_get(properties, "mlt_service");
3705 kDebug() << "// resize transpar from: " << oldStart << ", TO: " << newStart << 'x' << newEnd << ", " << track << ", " << id;
3706 while (mlt_type == "transition") {
3707 mlt_transition tr = (mlt_transition) nextservice;
3708 int currentTrack = mlt_transition_get_b_track(tr);
3709 int currentIn = (int) mlt_transition_get_in(tr);
3710 //mlt_properties props = MLT_TRANSITION_PROPERTIES(tr);
3711 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3712 kDebug() << "// resize transpar current in: " << currentIn << ", Track: " << currentTrack << ", id: " << id << 'x' << transitionId ;
3713 if (resource == "composite" && track == currentTrack && currentIn == oldStart && transitionId == id) {
3714 kDebug() << " / / / / /RESIZE TRANS TO: " << newStart << 'x' << newEnd;
3715 mlt_transition_set_in_and_out(tr, newStart, newEnd);
3718 nextservice = mlt_service_producer(nextservice);
3719 if (nextservice == NULL) break;
3720 properties = MLT_SERVICE_PROPERTIES(nextservice);
3721 mlt_type = mlt_properties_get(properties, "mlt_type");
3722 resource = mlt_properties_get(properties, "mlt_service");
3724 mlt_service_unlock(service.get_service());
3726 if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
3730 void Render::mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id)
3732 Mlt::Service service(m_mltProducer->parent().get_service());
3733 Mlt::Tractor tractor(service);
3735 mlt_service_lock(service.get_service());
3736 m_mltConsumer->set("refresh", 0);
3739 mlt_service serv = m_mltProducer->parent().get_service();
3740 mlt_service nextservice = mlt_service_get_producer(serv);
3741 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3742 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3743 QString resource = mlt_properties_get(properties, "mlt_service");
3745 while (mlt_type == "transition") {
3746 mlt_transition tr = (mlt_transition) nextservice;
3747 int currentTrack = mlt_transition_get_b_track(tr);
3748 int currentaTrack = mlt_transition_get_a_track(tr);
3749 int currentIn = (int) mlt_transition_get_in(tr);
3750 int currentOut = (int) mlt_transition_get_out(tr);
3751 //mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3752 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
3753 //kDebug()<<" + TRANSITION "<<id<<" == "<<transitionId<<", START TMIE: "<<currentIn<<", LOOK FR: "<<startTime<<", TRACK: "<<currentTrack<<'x'<<startTrack;
3754 if (resource == "composite" && transitionId == id && startTime == currentIn && startTrack == currentTrack) {
3755 kDebug() << "//////MOVING";
3756 mlt_transition_set_in_and_out(tr, endTime, endTime + currentOut - currentIn);
3757 if (endTrack != startTrack) {
3758 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3759 mlt_properties_set_int(properties, "a_track", currentaTrack + endTrack - currentTrack);
3760 mlt_properties_set_int(properties, "b_track", endTrack);
3764 nextservice = mlt_service_producer(nextservice);
3765 if (nextservice == NULL) break;
3766 properties = MLT_SERVICE_PROPERTIES(nextservice);
3767 mlt_type = mlt_properties_get(properties, "mlt_type");
3768 resource = mlt_properties_get(properties, "mlt_service");
3771 mlt_service_unlock(service.get_service());
3772 m_mltConsumer->set("refresh", 1);
3776 bool Render::mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool do_refresh)
3778 if (in >= out) return false;
3779 QMap<QString, QString> args = mltGetTransitionParamsFromXml(xml);
3780 Mlt::Service service(m_mltProducer->parent().get_service());
3782 Mlt::Tractor tractor(service);
3783 Mlt::Field *field = tractor.field();
3785 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, tag.toUtf8().constData());
3786 if (out != GenTime())
3787 transition->set_in_and_out((int) in.frames(m_fps), (int) out.frames(m_fps) - 1);
3789 if (do_refresh && (m_mltProducer->position() < in.frames(m_fps) || m_mltProducer->position() > out.frames(m_fps))) do_refresh = false;
3790 QMap<QString, QString>::Iterator it;
3792 if (xml.attribute("automatic") == "1") transition->set("automatic", 1);
3793 //kDebug() << " ------ ADDING TRANSITION PARAMs: " << args.count();
3794 if (xml.hasAttribute("id"))
3795 transition->set("kdenlive_id", xml.attribute("id").toUtf8().constData());
3796 if (xml.hasAttribute("force_track"))
3797 transition->set("force_track", xml.attribute("force_track").toInt());
3799 for (it = args.begin(); it != args.end(); ++it) {
3801 if (!it.value().isEmpty())
3802 transition->set(key.toUtf8().constData(), it.value().toUtf8().constData());
3803 //kDebug() << " ------ ADDING TRANS PARAM: " << key << ": " << it.value();
3805 // attach transition
3807 mlt_service_lock(service.get_service());
3808 mltPlantTransition(field, *transition, a_track, b_track);
3809 // field->plant_transition(*transition, a_track, b_track);
3810 mlt_service_unlock(service.get_service());
3812 if (do_refresh) refresh();
3816 void Render::mltSavePlaylist()
3818 kWarning() << "// UPDATING PLAYLIST TO DISK++++++++++++++++";
3819 Mlt::Consumer fileConsumer(*m_mltProfile, "xml");
3820 fileConsumer.set("resource", "/tmp/playlist.mlt");
3822 Mlt::Service service(m_mltProducer->get_service());
3824 fileConsumer.connect(service);
3825 fileConsumer.start();
3828 const QList <Mlt::Producer *> Render::producersList()
3830 QList <Mlt::Producer *> prods;
3831 if (m_mltProducer == NULL) return prods;
3832 Mlt::Service service(m_mltProducer->parent().get_service());
3833 if (service.type() != tractor_type) return prods;
3834 Mlt::Tractor tractor(service);
3837 int trackNb = tractor.count();
3838 for (int t = 1; t < trackNb; t++) {
3839 Mlt::Producer *tt = tractor.track(t);
3840 Mlt::Producer trackProducer(tt);
3842 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3843 int clipNb = trackPlaylist.count();
3844 for (int i = 0; i < clipNb; i++) {
3845 Mlt::Producer *c = trackPlaylist.get_clip(i);
3846 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
3848 QString prodId = nprod->get("id");
3849 if (!prodId.startsWith("slowmotion") && !prodId.isEmpty() && !nprod->is_blank() && !ids.contains(prodId)) {
3850 ids.append(nprod->get("id"));
3851 prods.append(nprod);
3852 } else delete nprod;
3860 void Render::fillSlowMotionProducers()
3862 if (m_mltProducer == NULL) return;
3863 Mlt::Service service(m_mltProducer->parent().get_service());
3864 if (service.type() != tractor_type) return;
3866 Mlt::Tractor tractor(service);
3868 int trackNb = tractor.count();
3869 for (int t = 1; t < trackNb; t++) {
3870 Mlt::Producer *tt = tractor.track(t);
3871 Mlt::Producer trackProducer(tt);
3873 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
3874 int clipNb = trackPlaylist.count();
3875 for (int i = 0; i < clipNb; i++) {
3876 Mlt::Producer *c = trackPlaylist.get_clip(i);
3877 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
3879 QString id = nprod->get("id");
3880 if (id.startsWith("slowmotion:") && !nprod->is_blank()) {
3881 // this is a slowmotion producer, add it to the list
3882 QString url = QString::fromUtf8(nprod->get("resource"));
3883 int strobe = nprod->get_int("strobe");
3884 if (strobe > 1) url.append("&strobe=" + QString::number(strobe));
3885 if (!m_slowmotionProducers.contains(url)) {
3886 m_slowmotionProducers.insert(url, nprod);
3888 } else delete nprod;
3895 void Render::mltInsertTrack(int ix, bool videoTrack)
3900 Mlt::Service service(m_mltProducer->parent().get_service());
3901 mlt_service_lock(service.get_service());
3902 if (service.type() != tractor_type) {
3903 kWarning() << "// TRACTOR PROBLEM";
3907 Mlt::Tractor tractor(service);
3909 Mlt::Playlist playlist;
3910 int ct = tractor.count();
3912 kDebug() << "// ERROR, TRYING TO insert TRACK " << ix << ", max: " << ct;
3918 Mlt::Producer *prodToMove = new Mlt::Producer(tractor.track(pos));
3919 tractor.set_track(playlist, pos);
3920 Mlt::Producer newProd(tractor.track(pos));
3921 if (!videoTrack) newProd.set("hide", 1);
3923 for (; pos <= ct; pos++) {
3924 Mlt::Producer *prodToMove2 = new Mlt::Producer(tractor.track(pos));
3925 tractor.set_track(*prodToMove, pos);
3926 prodToMove = prodToMove2;
3929 tractor.set_track(playlist, ix);
3930 Mlt::Producer newProd(tractor.track(ix));
3931 if (!videoTrack) newProd.set("hide", 1);
3935 mlt_service serv = m_mltProducer->parent().get_service();
3936 mlt_service nextservice = mlt_service_get_producer(serv);
3937 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3938 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3939 QString resource = mlt_properties_get(properties, "mlt_service");
3941 while (mlt_type == "transition") {
3942 if (resource != "mix") {
3943 mlt_transition tr = (mlt_transition) nextservice;
3944 int currentTrack = mlt_transition_get_b_track(tr);
3945 int currentaTrack = mlt_transition_get_a_track(tr);
3946 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3948 if (currentTrack >= ix) {
3949 mlt_properties_set_int(properties, "b_track", currentTrack + 1);
3950 mlt_properties_set_int(properties, "a_track", currentaTrack + 1);
3953 nextservice = mlt_service_producer(nextservice);
3954 if (nextservice == NULL) break;
3955 properties = MLT_SERVICE_PROPERTIES(nextservice);
3956 mlt_type = mlt_properties_get(properties, "mlt_type");
3957 resource = mlt_properties_get(properties, "mlt_service");
3960 // Add audio mix transition to last track
3961 Mlt::Field *field = tractor.field();
3962 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "mix");
3963 transition->set("a_track", 1);
3964 transition->set("b_track", ct);
3965 transition->set("always_active", 1);
3966 transition->set("internal_added", 237);
3967 transition->set("combine", 1);
3968 field->plant_transition(*transition, 1, ct);
3969 //mlt_service_unlock(m_mltConsumer->get_service());
3970 mlt_service_unlock(service.get_service());
3971 //tractor.multitrack()->refresh();
3972 //tractor.refresh();
3974 blockSignals(false);
3978 void Render::mltDeleteTrack(int ix)
3981 doc.setContent(sceneList(), false);
3982 int tracksCount = doc.elementsByTagName("track").count() - 1;
3983 QDomNode track = doc.elementsByTagName("track").at(ix);
3984 QDomNode tractor = doc.elementsByTagName("tractor").at(0);
3985 QDomNodeList transitions = doc.elementsByTagName("transition");
3986 for (int i = 0; i < transitions.count(); i++) {
3987 QDomElement e = transitions.at(i).toElement();
3988 QDomNodeList props = e.elementsByTagName("property");
3989 QMap <QString, QString> mappedProps;
3990 for (int j = 0; j < props.count(); j++) {
3991 QDomElement f = props.at(j).toElement();
3992 mappedProps.insert(f.attribute("name"), f.firstChild().nodeValue());
3994 if (mappedProps.value("mlt_service") == "mix" && mappedProps.value("b_track").toInt() == tracksCount) {
3995 tractor.removeChild(transitions.at(i));
3997 } else if (mappedProps.value("mlt_service") != "mix" && (mappedProps.value("b_track").toInt() >= ix || mappedProps.value("a_track").toInt() >= ix)) {
3998 // Transition needs to be moved
3999 int a_track = mappedProps.value("a_track").toInt();
4000 int b_track = mappedProps.value("b_track").toInt();
4001 if (a_track > 0 && a_track >= ix) a_track --;
4002 if (b_track == ix) {
4003 // transition was on the deleted track, so remove it
4004 tractor.removeChild(transitions.at(i));
4008 if (b_track > 0 && b_track > ix) b_track --;
4009 for (int j = 0; j < props.count(); j++) {
4010 QDomElement f = props.at(j).toElement();
4011 if (f.attribute("name") == "a_track") f.firstChild().setNodeValue(QString::number(a_track));
4012 else if (f.attribute("name") == "b_track") f.firstChild().setNodeValue(QString::number(b_track));
4017 tractor.removeChild(track);
4018 //kDebug() << "/////////// RESULT SCENE: \n" << doc.toString();
4019 setSceneList(doc.toString(), m_framePosition);
4020 emit refreshDocumentProducers(false, false);
4024 void Render::updatePreviewSettings()
4026 kDebug() << "////// RESTARTING CONSUMER";
4027 if (!m_mltConsumer || !m_mltProducer) return;
4028 if (m_mltProducer->get_playtime() == 0) return;
4029 Mlt::Service service(m_mltProducer->parent().get_service());
4030 if (service.type() != tractor_type) return;
4032 //m_mltConsumer->set("refresh", 0);
4033 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
4034 m_mltConsumer->purge();
4035 QString scene = sceneList();
4037 if (m_mltProducer) {
4038 pos = m_mltProducer->position();
4041 setSceneList(scene, pos);
4045 QString Render::updateSceneListFps(double current_fps, double new_fps, QString scene)
4047 // Update all frame positions to the new fps value
4048 //WARNING: there are probably some effects or other that hold a frame value
4049 // as parameter and will also need to be updated here!
4051 doc.setContent(scene);
4053 double factor = new_fps / current_fps;
4054 QDomNodeList producers = doc.elementsByTagName("producer");
4055 for (int i = 0; i < producers.count(); i++) {
4056 QDomElement prod = producers.at(i).toElement();
4057 prod.removeAttribute("in");
4058 prod.removeAttribute("out");
4060 QDomNodeList props = prod.childNodes();
4061 for (int j = 0; j < props.count(); j++) {
4062 QDomElement param = props.at(j).toElement();
4063 QString paramName = param.attribute("name");
4064 if (paramName.startsWith("meta.") || paramName == "length") {
4065 prod.removeChild(props.at(j));
4071 QDomNodeList entries = doc.elementsByTagName("entry");
4072 for (int i = 0; i < entries.count(); i++) {
4073 QDomElement entry = entries.at(i).toElement();
4074 int in = entry.attribute("in").toInt();
4075 int out = entry.attribute("out").toInt();
4076 in = factor * in + 0.5;
4077 out = factor * out + 0.5;
4078 entry.setAttribute("in", in);
4079 entry.setAttribute("out", out);
4082 QDomNodeList blanks = doc.elementsByTagName("blank");
4083 for (int i = 0; i < blanks.count(); i++) {
4084 QDomElement blank = blanks.at(i).toElement();
4085 int length = blank.attribute("length").toInt();
4086 length = factor * length + 0.5;
4087 blank.setAttribute("length", QString::number(length));
4090 QDomNodeList filters = doc.elementsByTagName("filter");
4091 for (int i = 0; i < filters.count(); i++) {
4092 QDomElement filter = filters.at(i).toElement();
4093 int in = filter.attribute("in").toInt();
4094 int out = filter.attribute("out").toInt();
4095 in = factor * in + 0.5;
4096 out = factor * out + 0.5;
4097 filter.setAttribute("in", in);
4098 filter.setAttribute("out", out);
4101 QDomNodeList transitions = doc.elementsByTagName("transition");
4102 for (int i = 0; i < transitions.count(); i++) {
4103 QDomElement transition = transitions.at(i).toElement();
4104 int in = transition.attribute("in").toInt();
4105 int out = transition.attribute("out").toInt();
4106 in = factor * in + 0.5;
4107 out = factor * out + 0.5;
4108 transition.setAttribute("in", in);
4109 transition.setAttribute("out", out);
4110 QDomNodeList props = transition.childNodes();
4111 for (int j = 0; j < props.count(); j++) {
4112 QDomElement param = props.at(j).toElement();
4113 QString paramName = param.attribute("name");
4114 if (paramName == "geometry") {
4115 QString geom = param.firstChild().nodeValue();
4116 QStringList keys = geom.split(';');
4117 QStringList newKeys;
4118 for (int k = 0; k < keys.size(); ++k) {
4119 if (keys.at(k).contains('=')) {
4120 int pos = keys.at(k).section('=', 0, 0).toInt();
4121 pos = factor * pos + 0.5;
4122 newKeys.append(QString::number(pos) + '=' + keys.at(k).section('=', 1));
4123 } else newKeys.append(keys.at(k));
4125 param.firstChild().setNodeValue(newKeys.join(";"));
4129 QDomElement tractor = doc.elementsByTagName("tractor").at(0).toElement();
4130 int out = tractor.attribute("out").toInt();
4131 out = factor * out + 0.5;
4132 tractor.setAttribute("out", out);
4133 emit durationChanged(out);
4135 //kDebug() << "///////////////////////////// " << out << " \n" << doc.toString() << "\n-------------------------";
4136 return doc.toString();
4140 void Render::sendFrameUpdate()
4142 if (m_mltProducer) {
4143 Mlt::Frame * frame = m_mltProducer->get_frame();
4144 emitFrameUpdated(*frame);
4149 Mlt::Producer* Render::getProducer()
4151 return m_mltProducer;
4155 #include "renderer.moc"