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"
31 #include <mlt++/Mlt.h>
34 #include <KStandardDirs>
35 #include <KMessageBox>
37 #include <KTemporaryFile>
41 #include <QApplication>
47 static void kdenlive_callback(void* /*ptr*/, int level, const char* fmt, va_list vl)
49 if (level > MLT_LOG_ERROR) return;
51 QApplication::postEvent(qApp->activeWindow() , new MltErrorEvent(error.vsprintf(fmt, vl).simplified()));
55 static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr)
57 // detect if the producer has finished playing. Is there a better way to do it?
58 if (self->m_isBlocked) return;
59 if (mlt_properties_get_double(MLT_FRAME_PROPERTIES(frame_ptr), "_speed") == 0.0) {
60 self->emitConsumerStopped();
62 self->emitFrameNumber(mlt_frame_get_position(frame_ptr));
66 Render::Render(const QString & rendererName, int winid, int /* extid */, QWidget *parent) :
79 kDebug() << "////////// USING PROFILE: " << (char*)KdenliveSettings::current_profile().toUtf8().data();
80 m_refreshTimer = new QTimer(this);
81 connect(m_refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
83 /*if (rendererName == "project") m_monitorId = 10000;
84 else m_monitorId = 10001;*/
85 m_osdTimer = new QTimer(this);
86 connect(m_osdTimer, SIGNAL(timeout()), this, SLOT(slotOsdTimeout()));
90 m_mltProducer = m_blackClip->cut(0, 50);
91 m_mltConsumer->connect(*m_mltProducer);
92 m_mltProducer->set_speed(0.0);
102 void Render::closeMlt()
105 delete m_refreshTimer;
106 delete m_mltConsumer;
107 delete m_mltProducer;
113 void Render::buildConsumer()
116 m_activeProfile = KdenliveSettings::current_profile();
117 tmp = decodedString(m_activeProfile);
118 setenv("MLT_PROFILE", tmp, 1);
122 m_mltProfile = new Mlt::Profile(tmp);
125 QString videoDriver = KdenliveSettings::videodrivername();
126 if (!videoDriver.isEmpty()) {
127 if (videoDriver == "x11_noaccel") {
128 setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
131 unsetenv("SDL_VIDEO_YUV_HWACCEL");
134 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
136 m_mltConsumer = new Mlt::Consumer(*m_mltProfile , "sdl_preview");
137 m_mltConsumer->set("resize", 1);
138 m_mltConsumer->set("window_id", m_winid);
139 m_mltConsumer->set("terminate_on_pause", 1);
140 tmp = decodedString(KdenliveSettings::window_background().name());
141 m_mltConsumer->set("window_background", tmp);
144 // FIXME: the event object returned by the listen gets leaked...
145 m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_show);
146 m_mltConsumer->set("rescale", "nearest");
147 mlt_log_set_callback(kdenlive_callback);
149 QString audioDevice = KdenliveSettings::audiodevicename();
150 if (!audioDevice.isEmpty()) {
151 tmp = decodedString(audioDevice);
152 m_mltConsumer->set("audio_device", tmp);
156 if (!videoDriver.isEmpty()) {
157 tmp = decodedString(videoDriver);
158 m_mltConsumer->set("video_driver", tmp);
162 QString audioDriver = KdenliveSettings::audiodrivername();
165 // Disabled because the "auto" detected driver was sometimes wrong
166 if (audioDriver.isEmpty())
167 audioDriver = KdenliveSettings::autoaudiodrivername();
170 if (!audioDriver.isEmpty()) {
171 tmp = decodedString(audioDriver);
172 m_mltConsumer->set("audio_driver", tmp);
177 m_mltConsumer->set("progressive", 1);
178 m_mltConsumer->set("audio_buffer", 1024);
179 m_mltConsumer->set("frequency", 48000);
181 m_blackClip = new Mlt::Producer(*m_mltProfile , "colour", "black");
182 m_blackClip->set("id", "black");
186 int Render::resetProfile()
188 if (!m_mltConsumer) return 0;
189 if (m_activeProfile == KdenliveSettings::current_profile()) {
190 kDebug() << "reset to same profile, nothing to do";
193 kDebug() << "// RESETTING PROFILE FROM: " << m_activeProfile << " TO: " << KdenliveSettings::current_profile();
194 if (m_isSplitView) slotSplitView(false);
195 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
196 m_mltConsumer->purge();
197 delete m_mltConsumer;
198 m_mltConsumer = NULL;
199 QString scene = sceneList();
202 pos = m_mltProducer->position();
203 delete m_mltProducer;
205 m_mltProducer = NULL;
207 //WARNING: Trying to delete the profile will crash when trying to display a clip afterwards...
208 /*if (m_mltProfile) delete m_mltProfile;
209 m_mltProfile = NULL;*/
213 //kDebug() << "//RESET WITHSCENE: " << scene;
214 setSceneList(scene, pos);
216 /*char *tmp = decodedString(scene);
217 Mlt::Producer *producer = new Mlt::Producer(*m_mltProfile , "xml-string", tmp);
219 m_mltProducer = producer;
220 m_blackClip = new Mlt::Producer(*m_mltProfile , "colour", "black");
221 m_mltProducer->optimise();
222 m_mltProducer->set_speed(0);
225 //delete m_mltProfile;
226 // mlt_properties properties = MLT_CONSUMER_PROPERTIES(m_mltConsumer->get_consumer());
227 //mlt_profile prof = m_mltProfile->get_profile();
228 //mlt_properties_set_data(properties, "_profile", prof, 0, (mlt_destructor)mlt_profile_close, NULL);
229 //mlt_properties_set(properties, "profile", "hdv_1080_50i");
230 //m_mltConsumer->set("profile", (char *) profile.toUtf8().data());
231 //m_mltProfile = new Mlt::Profile((char*) profile.toUtf8().data());
233 //apply_profile_properties( m_mltProfile, m_mltConsumer->get_consumer(), properties );
238 /** Wraps the VEML command of the same name; Seeks the renderer clip to the given time. */
239 void Render::seek(GenTime time)
244 m_mltProducer->seek((int)(time.frames(m_fps)));
249 char *Render::decodedString(QString str)
251 /*QCString fn = QFile::encodeName(str);
252 char *t = new char[fn.length() + 1];
253 strcpy(t, (const char *)fn);*/
255 return (char *) qstrdup(str.toUtf8().data()); //toLatin1
259 /*QPixmap Render::frameThumbnail(Mlt::Frame *frame, int width, int height, bool border) {
260 QPixmap pix(width, height);
262 mlt_image_format format = mlt_image_rgb24a;
263 uint8_t *thumb = frame->get_image(format, width, height);
264 QImage image(thumb, width, height, QImage::Format_ARGB32);
266 if (!image.isNull()) {
267 pix = pix.fromImage(image);
269 QPainter painter(&pix);
270 painter.drawRect(0, 0, width - 1, height - 1);
272 } else pix.fill(Qt::black);
276 int Render::renderWidth() const
278 return (int)(m_mltProfile->height() * m_mltProfile->dar());
281 int Render::renderHeight() const
283 return m_mltProfile->height();
286 QPixmap Render::extractFrame(int frame_position, int width, int height)
289 width = renderWidth();
290 height = renderHeight();
291 } else if (width % 2 == 1) width++;
292 QPixmap pix(width, height);
293 if (!m_mltProducer) {
297 return KThumb::getFrame(m_mltProducer, frame_position, width, height);
300 QPixmap Render::getImageThumbnail(KUrl url, int /*width*/, int /*height*/)
304 if (url.fileName().startsWith(".all.")) { // check for slideshow
305 QString fileType = url.fileName().right(3);
307 QStringList::Iterator it;
309 QDir dir(url.directory());
311 filter << "*." + fileType;
312 filter << "*." + fileType.toUpper();
313 more = dir.entryList(filter, QDir::Files);
314 im.load(url.directory() + '/' + more.at(0));
315 } else im.load(url.path());
316 //pixmap = im.scaled(width, height);
321 QPixmap Render::getVideoThumbnail(char *profile, QString file, int frame_position, int width, int height) {
322 QPixmap pix(width, height);
323 char *tmp = decodedString(file);
324 Mlt::Profile *prof = new Mlt::Profile(profile);
325 Mlt::Producer m_producer(*prof, tmp);
327 if (m_producer.is_blank()) {
332 Mlt::Filter m_convert(*prof, "avcolour_space");
333 m_convert.set("forced", mlt_image_rgb24a);
334 m_producer.attach(m_convert);
335 m_producer.seek(frame_position);
336 Mlt::Frame * frame = m_producer.get_frame();
338 pix = frameThumbnail(frame, width, height, true);
341 if (prof) delete prof;
346 void Render::getImage(KUrl url, int frame_position, QPoint size)
348 char *tmp = decodedString(url.path());
349 Mlt::Producer m_producer(tmp);
351 if (m_producer.is_blank()) {
354 Mlt::Filter m_convert("avcolour_space");
355 m_convert.set("forced", mlt_image_rgb24a);
356 m_producer.attach(m_convert);
357 m_producer.seek(frame_position);
359 Mlt::Frame * frame = m_producer.get_frame();
362 QPixmap pix = frameThumbnail(frame, size.x(), size.y(), true);
364 emit replyGetImage(url, frame_position, pix, size.x(), size.y());
368 /* Create thumbnail for color */
369 /*void Render::getImage(int id, QString color, QPoint size)
371 QPixmap pixmap(size.x() - 2, size.y() - 2);
372 color = color.replace(0, 2, "#");
373 color = color.left(7);
374 pixmap.fill(QColor(color));
375 QPixmap result(size.x(), size.y());
376 result.fill(Qt::black);
377 //copyBlt(&result, 1, 1, &pixmap, 0, 0, size.x() - 2, size.y() - 2);
378 emit replyGetImage(id, result, size.x(), size.y());
382 /* Create thumbnail for image */
383 /*void Render::getImage(KUrl url, QPoint size)
387 if (url.fileName().startsWith(".all.")) { // check for slideshow
388 QString fileType = url.fileName().right(3);
390 QStringList::Iterator it;
392 QDir dir( url.directory() );
393 more = dir.entryList( QDir::Files );
394 for ( it = more.begin() ; it != more.end() ; ++it ) {
395 if ((*it).endsWith("."+fileType, Qt::CaseInsensitive)) {
396 if (!im.load(url.directory() + '/' + *it))
397 kDebug()<<"++ ERROR LOADIN IMAGE: "<<url.directory() + '/' + *it;
402 else im.load(url.path());
404 //pixmap = im.smoothScale(size.x() - 2, size.y() - 2);
405 QPixmap result(size.x(), size.y());
406 result.fill(Qt::black);
407 //copyBlt(&result, 1, 1, &pixmap, 0, 0, size.x() - 2, size.y() - 2);
408 emit replyGetImage(url, 1, result, size.x(), size.y());
412 double Render::consumerRatio() const
414 if (!m_mltConsumer) return 1.0;
415 return (m_mltConsumer->get_double("aspect_ratio_num") / m_mltConsumer->get_double("aspect_ratio_den"));
419 int Render::getLength()
423 // kDebug()<<"////// LENGTH: "<<mlt_producer_get_playtime(m_mltProducer->get_producer());
424 return mlt_producer_get_playtime(m_mltProducer->get_producer());
429 bool Render::isValid(KUrl url)
431 char *tmp = decodedString(url.path());
432 Mlt::Producer producer(*m_mltProfile, tmp);
434 if (producer.is_blank())
440 double Render::dar() const
442 return m_mltProfile->dar();
445 void Render::slotSplitView(bool doit)
447 m_isSplitView = doit;
448 Mlt::Service service(m_mltProducer->parent().get_service());
449 Mlt::Tractor tractor(service);
450 if (service.type() != tractor_type || tractor.count() < 2) return;
451 Mlt::Field *field = tractor.field();
454 for (int i = 1; i < tractor.count() && screen < 4; i++) {
455 Mlt::Producer trackProducer(tractor.track(i));
456 kDebug() << "// TRACK: " << i << ", HIDE: " << trackProducer.get("hide");
457 if (QString(trackProducer.get("hide")).toInt() != 1) {
458 kDebug() << "// ADIDNG TRACK: " << i;
459 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
460 transition->set("mlt_service", "composite");
461 transition->set("a_track", 0);
462 transition->set("b_track", i);
463 transition->set("distort", 1);
464 transition->set("internal_added", "200");
471 tmp = "50%,0:50%x50%";
474 tmp = "0,50%:50%x50%";
478 tmp = "50%,50%:50%x50%";
481 transition->set("geometry", tmp);
482 transition->set("always_active", "1");
483 field->plant_transition(*transition, 0, i);
488 m_mltConsumer->set("refresh", 1);
490 mlt_service serv = m_mltProducer->parent().get_service();
491 mlt_service nextservice = mlt_service_get_producer(serv);
492 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
493 QString mlt_type = mlt_properties_get(properties, "mlt_type");
494 QString resource = mlt_properties_get(properties, "mlt_service");
496 while (mlt_type == "transition") {
497 QString added = mlt_properties_get(MLT_SERVICE_PROPERTIES(nextservice), "internal_added");
498 if (added == "200") {
499 mlt_field_disconnect_service(field->get_field(), nextservice);
501 nextservice = mlt_service_producer(nextservice);
502 if (nextservice == NULL) break;
503 properties = MLT_SERVICE_PROPERTIES(nextservice);
504 mlt_type = mlt_properties_get(properties, "mlt_type");
505 resource = mlt_properties_get(properties, "mlt_service");
506 m_mltConsumer->set("refresh", 1);
511 void Render::getFileProperties(const QDomElement &xml, const QString &clipId, bool replaceProducer)
513 KUrl url = KUrl(xml.attribute("resource", QString()));
514 Mlt::Producer *producer = NULL;
515 if (xml.attribute("type").toInt() == TEXT && !QFile::exists(url.path())) {
516 emit replyGetFileProperties(clipId, producer, QMap < QString, QString >(), QMap < QString, QString >(), replaceProducer);
519 if (xml.attribute("type").toInt() == COLOR) {
520 char *tmp = decodedString("colour:" + xml.attribute("colour"));
521 producer = new Mlt::Producer(*m_mltProfile, 0, tmp);
523 } else if (url.isEmpty()) {
525 QDomElement mlt = doc.createElement("mlt");
526 QDomElement play = doc.createElement("playlist");
527 doc.appendChild(mlt);
528 mlt.appendChild(play);
529 play.appendChild(doc.importNode(xml, true));
530 char *tmp = decodedString(doc.toString());
531 producer = new Mlt::Producer(*m_mltProfile, "xml-string", tmp);
534 char *tmp = decodedString(url.path());
535 producer = new Mlt::Producer(*m_mltProfile, tmp);
539 if (producer == NULL || producer->is_blank() || !producer->is_valid()) {
540 kDebug() << " / / / / / / / / ERROR / / / / // CANNOT LOAD PRODUCER: ";
541 emit removeInvalidClip(clipId, replaceProducer);
546 if (xml.hasAttribute("force_aspect_ratio")) {
547 double aspect = xml.attribute("force_aspect_ratio").toDouble();
548 if (aspect > 0) producer->set("force_aspect_ratio", aspect);
550 if (xml.hasAttribute("threads")) {
551 int threads = xml.attribute("threads").toInt();
552 if (threads != 1) producer->set("threads", threads);
554 if (xml.hasAttribute("video_index")) {
555 int vindex = xml.attribute("video_index").toInt();
556 if (vindex != 0) producer->set("video_index", vindex);
558 if (xml.hasAttribute("audio_index")) {
559 int aindex = xml.attribute("audio_index").toInt();
560 if (aindex != 0) producer->set("audio_index", aindex);
563 if (xml.hasAttribute("out")) producer->set_in_and_out(xml.attribute("in").toInt(), xml.attribute("out").toInt());
565 char *tmp = decodedString(clipId);
566 producer->set("id", tmp);
569 if (!replaceProducer && xml.hasAttribute("file_hash")) {
570 // Clip already has all properties
571 emit replyGetFileProperties(clipId, producer, QMap < QString, QString >(), QMap < QString, QString >(), replaceProducer);
576 int width = (int)(height * m_mltProfile->dar());
577 QMap < QString, QString > filePropertyMap;
578 QMap < QString, QString > metadataPropertyMap;
580 int frameNumber = xml.attribute("thumbnail", "0").toInt();
581 if (frameNumber != 0) producer->seek(frameNumber);
583 filePropertyMap["duration"] = QString::number(producer->get_playtime());
584 //kDebug() << "/////// PRODUCER: " << url.path() << " IS: " << producer.get_playtime();
586 Mlt::Frame *frame = producer->get_frame();
588 if (xml.attribute("type").toInt() == SLIDESHOW) {
589 if (xml.hasAttribute("ttl")) producer->set("ttl", xml.attribute("ttl").toInt());
590 if (xml.attribute("fade") == "1") {
591 // user wants a fade effect to slideshow
592 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "luma");
593 if (filter && filter->is_valid()) {
594 if (xml.hasAttribute("ttl")) filter->set("period", xml.attribute("ttl").toInt() - 1);
595 if (xml.hasAttribute("luma_duration") && !xml.attribute("luma_duration").isEmpty()) filter->set("luma.out", xml.attribute("luma_duration").toInt());
596 if (xml.hasAttribute("luma_file") && !xml.attribute("luma_file").isEmpty()) {
597 char *tmp = decodedString(xml.attribute("luma_file"));
598 filter->set("luma.resource", tmp);
600 if (xml.hasAttribute("softness")) {
601 int soft = xml.attribute("softness").toInt();
602 filter->set("luma.softness", (double) soft / 100.0);
605 Mlt::Service clipService(producer->get_service());
606 clipService.attach(*filter);
612 filePropertyMap["fps"] = producer->get("source_fps");
614 if (frame && frame->is_valid()) {
615 filePropertyMap["frame_size"] = QString::number(frame->get_int("width")) + 'x' + QString::number(frame->get_int("height"));
616 filePropertyMap["frequency"] = QString::number(frame->get_int("frequency"));
617 filePropertyMap["channels"] = QString::number(frame->get_int("channels"));
618 filePropertyMap["aspect_ratio"] = frame->get("aspect_ratio");
620 if (frame->get_int("test_image") == 0) {
621 if (url.path().endsWith(".mlt") || url.path().endsWith(".westley") || url.path().endsWith(".kdenlive")) {
622 filePropertyMap["type"] = "playlist";
623 metadataPropertyMap["comment"] = QString::fromUtf8(producer->get("title"));
624 } else if (frame->get_int("test_audio") == 0)
625 filePropertyMap["type"] = "av";
627 filePropertyMap["type"] = "video";
629 mlt_image_format format = mlt_image_yuv422;
631 int frame_height = 0;
632 //frame->set("rescale.interp", "hyper");
633 frame->set("normalised_height", height);
634 frame->set("normalised_width", width);
635 QPixmap pix(width, height);
637 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
638 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
639 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
640 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
642 if (!image.isNull()) {
643 pix = QPixmap::fromImage(image.rgbSwapped());
647 mlt_pool_release(new_image);
648 emit replyGetImage(clipId, pix);
650 } else if (frame->get_int("test_audio") == 0) {
651 QPixmap pixmap = KIcon("audio-x-generic").pixmap(QSize(width, height));
652 emit replyGetImage(clipId, pixmap);
653 filePropertyMap["type"] = "audio";
657 // Retrieve audio / video codec name
661 if (producer->get_int("video_index") > -1) {
662 /*if (context->duration == AV_NOPTS_VALUE) {
663 kDebug() << " / / / / / / / /ERROR / / / CLIP HAS UNKNOWN DURATION";
664 emit removeInvalidClip(clipId);
668 // Get the video_index
669 int default_video = producer->get_int("video_index");
671 int default_audio = producer->get_int("audio_index");
674 // Find maximum stream index values
675 for (int ix = 0; ix < producer->get_int("meta.media.nb_streams"); ix++) {
676 snprintf(property, sizeof(property), "meta.media.%d.stream.type", ix);
677 QString type = producer->get(property);
680 else if (type == "audio")
683 filePropertyMap["default_video"] = QString::number(default_video);
684 filePropertyMap["video_max"] = QString::number(video_max);
685 filePropertyMap["default_audio"] = QString::number(default_audio);
686 filePropertyMap["audio_max"] = QString::number(audio_max);
688 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", default_video);
689 if (producer->get(property)) {
690 filePropertyMap["videocodec"] = producer->get(property);
692 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
693 if (producer->get(property))
694 filePropertyMap["videocodec"] = producer->get(property);
697 if (KdenliveSettings::dropbframes()) {
698 kDebug() << "// LOOKING FOR H264 on: " << default_video;
699 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
700 kDebug() << "PROP: " << property << " = " << producer->get(property);
701 if (producer->get(property) && strcmp(producer->get(property), "h264") == 0) {
702 kDebug() << "// GOT H264 CLIP, SETTING FAST PROPS";
703 producer->set("skip_loop_filter", "all");
704 producer->set("skip_frame", "bidir");
708 } else kDebug() << " / / / / /WARNING, VIDEO CONTEXT IS NULL!!!!!!!!!!!!!!";
709 if (producer->get_int("audio_index") > -1) {
710 // Get the audio_index
711 int index = producer->get_int("audio_index");
713 snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", index);
714 if (producer->get(property)) {
715 filePropertyMap["audiocodec"] = producer->get(property);
717 snprintf(property, sizeof(property), "meta.media.%d.codec.name", index);
718 if (producer->get(property))
719 filePropertyMap["audiocodec"] = producer->get(property);
724 Mlt::Properties metadata;
725 metadata.pass_values(*producer, "meta.attr.");
726 int count = metadata.count();
727 for (int i = 0; i < count; i ++) {
728 QString name = metadata.get_name(i);
729 QString value = QString::fromUtf8(metadata.get(i));
730 if (name.endsWith("markup") && !value.isEmpty())
731 metadataPropertyMap[ name.section('.', 0, -2)] = value;
734 emit replyGetFileProperties(clipId, producer, filePropertyMap, metadataPropertyMap, replaceProducer);
735 kDebug() << "REquested fuile info for: " << url.path();
737 // FIXME: should delete this to avoid a leak...
742 /** Create the producer from the MLT XML QDomDocument */
744 void Render::initSceneList()
746 kDebug() << "-------- INIT SCENE LIST ------_";
748 QDomElement mlt = doc.createElement("mlt");
749 doc.appendChild(mlt);
750 QDomElement prod = doc.createElement("producer");
751 prod.setAttribute("resource", "colour");
752 prod.setAttribute("colour", "red");
753 prod.setAttribute("id", "black");
754 prod.setAttribute("in", "0");
755 prod.setAttribute("out", "0");
757 QDomElement tractor = doc.createElement("tractor");
758 QDomElement multitrack = doc.createElement("multitrack");
760 QDomElement playlist1 = doc.createElement("playlist");
761 playlist1.appendChild(prod);
762 multitrack.appendChild(playlist1);
763 QDomElement playlist2 = doc.createElement("playlist");
764 multitrack.appendChild(playlist2);
765 QDomElement playlist3 = doc.createElement("playlist");
766 multitrack.appendChild(playlist3);
767 QDomElement playlist4 = doc.createElement("playlist");
768 multitrack.appendChild(playlist4);
769 QDomElement playlist5 = doc.createElement("playlist");
770 multitrack.appendChild(playlist5);
771 tractor.appendChild(multitrack);
772 mlt.appendChild(tractor);
773 // kDebug()<<doc.toString();
775 QString tmp = QString("<mlt><producer resource=\"colour\" colour=\"red\" id=\"red\" /><tractor><multitrack><playlist></playlist><playlist></playlist><playlist /><playlist /><playlist></playlist></multitrack></tractor></mlt>");*/
776 setSceneList(doc, 0);
782 /** Create the producer from the MLT XML QDomDocument */
783 int Render::setProducer(Mlt::Producer *producer, int position)
785 if (m_winid == -1) return -1;
788 m_mltConsumer->stop();
791 m_mltConsumer->purge();
795 m_mltProducer->set_speed(0);
796 delete m_mltProducer;
797 m_mltProducer = NULL;
801 m_mltProducer = new Mlt::Producer(producer->get_producer());
802 } else m_mltProducer = m_blackClip->cut(0, 50);
803 /*if (KdenliveSettings::dropbframes()) {
804 m_mltProducer->set("skip_loop_filter", "all");
805 m_mltProducer->set("skip_frame", "bidir");
807 if (!m_mltProducer || !m_mltProducer->is_valid()) kDebug() << " WARNING - - - - -INVALID PLAYLIST: ";
809 m_fps = m_mltProducer->get_fps();
810 int error = connectPlaylist();
811 if (position != -1) {
812 m_mltProducer->seek(position);
813 emit rendererPosition(position);
821 /** Create the producer from the MLT XML QDomDocument */
822 int Render::setSceneList(QDomDocument list, int position)
824 return setSceneList(list.toString(), position);
827 /** Create the producer from the MLT XML QDomDocument */
828 int Render::setSceneList(QString playlist, int position)
830 if (m_winid == -1) return -1;
833 qDeleteAll(m_slowmotionProducers.values());
834 m_slowmotionProducers.clear();
836 //kWarning() << "////// RENDER, SET SCENE LIST: " << playlist;
838 if (m_mltConsumer == NULL) {
839 kWarning() << "/////// ERROR, TRYING TO USE NULL MLT CONSUMER";
844 if (!m_mltConsumer->is_stopped()) {
845 m_mltConsumer->stop();
846 //m_mltConsumer->set("refresh", 0);
850 m_mltProducer->set_speed(0);
851 //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
853 delete m_mltProducer;
854 m_mltProducer = NULL;
859 char *tmp = decodedString(playlist);
860 m_mltProducer = new Mlt::Producer(*m_mltProfile, "xml-string", tmp);
862 if (!m_mltProducer || !m_mltProducer->is_valid()) {
863 kDebug() << " WARNING - - - - -INVALID PLAYLIST: " << tmp;
864 m_mltProducer = m_blackClip->cut(0, 50);
868 m_mltProducer->optimise();
870 /*if (KdenliveSettings::osdtimecode()) {
871 // Attach filter for on screen display of timecode
873 QString attr = "attr_check";
874 mlt_filter filter = mlt_factory_filter( "data_feed", (char*) attr.ascii() );
875 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
876 mlt_producer_attach( m_mltProducer->get_producer(), filter );
877 mlt_filter_close( filter );
879 m_osdInfo = new Mlt::Filter("data_show");
880 tmp = decodedString(m_osdProfile);
881 m_osdInfo->set("resource", tmp);
883 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
884 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
885 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
886 m_osdInfo->set("dynamic", "1");
888 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
890 m_osdInfo->set("dynamic", "0");
893 m_fps = m_mltProducer->get_fps();
895 // Seek to correct place after opening project.
896 m_mltProducer->seek(position);
899 kDebug() << "// NEW SCENE LIST DURATION SET TO: " << m_mltProducer->get_playtime();
900 error = connectPlaylist();
901 fillSlowMotionProducers();
905 emit refreshDocumentProducers();
907 //kDebug()<<"// SETSCN LST, POS: "<<position;
908 //if (position != 0) emit rendererPosition(position);
911 /** Create the producer from the MLT XML QDomDocument */
912 const QString Render::sceneList()
915 Mlt::Consumer xmlConsumer(*m_mltProfile , "xml:kdenlive_playlist");
916 m_mltProducer->optimise();
917 xmlConsumer.set("terminate_on_pause", 1);
918 Mlt::Producer prod(m_mltProducer->get_producer());
919 bool split = m_isSplitView;
920 if (split) slotSplitView(false);
921 xmlConsumer.connect(prod);
923 while (!xmlConsumer.is_stopped()) {}
924 playlist = QString::fromUtf8(xmlConsumer.get("kdenlive_playlist"));
925 if (split) slotSplitView(true);
929 bool Render::saveSceneList(QString path, QDomElement kdenliveData)
933 doc.setContent(sceneList(), false);
934 if (!kdenliveData.isNull()) {
935 // add Kdenlive specific tags
936 QDomNode mlt = doc.elementsByTagName("mlt").at(0);
937 mlt.appendChild(doc.importNode(kdenliveData, true));
939 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
940 kWarning() << "////// ERROR writing to file: " << path;
943 file.write(doc.toString().toUtf8());
944 if (file.error() != QFile::NoError) {
953 void Render::saveZone(KUrl url, QString desc, QPoint zone)
955 kDebug() << "// SAVING CLIP ZONE, RENDER: " << m_name;
956 char *tmppath = decodedString("xml:" + url.path());
957 Mlt::Consumer xmlConsumer(*m_mltProfile , tmppath);
958 m_mltProducer->optimise();
960 xmlConsumer.set("terminate_on_pause", 1);
961 if (m_name == "clip") {
962 Mlt::Producer *prod = m_mltProducer->cut(zone.x(), zone.y());
963 tmppath = decodedString(desc);
965 list.insert_at(0, prod, 0);
966 list.set("title", tmppath);
968 xmlConsumer.connect(list);
971 //TODO: not working yet, save zone from timeline
972 Mlt::Producer *p1 = new Mlt::Producer(m_mltProducer->get_producer());
973 /* Mlt::Service service(p1->parent().get_service());
974 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";*/
976 //Mlt::Producer *prod = p1->cut(zone.x(), zone.y());
977 tmppath = decodedString(desc);
978 //prod->set("title", tmppath);
980 xmlConsumer.connect(*p1); //list);
986 double Render::fps() const
991 int Render::connectPlaylist()
993 if (!m_mltConsumer) return -1;
994 //m_mltConsumer->set("refresh", "0");
995 m_mltConsumer->connect(*m_mltProducer);
996 m_mltProducer->set_speed(0);
997 if (m_mltConsumer->start() == -1) {
998 // ARGH CONSUMER BROKEN!!!!
999 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."));
1000 emit blockMonitors();
1001 delete m_mltProducer;
1002 m_mltProducer = NULL;
1005 emit durationChanged(m_mltProducer->get_playtime());
1010 void Render::refreshDisplay()
1013 if (!m_mltProducer) return;
1014 //m_mltConsumer->set("refresh", 0);
1016 //mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1017 /*if (KdenliveSettings::osdtimecode()) {
1018 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
1019 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
1020 m_osdInfo->set("dynamic", "1");
1021 m_mltProducer->attach(*m_osdInfo);
1024 m_mltProducer->detach(*m_osdInfo);
1025 m_osdInfo->set("dynamic", "0");
1030 void Render::setVolume(double /*volume*/)
1032 if (!m_mltConsumer || !m_mltProducer) return;
1034 m_mltConsumer->set("refresh", 0);
1035 // Attach filter for on screen display of timecode
1036 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1037 mlt_properties_set_double( properties, "meta.volume", volume );
1038 mlt_properties_set_int( properties, "meta.attr.osdvolume", 1);
1039 mlt_properties_set( properties, "meta.attr.osdvolume.markup", i18n("Volume: ") + QString::number(volume * 100));
1041 if (!KdenliveSettings::osdtimecode()) {
1042 m_mltProducer->detach(*m_osdInfo);
1043 mlt_properties_set_int( properties, "meta.attr.timecode", 0);
1044 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
1047 m_osdTimer->setSingleShot(2500);
1050 void Render::slotOsdTimeout()
1052 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
1053 mlt_properties_set_int(properties, "meta.attr.osdvolume", 0);
1054 mlt_properties_set(properties, "meta.attr.osdvolume.markup", NULL);
1055 //if (!KdenliveSettings::osdtimecode()) m_mltProducer->detach(*m_osdInfo);
1059 void Render::start()
1061 kDebug() << "----- STARTING MONITOR: " << m_name;
1062 if (m_winid == -1) {
1063 kDebug() << "----- BROKEN MONITOR: " << m_name << ", RESTART";
1066 if (m_mltConsumer && m_mltConsumer->is_stopped()) {
1067 kDebug() << "----- MONITOR: " << m_name << " WAS STOPPED";
1068 if (m_mltConsumer->start() == -1) {
1069 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."));
1070 emit blockMonitors();
1071 delete m_mltProducer;
1072 m_mltProducer = NULL;
1074 kDebug() << "----- MONITOR: " << m_name << " REFRESH";
1075 m_isBlocked = false;
1079 m_isBlocked = false;
1082 void Render::clear()
1084 kDebug() << " ********* RENDER CLEAR";
1085 if (m_mltConsumer) {
1086 //m_mltConsumer->set("refresh", 0);
1087 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
1090 if (m_mltProducer) {
1091 //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
1092 m_mltProducer->set_speed(0.0);
1093 delete m_mltProducer;
1094 m_mltProducer = NULL;
1101 if (m_mltProducer == NULL) return;
1102 if (m_mltConsumer && !m_mltConsumer->is_stopped()) {
1103 kDebug() << "///////////// RENDER STOPPED: " << m_name;
1105 //m_mltConsumer->set("refresh", 0);
1106 m_mltConsumer->stop();
1107 // delete m_mltConsumer;
1108 // m_mltConsumer = NULL;
1110 kDebug() << "///////////// RENDER STOP2-------";
1113 if (m_mltProducer) {
1114 if (m_isZoneMode) resetZoneMode();
1115 m_mltProducer->set_speed(0.0);
1116 //m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1117 //kDebug() << m_mltProducer->get_length();
1119 kDebug() << "///////////// RENDER STOP3-------";
1122 void Render::stop(const GenTime & startTime)
1125 kDebug() << "///////////// RENDER STOP-------2";
1126 if (m_mltProducer) {
1127 if (m_isZoneMode) resetZoneMode();
1128 m_mltProducer->set_speed(0.0);
1129 m_mltProducer->seek((int) startTime.frames(m_fps));
1131 m_mltConsumer->purge();
1134 void Render::pause()
1136 if (!m_mltProducer || !m_mltConsumer)
1138 if (m_mltProducer->get_speed() == 0.0) return;
1139 if (m_isZoneMode) resetZoneMode();
1141 m_mltConsumer->set("refresh", 0);
1142 m_mltProducer->set_speed(0.0);
1143 emit rendererPosition(m_framePosition);
1144 m_mltProducer->seek(m_framePosition);
1145 m_mltConsumer->purge();
1148 void Render::switchPlay()
1150 if (!m_mltProducer || !m_mltConsumer)
1152 if (m_isZoneMode) resetZoneMode();
1153 if (m_mltProducer->get_speed() == 0.0) {
1154 m_isBlocked = false;
1155 m_mltProducer->set_speed(1.0);
1156 m_mltConsumer->set("refresh", 1);
1159 m_mltConsumer->set("refresh", 0);
1160 m_mltProducer->set_speed(0.0);
1161 emit rendererPosition(m_framePosition);
1162 m_mltProducer->seek(m_framePosition);
1163 m_mltConsumer->purge();
1164 //kDebug()<<" ********* RENDER PAUSE: "<<m_mltProducer->get_speed();
1165 //m_mltConsumer->set("refresh", 0);
1166 /*mlt_position position = mlt_producer_position( m_mltProducer->get_producer() );
1167 m_mltProducer->set_speed(0);
1168 m_mltProducer->seek( position );
1169 //m_mltProducer->seek((int) m_framePosition);
1170 m_isBlocked = false;*/
1172 /*if (speed == 0.0) {
1173 m_mltProducer->seek((int) m_framePosition + 1);
1174 m_mltConsumer->purge();
1179 void Render::play(double speed)
1183 // if (speed == 0.0) m_mltProducer->set("out", m_mltProducer->get_length() - 1);
1184 m_isBlocked = false;
1185 m_mltProducer->set_speed(speed);
1186 /*if (speed == 0.0) {
1187 m_mltProducer->seek((int) m_framePosition + 1);
1188 m_mltConsumer->purge();
1193 void Render::play(const GenTime & startTime)
1195 if (!m_mltProducer || !m_mltConsumer)
1197 m_isBlocked = false;
1198 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1199 m_mltProducer->set_speed(1.0);
1200 m_mltConsumer->set("refresh", 1);
1203 void Render::loopZone(const GenTime & startTime, const GenTime & stopTime)
1205 if (!m_mltProducer || !m_mltConsumer)
1207 //m_mltProducer->set("eof", "loop");
1208 m_isLoopMode = true;
1209 m_loopStart = startTime;
1210 playZone(startTime, stopTime);
1213 void Render::playZone(const GenTime & startTime, const GenTime & stopTime)
1215 if (!m_mltProducer || !m_mltConsumer)
1217 m_isBlocked = false;
1218 if (!m_isZoneMode) m_originalOut = m_mltProducer->get_playtime() - 1;
1219 m_mltProducer->set("out", stopTime.frames(m_fps));
1220 m_mltProducer->seek((int)(startTime.frames(m_fps)));
1221 m_mltProducer->set_speed(1.0);
1222 m_mltConsumer->set("refresh", 1);
1223 m_isZoneMode = true;
1226 void Render::resetZoneMode()
1228 if (!m_isZoneMode && !m_isLoopMode) return;
1229 m_mltProducer->set("out", m_originalOut);
1230 //m_mltProducer->set("eof", "pause");
1231 m_isZoneMode = false;
1232 m_isLoopMode = false;
1235 void Render::seekToFrame(int pos)
1237 //kDebug()<<" ********* RENDER SEEK TO POS";
1240 m_isBlocked = false;
1242 m_mltProducer->seek(pos);
1246 void Render::askForRefresh()
1248 // Use a Timer so that we don't refresh too much
1249 m_refreshTimer->start(300);
1252 void Render::doRefresh()
1254 // Use a Timer so that we don't refresh too much
1255 if (!m_isBlocked && m_mltConsumer) m_mltConsumer->set("refresh", 1);
1258 void Render::refresh()
1260 if (!m_mltProducer || m_isBlocked)
1262 m_refreshTimer->stop();
1263 if (m_mltConsumer) {
1264 m_mltConsumer->set("refresh", 1);
1268 void Render::setDropFrames(bool show)
1270 if (m_mltConsumer) {
1272 if (show == false) dropFrames = 0;
1273 m_mltConsumer->stop();
1274 m_mltConsumer->set("play.real_time", dropFrames);
1275 if (m_mltConsumer->start() == -1) {
1276 emit blockMonitors();
1277 delete m_mltProducer;
1278 m_mltProducer = NULL;
1284 double Render::playSpeed()
1286 if (m_mltProducer) return m_mltProducer->get_speed();
1290 GenTime Render::seekPosition() const
1292 if (m_mltProducer) return GenTime((int) m_mltProducer->position(), m_fps);
1293 else return GenTime();
1297 const QString & Render::rendererName() const
1303 void Render::emitFrameNumber(double position)
1305 m_framePosition = position;
1306 emit rendererPosition((int) position);
1307 //if (qApp->activeWindow()) QApplication::postEvent(qApp->activeWindow(), new PositionChangeEvent( GenTime((int) position, m_fps), m_monitorId));
1310 void Render::emitConsumerStopped()
1312 // This is used to know when the playing stopped
1313 if (m_mltProducer) {
1314 double pos = m_mltProducer->position();
1315 if (m_isLoopMode) play(m_loopStart);
1316 else if (m_isZoneMode) resetZoneMode();
1317 emit rendererStopped((int) pos);
1318 //if (qApp->activeWindow()) QApplication::postEvent(qApp->activeWindow(), new PositionChangeEvent(GenTime((int) pos, m_fps), m_monitorId + 100));
1319 //new QCustomEvent(10002));
1325 void Render::exportFileToFirewire(QString /*srcFileName*/, int /*port*/, GenTime /*startTime*/, GenTime /*endTime*/)
1327 KMessageBox::sorry(0, i18n("Firewire is not enabled on your system.\n Please install Libiec61883 and recompile Kdenlive"));
1331 void Render::exportCurrentFrame(KUrl url, bool /*notify*/)
1333 if (!m_mltProducer) {
1334 KMessageBox::sorry(qApp->activeWindow(), i18n("There is no clip, cannot extract frame."));
1338 //int height = 1080;//KdenliveSettings::defaultheight();
1339 //int width = 1940; //KdenliveSettings::displaywidth();
1341 QPixmap pix; // = KThumb::getFrame(m_mltProducer, -1, width, height);
1343 QPixmap pix(width, height);
1344 Mlt::Filter m_convert(*m_mltProfile, "avcolour_space");
1345 m_convert.set("forced", mlt_image_rgb24a);
1346 m_mltProducer->attach(m_convert);
1347 Mlt::Frame * frame = m_mltProducer->get_frame();
1348 m_mltProducer->detach(m_convert);
1350 pix = frameThumbnail(frame, width, height);
1353 pix.save(url.path(), "PNG");
1354 //if (notify) QApplication::postEvent(qApp->activeWindow(), new UrlEvent(url, 10003));
1357 /** MLT PLAYLIST DIRECT MANIPULATON **/
1360 void Render::mltCheckLength()
1362 //kDebug()<<"checking track length: "<<track<<"..........";
1364 Mlt::Service service(m_mltProducer->get_service());
1365 Mlt::Tractor tractor(service);
1367 int trackNb = tractor.count();
1371 Mlt::Producer trackProducer(tractor.track(0));
1372 duration = trackProducer.get_playtime() - 1;
1373 m_mltProducer->set("out", duration);
1374 emit durationChanged(duration);
1377 while (trackNb > 1) {
1378 Mlt::Producer trackProducer(tractor.track(trackNb - 1));
1379 trackDuration = trackProducer.get_playtime() - 1;
1381 //kDebug() << " / / /DURATON FOR TRACK " << trackNb - 1 << " = " << trackDuration;
1382 if (trackDuration > duration) duration = trackDuration;
1386 Mlt::Producer blackTrackProducer(tractor.track(0));
1388 if (blackTrackProducer.get_playtime() - 1 != duration) {
1389 Mlt::Playlist blackTrackPlaylist((mlt_playlist) blackTrackProducer.get_service());
1390 Mlt::Producer *blackclip = blackTrackPlaylist.get_clip(0);
1391 if (duration > m_blackClip->get_length()) {
1392 m_blackClip->set("length", duration);
1393 if (blackclip) blackclip->set("length", duration);
1395 if (blackclip == NULL || blackclip->is_blank() || blackTrackPlaylist.count() != 1) {
1396 blackTrackPlaylist.clear();
1397 blackTrackPlaylist.append(*m_blackClip, 0, duration - 1);
1398 } else blackTrackPlaylist.resize_clip(0, 0, duration - 1);
1400 m_mltProducer->set("out", duration);
1401 emit durationChanged(duration);
1405 void Render::mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod)
1407 if (m_mltProducer == NULL) {
1408 kDebug() << "PLAYLIST NOT INITIALISED //////";
1411 Mlt::Producer parentProd(m_mltProducer->parent());
1412 if (parentProd.get_producer() == NULL) {
1413 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1417 Mlt::Service service(parentProd.get_service());
1418 Mlt::Tractor tractor(service);
1419 if (info.track > tractor.count() - 1) {
1420 kDebug() << "ERROR TRYING TO INSERT CLIP ON TRACK " << info.track << ", at POS: " << info.startPos.frames(25);
1423 mlt_service_lock(service.get_service());
1424 Mlt::Producer trackProducer(tractor.track(info.track));
1425 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1426 //kDebug()<<"/// INSERT cLIP: "<<info.cropStart.frames(m_fps)<<", "<<info.startPos.frames(m_fps)<<"-"<<info.endPos.frames(m_fps);
1428 if (element.attribute("speed", "1.0").toDouble() != 1.0) {
1429 // We want a slowmotion producer
1430 double speed = element.attribute("speed", "1.0").toDouble();
1431 QString url = QString::fromUtf8(prod->get("resource"));
1432 url.append('?' + QString::number(speed));
1433 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1434 if (!slowprod || slowprod->get_producer() == NULL) {
1435 char *tmp = decodedString(url);
1436 slowprod = new Mlt::Producer(*m_mltProfile, "framebuffer", tmp);
1438 QString id = prod->get("id");
1439 if (id.contains('_')) id = id.section('_', 0, 0);
1440 QString producerid = "slowmotion:" + id + ':' + QString::number(speed);
1441 tmp = decodedString(producerid);
1442 slowprod->set("id", tmp);
1444 m_slowmotionProducers.insert(url, slowprod);
1449 Mlt::Producer *clip = prod->cut((int) info.cropStart.frames(m_fps), (int)(info.endPos - info.startPos + info.cropStart).frames(m_fps) - 1);
1450 int newIndex = trackPlaylist.insert_at((int) info.startPos.frames(m_fps), *clip, 1);
1452 /*if (QString(prod->get("transparency")).toInt() == 1)
1453 mltAddClipTransparency(info, info.track - 1, QString(prod->get("id")).toInt());*/
1455 mlt_service_unlock(service.get_service());
1457 if (info.track != 0 && (newIndex + 1 == trackPlaylist.count())) mltCheckLength();
1458 //tractor.multitrack()->refresh();
1459 //tractor.refresh();
1463 void Render::mltCutClip(int track, GenTime position)
1468 Mlt::Service service(m_mltProducer->parent().get_service());
1469 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
1471 Mlt::Tractor tractor(service);
1472 Mlt::Producer trackProducer(tractor.track(track));
1473 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1476 /* // Display playlist info
1477 kDebug()<<"//////////// BEFORE";
1478 for (int i = 0; i < trackPlaylist.count(); i++) {
1479 int blankStart = trackPlaylist.clip_start(i);
1480 int blankDuration = trackPlaylist.clip_length(i) - 1;
1482 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1483 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1486 int cutPos = (int) position.frames(m_fps);
1488 int clipIndex = trackPlaylist.get_clip_index_at(cutPos);
1489 if (trackPlaylist.is_blank(clipIndex)) {
1490 kDebug() << "// WARNING, TRYING TO CUT A BLANK";
1491 m_isBlocked = false;
1494 mlt_service_lock(service.get_service());
1495 int clipStart = trackPlaylist.clip_start(clipIndex);
1496 trackPlaylist.split(clipIndex, cutPos - clipStart - 1);
1497 mlt_service_unlock(service.get_service());
1499 // duplicate effects
1500 Mlt::Producer *original = trackPlaylist.get_clip_at(clipStart);
1501 Mlt::Producer *clip = trackPlaylist.get_clip_at(cutPos);
1503 if (original == NULL || clip == NULL) {
1504 kDebug() << "// ERROR GRABBING CLIP AFTER SPLIT";
1506 Mlt::Service clipService(original->get_service());
1507 Mlt::Service dupService(clip->get_service());
1509 Mlt::Filter *filter = clipService.filter(ct);
1511 // Only duplicate Kdenlive filters, and skip the fade in effects
1512 if (filter->is_valid() && strcmp(filter->get("kdenlive_id"), "") && strcmp(filter->get("kdenlive_id"), "fadein") && strcmp(filter->get("kdenlive_id"), "fade_from_black")) {
1513 // looks like there is no easy way to duplicate a filter,
1514 // so we will create a new one and duplicate its properties
1515 Mlt::Filter *dup = new Mlt::Filter(*m_mltProfile, filter->get("mlt_service"));
1516 if (dup && dup->is_valid()) {
1517 Mlt::Properties entries(filter->get_properties());
1518 for (int i = 0; i < entries.count(); i++) {
1519 dup->set(entries.get_name(i), entries.get(i));
1521 dupService.attach(*dup);
1525 filter = clipService.filter(ct);
1528 /* // Display playlist info
1529 kDebug()<<"//////////// AFTER";
1530 for (int i = 0; i < trackPlaylist.count(); i++) {
1531 int blankStart = trackPlaylist.clip_start(i);
1532 int blankDuration = trackPlaylist.clip_length(i) - 1;
1534 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1535 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1538 m_isBlocked = false;
1541 void Render::mltUpdateClip(ItemInfo info, QDomElement element, Mlt::Producer *prod)
1544 mltRemoveClip(info.track, info.startPos);
1545 mltInsertClip(info, element, prod);
1549 bool Render::mltRemoveClip(int track, GenTime position)
1551 Mlt::Service service(m_mltProducer->parent().get_service());
1552 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
1554 Mlt::Tractor tractor(service);
1555 mlt_service_lock(service.get_service());
1556 Mlt::Producer trackProducer(tractor.track(track));
1557 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1558 int clipIndex = trackPlaylist.get_clip_index_at((int) position.frames(m_fps));
1560 // Display playlist info
1561 //kDebug() << "//// BEFORE -( " << position.frames(m_fps) << " )-------------------------------";
1562 /*for (int i = 0; i < trackPlaylist.count(); i++) {
1563 int blankStart = trackPlaylist.clip_start(i);
1564 int blankDuration = trackPlaylist.clip_length(i) - 1;
1566 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1567 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1569 if (trackPlaylist.is_blank(clipIndex)) {
1570 kDebug() << "// WARNING, TRYING TO REMOVE A BLANK: " << position.frames(25);
1571 mlt_service_unlock(service.get_service());
1574 //kDebug()<<"//// Deleting at: "<< (int) position.frames(m_fps) <<" --------------------------------------";
1576 trackPlaylist.replace_with_blank(clipIndex);
1577 trackPlaylist.consolidate_blanks(0);
1578 /*if (QString(clip.parent().get("transparency")).toInt() == 1)
1579 mltDeleteTransparency((int) position.frames(m_fps), track, QString(clip.parent().get("id")).toInt());*/
1581 /* // Display playlist info
1582 kDebug()<<"//// AFTER";
1583 for (int i = 0; i < trackPlaylist.count(); i++) {
1584 int blankStart = trackPlaylist.clip_start(i);
1585 int blankDuration = trackPlaylist.clip_length(i) - 1;
1587 if (trackPlaylist.is_blank(i)) blk = "(blank)";
1588 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
1590 mlt_service_unlock(service.get_service());
1591 if (track != 0 && trackPlaylist.count() <= clipIndex) mltCheckLength();
1592 m_isBlocked = false;
1596 int Render::mltGetSpaceLength(const GenTime pos, int track, bool fromBlankStart)
1598 if (!m_mltProducer) {
1599 kDebug() << "PLAYLIST NOT INITIALISED //////";
1602 Mlt::Producer parentProd(m_mltProducer->parent());
1603 if (parentProd.get_producer() == NULL) {
1604 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1608 Mlt::Service service(parentProd.get_service());
1609 Mlt::Tractor tractor(service);
1610 int insertPos = pos.frames(m_fps);
1612 Mlt::Producer trackProducer(tractor.track(track));
1613 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1614 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1615 if (!trackPlaylist.is_blank(clipIndex)) return -1;
1616 if (fromBlankStart) return trackPlaylist.clip_length(clipIndex);
1617 return trackPlaylist.clip_length(clipIndex) + trackPlaylist.clip_start(clipIndex) - insertPos;
1620 int Render::mltTrackDuration(int track)
1622 if (!m_mltProducer) {
1623 kDebug() << "PLAYLIST NOT INITIALISED //////";
1626 Mlt::Producer parentProd(m_mltProducer->parent());
1627 if (parentProd.get_producer() == NULL) {
1628 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1632 Mlt::Service service(parentProd.get_service());
1633 Mlt::Tractor tractor(service);
1635 Mlt::Producer trackProducer(tractor.track(track));
1636 return trackProducer.get_playtime() - 1;
1639 void Render::mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime duration, const GenTime timeOffset)
1641 if (!m_mltProducer) {
1642 kDebug() << "PLAYLIST NOT INITIALISED //////";
1645 Mlt::Producer parentProd(m_mltProducer->parent());
1646 if (parentProd.get_producer() == NULL) {
1647 kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
1650 //kDebug()<<"// CLP STRT LST: "<<trackClipStartList;
1651 //kDebug()<<"// TRA STRT LST: "<<trackTransitionStartList;
1653 Mlt::Service service(parentProd.get_service());
1654 Mlt::Tractor tractor(service);
1655 mlt_service_lock(service.get_service());
1656 int diff = duration.frames(m_fps);
1657 int offset = timeOffset.frames(m_fps);
1661 // insert space in one track only
1662 Mlt::Producer trackProducer(tractor.track(track));
1663 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1664 insertPos = trackClipStartList.value(track);
1665 if (insertPos != -1) {
1666 insertPos += offset;
1667 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1668 if (diff > 0) trackPlaylist.insert_blank(clipIndex, diff - 1);
1670 if (!trackPlaylist.is_blank(clipIndex)) clipIndex --;
1671 if (!trackPlaylist.is_blank(clipIndex)) kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
1672 int position = trackPlaylist.clip_start(clipIndex);
1673 trackPlaylist.remove_region(position, - diff - 1);
1675 trackPlaylist.consolidate_blanks(0);
1677 // now move transitions
1678 mlt_service serv = m_mltProducer->parent().get_service();
1679 mlt_service nextservice = mlt_service_get_producer(serv);
1680 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
1681 QString mlt_type = mlt_properties_get(properties, "mlt_type");
1682 QString resource = mlt_properties_get(properties, "mlt_service");
1684 while (mlt_type == "transition") {
1685 mlt_transition tr = (mlt_transition) nextservice;
1686 int currentTrack = mlt_transition_get_b_track(tr);
1687 int currentIn = (int) mlt_transition_get_in(tr);
1688 int currentOut = (int) mlt_transition_get_out(tr);
1689 insertPos = trackTransitionStartList.value(track);
1690 if (insertPos != -1) {
1691 insertPos += offset;
1692 if (track == currentTrack && currentOut > insertPos && resource != "mix") {
1693 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
1696 nextservice = mlt_service_producer(nextservice);
1697 if (nextservice == NULL) break;
1698 properties = MLT_SERVICE_PROPERTIES(nextservice);
1699 mlt_type = mlt_properties_get(properties, "mlt_type");
1700 resource = mlt_properties_get(properties, "mlt_service");
1703 int trackNb = tractor.count();
1704 while (trackNb > 1) {
1705 Mlt::Producer trackProducer(tractor.track(trackNb - 1));
1706 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1709 //int clipNb = trackPlaylist.count();
1710 insertPos = trackClipStartList.value(trackNb - 1);
1711 if (insertPos != -1) {
1712 insertPos += offset;
1714 /* kDebug()<<"-------------\nTRACK "<<trackNb<<" HAS "<<clipNb<<" CLPIS";
1715 kDebug() << "INSERT SPACE AT: "<<insertPos<<", DIFF: "<<diff<<", TK: "<<trackNb;
1716 for (int i = 0; i < clipNb; i++) {
1717 kDebug()<<"CLIP "<<i<<", START: "<<trackPlaylist.clip_start(i)<<", END: "<<trackPlaylist.clip_start(i) + trackPlaylist.clip_length(i);
1718 if (trackPlaylist.is_blank(i)) kDebug()<<"++ BLANK ++ ";
1719 kDebug()<<"-------------";
1721 kDebug()<<"END-------------";*/
1724 int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
1725 if (diff > 0) trackPlaylist.insert_blank(clipIndex, diff - 1);
1727 if (!trackPlaylist.is_blank(clipIndex)) clipIndex --;
1728 if (!trackPlaylist.is_blank(clipIndex)) kDebug() << "//// ERROR TRYING TO DELETE SPACE FROM " << insertPos;
1729 int position = trackPlaylist.clip_start(clipIndex);
1730 trackPlaylist.remove_region(position, - diff - 1);
1732 trackPlaylist.consolidate_blanks(0);
1736 // now move transitions
1737 mlt_service serv = m_mltProducer->parent().get_service();
1738 mlt_service nextservice = mlt_service_get_producer(serv);
1739 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
1740 QString mlt_type = mlt_properties_get(properties, "mlt_type");
1741 QString resource = mlt_properties_get(properties, "mlt_service");
1743 while (mlt_type == "transition") {
1744 mlt_transition tr = (mlt_transition) nextservice;
1745 int currentIn = (int) mlt_transition_get_in(tr);
1746 int currentOut = (int) mlt_transition_get_out(tr);
1747 int currentTrack = mlt_transition_get_b_track(tr);
1748 insertPos = trackTransitionStartList.value(currentTrack);
1749 if (insertPos != -1) {
1750 insertPos += offset;
1751 if (currentOut > insertPos && resource != "mix") {
1752 mlt_transition_set_in_and_out(tr, currentIn + diff, currentOut + diff);
1755 nextservice = mlt_service_producer(nextservice);
1756 if (nextservice == NULL) break;
1757 properties = MLT_SERVICE_PROPERTIES(nextservice);
1758 mlt_type = mlt_properties_get(properties, "mlt_type");
1759 resource = mlt_properties_get(properties, "mlt_service");
1762 mlt_service_unlock(service.get_service());
1764 m_mltConsumer->set("refresh", 1);
1768 void Render::mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest)
1770 Mlt::Service sourceService(source->get_service());
1771 Mlt::Service destService(dest->get_service());
1773 // move all effects to the correct producer
1775 Mlt::Filter *filter = sourceService.filter(ct);
1777 if (filter->get("kdenlive_ix") != 0) {
1778 sourceService.detach(*filter);
1779 destService.attach(*filter);
1781 filter = sourceService.filter(ct);
1785 int Render::mltChangeClipSpeed(ItemInfo info, double speed, double oldspeed, Mlt::Producer *prod)
1789 Mlt::Service service(m_mltProducer->parent().get_service());
1790 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
1791 //kDebug() << "Changing clip speed, set in and out: " << info.cropStart.frames(m_fps) << " to " << (info.endPos - info.startPos).frames(m_fps) - 1;
1792 Mlt::Tractor tractor(service);
1793 Mlt::Producer trackProducer(tractor.track(info.track));
1794 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1795 int startPos = info.startPos.frames(m_fps);
1796 int clipIndex = trackPlaylist.get_clip_index_at(startPos);
1797 int clipLength = trackPlaylist.clip_length(clipIndex);
1799 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
1803 if (!clip->is_valid() || clip->is_blank()) {
1808 Mlt::Producer clipparent = clip->parent();
1809 if (!clipparent.is_valid() || clipparent.is_blank()) {
1815 QString serv = clipparent.get("mlt_service");
1816 QString id = clipparent.get("id");
1817 //kDebug() << "CLIP SERVICE: " << serv;
1818 if (serv == "avformat" && speed != 1.0) {
1819 mlt_service_lock(service.get_service());
1820 QString url = QString::fromUtf8(clipparent.get("resource"));
1821 url.append('?' + QString::number(speed));
1822 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1823 if (!slowprod || slowprod->get_producer() == NULL) {
1824 char *tmp = decodedString(url);
1825 slowprod = new Mlt::Producer(*m_mltProfile, "framebuffer", tmp);
1827 QString producerid = "slowmotion:" + id + ':' + QString::number(speed);
1828 tmp = decodedString(producerid);
1829 slowprod->set("id", tmp);
1831 m_slowmotionProducers.insert(url, slowprod);
1833 trackPlaylist.replace_with_blank(clipIndex);
1834 trackPlaylist.consolidate_blanks(0);
1835 // Check that the blank space is long enough for our new duration
1836 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1837 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
1839 if (clipIndex + 1 < trackPlaylist.count() && (startPos + clipLength / speed > blankEnd)) {
1840 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
1841 cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)(info.cropStart.frames(m_fps) / speed + maxLength.frames(m_fps) - 1));
1842 } else cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)((info.cropStart.frames(m_fps) + clipLength) / speed - 1));
1844 // move all effects to the correct producer
1845 mltPasteEffects(clip, cut);
1847 trackPlaylist.insert_at(startPos, *cut, 1);
1848 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1849 newLength = trackPlaylist.clip_length(clipIndex);
1850 mlt_service_unlock(service.get_service());
1851 } else if (speed == 1.0) {
1852 mlt_service_lock(service.get_service());
1854 trackPlaylist.replace_with_blank(clipIndex);
1855 trackPlaylist.consolidate_blanks(0);
1857 // Check that the blank space is long enough for our new duration
1858 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1859 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
1862 GenTime oldDuration = GenTime(clipLength, m_fps);
1863 GenTime newDuration = oldDuration * oldspeed;
1864 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + newDuration).frames(m_fps) > blankEnd) {
1865 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
1866 cut = prod->cut((int)(info.cropStart.frames(m_fps)), (int)(info.cropStart.frames(m_fps) + maxLength.frames(m_fps) - 1));
1867 } else cut = prod->cut((int)(info.cropStart.frames(m_fps)), (int)((info.cropStart + newDuration).frames(m_fps)) - 1);
1869 // move all effects to the correct producer
1870 mltPasteEffects(clip, cut);
1872 trackPlaylist.insert_at(startPos, *cut, 1);
1873 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1874 newLength = trackPlaylist.clip_length(clipIndex);
1875 mlt_service_unlock(service.get_service());
1877 } else if (serv == "framebuffer") {
1878 mlt_service_lock(service.get_service());
1879 QString url = QString::fromUtf8(clipparent.get("resource"));
1880 url = url.section('?', 0, 0);
1881 url.append('?' + QString::number(speed));
1882 Mlt::Producer *slowprod = m_slowmotionProducers.value(url);
1883 if (!slowprod || slowprod->get_producer() == NULL) {
1884 char *tmp = decodedString(url);
1885 slowprod = new Mlt::Producer(*m_mltProfile, "framebuffer", tmp);
1887 QString producerid = "slowmotion:" + id.section(':', 1, 1) + ':' + QString::number(speed);
1888 tmp = decodedString(producerid);
1889 slowprod->set("id", tmp);
1891 m_slowmotionProducers.insert(url, slowprod);
1893 trackPlaylist.replace_with_blank(clipIndex);
1894 trackPlaylist.consolidate_blanks(0);
1896 GenTime oldDuration = GenTime(clipLength, m_fps);
1897 GenTime newDuration = oldDuration * oldspeed / speed;
1899 // Check that the blank space is long enough for our new duration
1900 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1901 int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
1904 if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + newDuration).frames(m_fps) > blankEnd) {
1905 GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
1906 cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)(info.cropStart.frames(m_fps) / speed + maxLength.frames(m_fps) - 1));
1907 } else cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)((info.cropStart / speed + newDuration).frames(m_fps) - 1));
1909 // move all effects to the correct producer
1910 mltPasteEffects(clip, cut);
1912 trackPlaylist.insert_at(startPos, *cut, 1);
1913 clipIndex = trackPlaylist.get_clip_index_at(startPos);
1914 newLength = trackPlaylist.clip_length(clipIndex);
1916 mlt_service_unlock(service.get_service());
1920 if (clipIndex + 1 == trackPlaylist.count()) mltCheckLength();
1921 m_isBlocked = false;
1925 bool Render::mltRemoveEffect(int track, GenTime position, QString index, bool updateIndex, bool doRefresh)
1927 kDebug() << "// TRYing to remove effect at: " << index;
1928 Mlt::Service service(m_mltProducer->parent().get_service());
1929 bool success = false;
1930 Mlt::Tractor tractor(service);
1931 Mlt::Producer trackProducer(tractor.track(track));
1932 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1933 //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
1934 Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
1936 kDebug() << " / / / CANNOT FIND CLIP TO REMOVE EFFECT";
1939 Mlt::Service clipService(clip->get_service());
1940 // if (tag.startsWith("ladspa")) tag = "ladspa";
1943 Mlt::Filter *filter = clipService.filter(ct);
1945 if ((index == "-1" && strcmp(filter->get("kdenlive_id"), "")) || filter->get("kdenlive_ix") == index) {// && filter->get("kdenlive_id") == id) {
1946 if (clipService.detach(*filter) == 0) success = true;
1947 kDebug() << " / / / DLEETED EFFECT: " << ct;
1948 } else if (updateIndex) {
1949 // Adjust the other effects index
1950 if (QString(filter->get("kdenlive_ix")).toInt() > index.toInt()) filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() - 1);
1953 filter = clipService.filter(ct);
1955 m_isBlocked = false;
1956 if (doRefresh) refresh();
1961 bool Render::mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh)
1964 Mlt::Service service(m_mltProducer->parent().get_service());
1966 Mlt::Tractor tractor(service);
1967 Mlt::Producer trackProducer(tractor.track(track));
1968 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
1970 Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
1974 Mlt::Service clipService(clip->get_service());
1977 // temporarily remove all effects after insert point
1978 QList <Mlt::Filter *> filtersList;
1979 const int filter_ix = params.paramValue("kdenlive_ix").toInt();
1981 Mlt::Filter *filter = clipService.filter(ct);
1983 if (QString(filter->get("kdenlive_ix")).toInt() > filter_ix) {
1984 filtersList.append(filter);
1985 clipService.detach(*filter);
1987 filter = clipService.filter(ct);
1991 QString tag = params.paramValue("tag");
1992 kDebug() << " / / INSERTING EFFECT: " << tag;
1993 if (tag.startsWith("ladspa")) tag = "ladspa";
1994 char *filterTag = decodedString(tag);
1995 char *filterId = decodedString(params.paramValue("id"));
1996 QHash<QString, QString>::Iterator it;
1997 QString kfr = params.paramValue("keyframes");
1999 if (!kfr.isEmpty()) {
2000 QStringList keyFrames = kfr.split(';', QString::SkipEmptyParts);
2001 kDebug() << "// ADDING KEYFRAME EFFECT: " << params.paramValue("keyframes");
2002 char *starttag = decodedString(params.paramValue("starttag", "start"));
2003 char *endtag = decodedString(params.paramValue("endtag", "end"));
2004 kDebug() << "// ADDING KEYFRAME TAGS: " << starttag << ", " << endtag;
2005 int duration = clip->get_playtime();
2006 //double max = params.paramValue("max").toDouble();
2007 double min = params.paramValue("min").toDouble();
2008 double factor = params.paramValue("factor", "1").toDouble();
2009 params.removeParam("starttag");
2010 params.removeParam("endtag");
2011 params.removeParam("keyframes");
2012 params.removeParam("min");
2013 params.removeParam("max");
2014 params.removeParam("factor");
2016 for (int i = 0; i < keyFrames.size() - 1; ++i) {
2017 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2018 if (filter && filter->is_valid()) {
2019 filter->set("kdenlive_id", filterId);
2020 int x1 = keyFrames.at(i).section(':', 0, 0).toInt() + offset;
2021 double y1 = keyFrames.at(i).section(':', 1, 1).toDouble();
2022 int x2 = keyFrames.at(i + 1).section(':', 0, 0).toInt();
2023 double y2 = keyFrames.at(i + 1).section(':', 1, 1).toDouble();
2024 if (x2 == -1) x2 = duration;
2026 for (int j = 0; j < params.count(); j++) {
2027 char *name = decodedString(params.at(j).name());
2028 char *value = decodedString(params.at(j).value());
2029 filter->set(name, value);
2034 filter->set("in", x1);
2035 filter->set("out", x2);
2036 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
2037 filter->set(starttag, QString::number((min + y1) / factor).toUtf8().data());
2038 filter->set(endtag, QString::number((min + y2) / factor).toUtf8().data());
2039 clipService.attach(*filter);
2046 Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
2047 if (filter && filter->is_valid())
2048 filter->set("kdenlive_id", filterId);
2050 kDebug() << "filter is NULL";
2051 m_isBlocked = false;
2055 params.removeParam("kdenlive_id");
2057 for (int j = 0; j < params.count(); j++) {
2058 char *name = decodedString(params.at(j).name());
2059 char *value = decodedString(params.at(j).value());
2060 filter->set(name, value);
2066 QString effectArgs = params.paramValue("id").section('_', 1);
2068 params.removeParam("id");
2069 params.removeParam("kdenlive_ix");
2070 params.removeParam("tag");
2071 params.removeParam("disabled");
2073 for (int j = 0; j < params.count(); j++) {
2074 effectArgs.append(' ' + params.at(j).value());
2076 //kDebug() << "SOX EFFECTS: " << effectArgs.simplified();
2077 char *value = decodedString(effectArgs.simplified());
2078 filter->set("effect", value);
2083 // attach filter to the clip
2084 clipService.attach(*filter);
2089 // re-add following filters
2090 for (int i = 0; i < filtersList.count(); i++) {
2091 clipService.attach(*(filtersList.at(i)));
2094 m_isBlocked = false;
2095 if (doRefresh) refresh();
2099 bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList params)
2101 QString index = params.paramValue("kdenlive_ix");
2102 QString tag = params.paramValue("tag");
2104 if (!params.paramValue("keyframes").isEmpty() || /*it.key().startsWith("#") || */tag.startsWith("ladspa") || tag == "sox" || tag == "autotrack_rectangle") {
2105 // This is a keyframe effect, to edit it, we remove it and re-add it.
2106 mltRemoveEffect(track, position, index, true);
2107 bool success = mltAddEffect(track, position, params);
2112 Mlt::Service service(m_mltProducer->parent().get_service());
2114 Mlt::Tractor tractor(service);
2115 Mlt::Producer trackProducer(tractor.track(track));
2116 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2117 //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
2118 Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
2120 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2123 Mlt::Service clipService(clip->get_service());
2126 Mlt::Filter *filter = clipService.filter(ct);
2128 if (filter->get("kdenlive_ix") == index) {
2132 filter = clipService.filter(ct);
2136 kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT!!!!!";
2137 // filter was not found, it was probably a disabled filter, so add it to the correct place...
2139 filter = clipService.filter(ct);
2140 QList <Mlt::Filter *> filtersList;
2142 if (QString(filter->get("kdenlive_ix")).toInt() > index.toInt()) {
2143 filtersList.append(filter);
2144 clipService.detach(*filter);
2146 filter = clipService.filter(ct);
2148 bool success = mltAddEffect(track, position, params);
2150 for (int i = 0; i < filtersList.count(); i++) {
2151 clipService.attach(*(filtersList.at(i)));
2154 m_isBlocked = false;
2158 for (int j = 0; j < params.count(); j++) {
2159 char *name = decodedString(params.at(j).name());
2160 char *value = decodedString(params.at(j).value());
2161 filter->set(name, value);
2166 m_isBlocked = false;
2171 void Render::mltMoveEffect(int track, GenTime position, int oldPos, int newPos)
2174 kDebug() << "MOVING EFFECT FROM " << oldPos << ", TO: " << newPos;
2175 Mlt::Service service(m_mltProducer->parent().get_service());
2177 Mlt::Tractor tractor(service);
2178 Mlt::Producer trackProducer(tractor.track(track));
2179 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2180 //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
2181 Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
2183 kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
2186 Mlt::Service clipService(clip->get_service());
2189 QList <Mlt::Filter *> filtersList;
2190 Mlt::Filter *filter = clipService.filter(ct);
2192 if (newPos > oldPos) {
2194 if (!found && QString(filter->get("kdenlive_ix")).toInt() == oldPos) {
2195 filter->set("kdenlive_ix", newPos);
2196 filtersList.append(filter);
2197 clipService.detach(*filter);
2198 filter = clipService.filter(ct);
2199 while (filter && QString(filter->get("kdenlive_ix")).toInt() <= newPos) {
2200 filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() - 1);
2202 filter = clipService.filter(ct);
2206 if (filter && QString(filter->get("kdenlive_ix")).toInt() > newPos) {
2207 filtersList.append(filter);
2208 clipService.detach(*filter);
2210 filter = clipService.filter(ct);
2214 if (QString(filter->get("kdenlive_ix")).toInt() == oldPos) {
2215 filter->set("kdenlive_ix", newPos);
2216 filtersList.append(filter);
2217 clipService.detach(*filter);
2219 filter = clipService.filter(ct);
2223 filter = clipService.filter(ct);
2225 int pos = QString(filter->get("kdenlive_ix")).toInt();
2226 if (pos >= newPos) {
2227 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
2228 filtersList.append(filter);
2229 clipService.detach(*filter);
2231 filter = clipService.filter(ct);
2235 for (int i = 0; i < filtersList.count(); i++) {
2236 clipService.attach(*(filtersList.at(i)));
2239 m_isBlocked = false;
2243 bool Render::mltResizeClipEnd(ItemInfo info, GenTime clipDuration)
2246 Mlt::Service service(m_mltProducer->parent().get_service());
2247 Mlt::Tractor tractor(service);
2248 Mlt::Producer trackProducer(tractor.track(info.track));
2249 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2251 /* // Display playlist info
2252 kDebug()<<"//////////// BEFORE RESIZE";
2253 for (int i = 0; i < trackPlaylist.count(); i++) {
2254 int blankStart = trackPlaylist.clip_start(i);
2255 int blankDuration = trackPlaylist.clip_length(i) - 1;
2257 if (trackPlaylist.is_blank(i)) blk = "(blank)";
2258 kDebug()<<"CLIP "<<i<<": ("<<blankStart<<'x'<<blankStart + blankDuration<<")"<<blk;
2261 if (trackPlaylist.is_blank_at((int) info.startPos.frames(m_fps))) {
2262 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
2263 m_isBlocked = false;
2266 mlt_service_lock(service.get_service());
2267 int clipIndex = trackPlaylist.get_clip_index_at((int) info.startPos.frames(m_fps));
2268 //kDebug() << "// SELECTED CLIP START: " << trackPlaylist.clip_start(clipIndex);
2269 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2270 int previousStart = clip->get_in();
2271 int newDuration = (int) clipDuration.frames(m_fps) - 1;
2272 int diff = newDuration - (trackPlaylist.clip_length(clipIndex) - 1);
2273 if (newDuration > clip->get_length()) {
2274 clip->parent().set("length", newDuration + 1);
2275 clip->set("length", newDuration + 1);
2277 if (newDuration > clip->get_out()) {
2278 clip->parent().set_in_and_out(0, newDuration + 1);
2279 clip->set_in_and_out(0, newDuration + 1);
2282 trackPlaylist.resize_clip(clipIndex, previousStart, newDuration + previousStart);
2283 trackPlaylist.consolidate_blanks(0);
2284 // skip to next clip
2286 //kDebug() << "//////// RESIZE CLIP: " << clipIndex << "( pos: " << info.startPos.frames(25) << "), DIFF: " << diff << ", CURRENT DUR: " << previousDuration << ", NEW DUR: " << newDuration << ", IX: " << clipIndex << ", MAX: " << trackPlaylist.count();
2288 // clip was made longer, trim next blank if there is one.
2289 if (clipIndex < trackPlaylist.count()) {
2290 // If this is not the last clip in playlist
2291 if (trackPlaylist.is_blank(clipIndex)) {
2292 int blankStart = trackPlaylist.clip_start(clipIndex);
2293 int blankDuration = trackPlaylist.clip_length(clipIndex) - 1;
2294 if (diff > blankDuration) kDebug() << "// ERROR blank clip is not large enough to get back required space!!!";
2295 if (diff - blankDuration == 1) {
2296 trackPlaylist.remove(clipIndex);
2297 } else trackPlaylist.remove_region(blankStart, diff - 1);
2299 kDebug() << "/// RESIZE ERROR, NXT CLIP IS NOT BLK: " << clipIndex;
2302 } else trackPlaylist.insert_blank(clipIndex, 0 - diff - 1);
2303 trackPlaylist.consolidate_blanks(0);
2304 mlt_service_unlock(service.get_service());
2306 if (info.track != 0 && clipIndex == trackPlaylist.count()) mltCheckLength();
2307 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
2308 //mltResizeTransparency(previousStart, previousStart, previousStart + newDuration, track, QString(clip->parent().get("id")).toInt());
2309 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
2310 ItemInfo transpinfo;
2311 transpinfo.startPos = info.startPos;
2312 transpinfo.endPos = info.startPos + clipDuration;
2313 transpinfo.track = info.track;
2314 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
2316 m_isBlocked = false;
2317 m_mltConsumer->set("refresh", 1);
2321 void Render::mltChangeTrackState(int track, bool mute, bool blind)
2323 Mlt::Service service(m_mltProducer->parent().get_service());
2324 Mlt::Tractor tractor(service);
2325 Mlt::Producer trackProducer(tractor.track(track));
2328 if (blind) trackProducer.set("hide", 3);
2329 else trackProducer.set("hide", 2);
2331 trackProducer.set("hide", 1);
2333 trackProducer.set("hide", 0);
2335 tractor.multitrack()->refresh();
2341 bool Render::mltResizeClipCrop(ItemInfo info, GenTime diff)
2343 Mlt::Service service(m_mltProducer->parent().get_service());
2344 int frameOffset = (int) diff.frames(m_fps);
2345 Mlt::Tractor tractor(service);
2346 Mlt::Producer trackProducer(tractor.track(info.track));
2347 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2348 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
2349 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
2352 mlt_service_lock(service.get_service());
2353 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
2354 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2356 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
2357 mlt_service_unlock(service.get_service());
2360 int previousStart = clip->get_in();
2362 int previousDuration = trackPlaylist.clip_length(clipIndex) - 1;
2364 trackPlaylist.resize_clip(clipIndex, previousStart + frameOffset, previousStart + previousDuration + frameOffset);
2365 m_isBlocked = false;
2366 mlt_service_unlock(service.get_service());
2367 m_mltConsumer->set("refresh", 1);
2371 bool Render::mltResizeClipStart(ItemInfo info, GenTime diff)
2373 //kDebug() << "//////// RSIZING CLIP from: "<<info.startPos.frames(25)<<" to "<<diff.frames(25);
2374 Mlt::Service service(m_mltProducer->parent().get_service());
2375 int moveFrame = (int) diff.frames(m_fps);
2376 Mlt::Tractor tractor(service);
2377 Mlt::Producer trackProducer(tractor.track(info.track));
2378 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2379 if (trackPlaylist.is_blank_at(info.startPos.frames(m_fps))) {
2380 kDebug() << "//////// ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
2383 mlt_service_lock(service.get_service());
2384 int clipIndex = trackPlaylist.get_clip_index_at(info.startPos.frames(m_fps));
2385 Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
2387 kDebug() << "//////// ERROR RSIZING NULL CLIP!!!!!!!!!!!";
2388 mlt_service_unlock(service.get_service());
2391 int previousStart = clip->get_in();
2393 int previousDuration = trackPlaylist.clip_length(clipIndex) - 1;
2395 kDebug() << "RESIZE, old start: " << previousStart << ", PREV DUR: " << previousDuration << ", DIFF: " << moveFrame;
2396 trackPlaylist.resize_clip(clipIndex, previousStart + moveFrame, previousStart + previousDuration);
2397 if (moveFrame > 0) trackPlaylist.insert_blank(clipIndex, moveFrame - 1);
2399 //int midpos = info.startPos.frames(m_fps) + moveFrame - 1;
2400 int blankIndex = clipIndex - 1;
2401 int blankLength = trackPlaylist.clip_length(blankIndex);
2402 kDebug() << " + resizing blank length " << blankLength << ", SIZE DIFF: " << moveFrame;
2403 if (! trackPlaylist.is_blank(blankIndex)) {
2404 kDebug() << "WARNING, CLIP TO RESIZE IS NOT BLANK";
2406 if (blankLength + moveFrame == 0) trackPlaylist.remove(blankIndex);
2407 else trackPlaylist.resize_clip(blankIndex, 0, blankLength + moveFrame - 1);
2409 trackPlaylist.consolidate_blanks(0);
2410 /*if (QString(clip->parent().get("transparency")).toInt() == 1) {
2411 //mltResizeTransparency(previousStart, (int) moveEnd.frames(m_fps), (int) (moveEnd + out - in).frames(m_fps), track, QString(clip->parent().get("id")).toInt());
2412 mltDeleteTransparency(info.startPos.frames(m_fps), info.track, QString(clip->parent().get("id")).toInt());
2413 ItemInfo transpinfo;
2414 transpinfo.startPos = info.startPos + diff;
2415 transpinfo.endPos = info.startPos + diff + (info.endPos - info.startPos);
2416 transpinfo.track = info.track;
2417 mltAddClipTransparency(transpinfo, info.track - 1, QString(clip->parent().get("id")).toInt());
2419 m_isBlocked = false;
2420 //m_mltConsumer->set("refresh", 1);
2421 mlt_service_unlock(service.get_service());
2422 m_mltConsumer->set("refresh", 1);
2426 bool Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd, Mlt::Producer *prod)
2428 return mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps), prod);
2432 void Render::mltUpdateClipProducer(int track, int pos, Mlt::Producer *prod)
2434 if (prod == NULL || !prod->is_valid()) {
2435 kDebug() << "// Warning, CLIP on track " << track << ", at: " << pos << " is invalid, cannot update it!!!";
2438 kDebug() << "NEW PROD ID: " << prod->get("id");
2440 kDebug() << "// TRYING TO UPDATE CLIP at: " << pos << ", TK: " << track;
2441 mlt_service_lock(m_mltConsumer->get_service());
2442 Mlt::Service service(m_mltProducer->parent().get_service());
2443 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
2445 Mlt::Tractor tractor(service);
2446 Mlt::Producer trackProducer(tractor.track(track));
2447 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2448 int clipIndex = trackPlaylist.get_clip_index_at(pos + 1);
2449 Mlt::Producer clipProducer(trackPlaylist.replace_with_blank(clipIndex));
2450 if (clipProducer.is_blank()) {
2451 kDebug() << "// ERROR UPDATING CLIP PROD";
2452 mlt_service_unlock(m_mltConsumer->get_service());
2456 Mlt::Producer *clip = prod->cut(clipProducer.get_in(), clipProducer.get_out());
2458 // move all effects to the correct producer
2459 mltPasteEffects(&clipProducer, clip);
2461 trackPlaylist.insert_at(pos, clip, 1);
2462 mlt_service_unlock(m_mltConsumer->get_service());
2466 bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd, Mlt::Producer *prod)
2470 Mlt::Service service(m_mltProducer->parent().get_service());
2471 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
2473 Mlt::Tractor tractor(service);
2474 mlt_service_lock(service.get_service());
2475 Mlt::Producer trackProducer(tractor.track(startTrack));
2476 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2477 int clipIndex = trackPlaylist.get_clip_index_at(moveStart);
2478 kDebug() << "////// LOOKING FOR CLIP TO MOVE, INDEX: " << clipIndex;
2479 bool checkLength = false;
2480 if (endTrack == startTrack) {
2481 Mlt::Producer clipProducer(trackPlaylist.replace_with_blank(clipIndex));
2482 if (!trackPlaylist.is_blank_at(moveEnd) || clipProducer.is_blank()) {
2483 // error, destination is not empty
2484 if (!trackPlaylist.is_blank_at(moveEnd)) trackPlaylist.insert_at(moveStart, clipProducer, 1);
2485 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
2486 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
2487 mlt_service_unlock(service.get_service());
2491 trackPlaylist.consolidate_blanks(0);
2492 int newIndex = trackPlaylist.insert_at(moveEnd, clipProducer, 1);
2493 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
2494 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
2496 if (newIndex + 1 == trackPlaylist.count()) checkLength = true;
2498 //mlt_service_unlock(service.get_service());
2500 Mlt::Producer destTrackProducer(tractor.track(endTrack));
2501 Mlt::Playlist destTrackPlaylist((mlt_playlist) destTrackProducer.get_service());
2502 if (!destTrackPlaylist.is_blank_at(moveEnd)) {
2503 // error, destination is not empty
2504 mlt_service_unlock(service.get_service());
2508 Mlt::Producer clipProducer(trackPlaylist.replace_with_blank(clipIndex));
2509 if (clipProducer.is_blank()) {
2510 // error, destination is not empty
2511 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
2512 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
2513 mlt_service_unlock(service.get_service());
2517 trackPlaylist.consolidate_blanks(0);
2518 destTrackPlaylist.consolidate_blanks(1);
2519 Mlt::Producer *clip;
2520 // check if we are moving a slowmotion producer
2521 QString serv = clipProducer.parent().get("mlt_service");
2522 QString currentid = clipProducer.parent().get("id");
2523 if (serv == "framebuffer" || currentid.endsWith("_video")) {
2524 clip = &clipProducer;
2527 // Special case: prod is null when using placeholder clips.
2528 // in that case, use the producer existing in playlist. Note that
2529 // it will bypass the one producer per track logic and might cause
2530 // Sound cracks if clip is moved so that it overlaps another copy of itself
2531 clip = clipProducer.cut(clipProducer.get_in(), clipProducer.get_out());
2532 } else clip = prod->cut(clipProducer.get_in(), clipProducer.get_out());
2535 // move all effects to the correct producer
2536 mltPasteEffects(&clipProducer, clip);
2538 int newIndex = destTrackPlaylist.insert_at(moveEnd, clip, 1);
2539 destTrackPlaylist.consolidate_blanks(0);
2540 /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
2541 kDebug() << "//////// moving clip transparency";
2542 mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
2544 if (clipIndex > trackPlaylist.count()) checkLength = true;
2545 else if (newIndex + 1 == destTrackPlaylist.count()) checkLength = true;
2548 mlt_service_unlock(service.get_service());
2549 if (checkLength) mltCheckLength();
2552 //m_mltConsumer->set("refresh", 1);
2556 bool Render::mltMoveTransition(QString type, int startTrack, int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut)
2558 int new_in = (int)newIn.frames(m_fps);
2559 int new_out = (int)newOut.frames(m_fps) - 1;
2560 if (new_in >= new_out) return false;
2562 mlt_service serv = m_mltProducer->parent().get_service();
2564 mlt_service_lock(serv);
2565 //m_mltConsumer->set("refresh", 0);
2568 mlt_service nextservice = mlt_service_get_producer(serv);
2569 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2570 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2571 QString resource = mlt_properties_get(properties, "mlt_service");
2572 int old_pos = (int)(oldIn.frames(m_fps) + oldOut.frames(m_fps)) / 2;
2574 while (mlt_type == "transition") {
2575 mlt_transition tr = (mlt_transition) nextservice;
2576 int currentTrack = mlt_transition_get_b_track(tr);
2577 int currentIn = (int) mlt_transition_get_in(tr);
2578 int currentOut = (int) mlt_transition_get_out(tr);
2580 if (resource == type && startTrack == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
2581 mlt_transition_set_in_and_out(tr, new_in, new_out);
2582 if (newTrack - startTrack != 0) {
2583 //kDebug() << "///// TRANSITION CHANGE TRACK. CUrrent (b): " << currentTrack << 'x' << mlt_transition_get_a_track(tr) << ", NEw: " << newTrack << 'x' << newTransitionTrack;
2585 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
2586 mlt_properties_set_int(properties, "a_track", newTransitionTrack);
2587 mlt_properties_set_int(properties, "b_track", newTrack);
2588 //kDebug() << "set new start & end :" << new_in << new_out<< "TR OFFSET: "<<trackOffset<<", TRACKS: "<<mlt_transition_get_a_track(tr)<<'x'<<mlt_transition_get_b_track(tr);
2592 nextservice = mlt_service_producer(nextservice);
2593 if (nextservice == NULL) break;
2594 properties = MLT_SERVICE_PROPERTIES(nextservice);
2595 mlt_type = mlt_properties_get(properties, "mlt_type");
2596 resource = mlt_properties_get(properties, "mlt_service");
2598 mlt_service_unlock(serv);
2601 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
2605 void Render::mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml)
2607 if (oldTag == tag) mltUpdateTransitionParams(tag, a_track, b_track, in, out, xml);
2609 mltDeleteTransition(oldTag, a_track, b_track, in, out, xml, false);
2610 mltAddTransition(tag, a_track, b_track, in, out, xml);
2612 //mltSavePlaylist();
2615 void Render::mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml)
2617 mlt_service serv = m_mltProducer->parent().get_service();
2618 mlt_service_lock(serv);
2622 mlt_service nextservice = mlt_service_get_producer(serv);
2623 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2624 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2625 QString resource = mlt_properties_get(properties, "mlt_service");
2626 int in_pos = (int) in.frames(m_fps);
2627 int out_pos = (int) out.frames(m_fps) - 1;
2629 while (mlt_type == "transition") {
2630 mlt_transition tr = (mlt_transition) nextservice;
2631 int currentTrack = mlt_transition_get_b_track(tr);
2632 int currentBTrack = mlt_transition_get_a_track(tr);
2633 int currentIn = (int) mlt_transition_get_in(tr);
2634 int currentOut = (int) mlt_transition_get_out(tr);
2636 // kDebug()<<"Looking for transition : " << currentIn <<'x'<<currentOut<< ", OLD oNE: "<<in_pos<<'x'<<out_pos;
2638 if (resource == type && b_track == currentTrack && currentIn == in_pos && currentOut == out_pos) {
2639 QMap<QString, QString> map = mltGetTransitionParamsFromXml(xml);
2640 QMap<QString, QString>::Iterator it;
2642 mlt_properties transproperties = MLT_TRANSITION_PROPERTIES(tr);
2643 mlt_properties_set_int(transproperties, "force_track", xml.attribute("force_track").toInt());
2644 if (currentBTrack != a_track) {
2645 mlt_properties_set_int(properties, "a_track", a_track);
2647 for (it = map.begin(); it != map.end(); ++it) {
2649 char *name = decodedString(key);
2650 char *value = decodedString(it.value());
2651 mlt_properties_set(transproperties, name, value);
2652 //kDebug() << " ------ UPDATING TRANS PARAM: " << name << ": " << value;
2653 //filter->set("kdenlive_id", id);
2659 nextservice = mlt_service_producer(nextservice);
2660 if (nextservice == NULL) break;
2661 properties = MLT_SERVICE_PROPERTIES(nextservice);
2662 mlt_type = mlt_properties_get(properties, "mlt_type");
2663 resource = mlt_properties_get(properties, "mlt_service");
2665 mlt_service_unlock(serv);
2668 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
2671 void Render::mltDeleteTransition(QString tag, int /*a_track*/, int b_track, GenTime in, GenTime out, QDomElement /*xml*/, bool /*do_refresh*/)
2673 mlt_service serv = m_mltProducer->parent().get_service();
2675 mlt_service_lock(serv);
2677 Mlt::Service service(serv);
2678 Mlt::Tractor tractor(service);
2679 Mlt::Field *field = tractor.field();
2681 //if (do_refresh) m_mltConsumer->set("refresh", 0);
2683 mlt_service nextservice = mlt_service_get_producer(serv);
2684 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2685 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2686 QString resource = mlt_properties_get(properties, "mlt_service");
2688 const int old_pos = (int)((in + out).frames(m_fps) / 2);
2689 kDebug() << " del trans pos: " << in.frames(25) << "-" << out.frames(25);
2691 while (mlt_type == "transition") {
2692 mlt_transition tr = (mlt_transition) nextservice;
2693 int currentTrack = mlt_transition_get_b_track(tr);
2694 int currentIn = (int) mlt_transition_get_in(tr);
2695 int currentOut = (int) mlt_transition_get_out(tr);
2696 //kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
2698 if (resource == tag && b_track == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
2699 mlt_field_disconnect_service(field->get_field(), nextservice);
2702 nextservice = mlt_service_producer(nextservice);
2703 if (nextservice == NULL) break;
2704 properties = MLT_SERVICE_PROPERTIES(nextservice);
2705 mlt_type = mlt_properties_get(properties, "mlt_type");
2706 resource = mlt_properties_get(properties, "mlt_service");
2708 mlt_service_unlock(serv);
2711 //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
2714 QMap<QString, QString> Render::mltGetTransitionParamsFromXml(QDomElement xml)
2716 QDomNodeList attribs = xml.elementsByTagName("parameter");
2717 QMap<QString, QString> map;
2718 for (int i = 0; i < attribs.count(); i++) {
2719 QDomElement e = attribs.item(i).toElement();
2720 QString name = e.attribute("name");
2721 //kDebug()<<"-- TRANSITION PARAM: "<<name<<" = "<< e.attribute("name")<<" / " << e.attribute("value");
2722 map[name] = e.attribute("default");
2723 if (!e.attribute("value").isEmpty()) {
2724 map[name] = e.attribute("value");
2726 if (!e.attribute("factor").isEmpty() && e.attribute("factor").toDouble() > 0) {
2727 map[name] = QString::number(map[name].toDouble() / e.attribute("factor").toDouble());
2728 //map[name]=map[name].replace(".",","); //FIXME how to solve locale conversion of . ,
2731 if (e.attribute("namedesc").contains(';')) {
2732 QString format = e.attribute("format");
2733 QStringList separators = format.split("%d", QString::SkipEmptyParts);
2734 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
2736 QTextStream txtNeu(&neu);
2737 if (values.size() > 0)
2738 txtNeu << (int)values[0].toDouble();
2740 for (i = 0; i < separators.size() && i + 1 < values.size(); i++) {
2741 txtNeu << separators[i];
2742 txtNeu << (int)(values[i+1].toDouble());
2744 if (i < separators.size())
2745 txtNeu << separators[i];
2746 map[e.attribute("name")] = neu;
2753 void Render::mltAddClipTransparency(ItemInfo info, int transitiontrack, int id)
2755 kDebug() << "///////// ADDING CLIP TRANSPARENCY AT: " << info.startPos.frames(25);
2756 Mlt::Service service(m_mltProducer->parent().get_service());
2757 Mlt::Tractor tractor(service);
2758 Mlt::Field *field = tractor.field();
2760 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
2761 transition->set_in_and_out((int) info.startPos.frames(m_fps), (int) info.endPos.frames(m_fps) - 1);
2762 transition->set("transparency", id);
2763 transition->set("fill", 1);
2764 transition->set("internal_added", 237);
2765 field->plant_transition(*transition, transitiontrack, info.track);
2769 void Render::mltDeleteTransparency(int pos, int track, int id)
2771 Mlt::Service service(m_mltProducer->parent().get_service());
2772 Mlt::Tractor tractor(service);
2773 Mlt::Field *field = tractor.field();
2775 //if (do_refresh) m_mltConsumer->set("refresh", 0);
2776 mlt_service serv = m_mltProducer->parent().get_service();
2778 mlt_service nextservice = mlt_service_get_producer(serv);
2779 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2780 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2781 QString resource = mlt_properties_get(properties, "mlt_service");
2783 while (mlt_type == "transition") {
2784 mlt_transition tr = (mlt_transition) nextservice;
2785 int currentTrack = mlt_transition_get_b_track(tr);
2786 int currentIn = (int) mlt_transition_get_in(tr);
2787 int currentOut = (int) mlt_transition_get_out(tr);
2788 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
2789 kDebug() << "// FOUND EXISTING TRANS, IN: " << currentIn << ", OUT: " << currentOut << ", TRACK: " << currentTrack;
2791 if (resource == "composite" && track == currentTrack && currentIn == pos && transitionId == id) {
2792 //kDebug() << " / / / / /DELETE TRANS DOOOMNE";
2793 mlt_field_disconnect_service(field->get_field(), nextservice);
2796 nextservice = mlt_service_producer(nextservice);
2797 if (nextservice == NULL) break;
2798 properties = MLT_SERVICE_PROPERTIES(nextservice);
2799 mlt_type = mlt_properties_get(properties, "mlt_type");
2800 resource = mlt_properties_get(properties, "mlt_service");
2802 //if (do_refresh) m_mltConsumer->set("refresh", 1);
2805 void Render::mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id)
2807 Mlt::Service service(m_mltProducer->parent().get_service());
2808 Mlt::Tractor tractor(service);
2810 mlt_service_lock(service.get_service());
2811 m_mltConsumer->set("refresh", 0);
2814 mlt_service serv = m_mltProducer->parent().get_service();
2815 mlt_service nextservice = mlt_service_get_producer(serv);
2816 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2817 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2818 QString resource = mlt_properties_get(properties, "mlt_service");
2819 kDebug() << "// resize transpar from: " << oldStart << ", TO: " << newStart << 'x' << newEnd << ", " << track << ", " << id;
2820 while (mlt_type == "transition") {
2821 mlt_transition tr = (mlt_transition) nextservice;
2822 int currentTrack = mlt_transition_get_b_track(tr);
2823 int currentIn = (int) mlt_transition_get_in(tr);
2824 //mlt_properties props = MLT_TRANSITION_PROPERTIES(tr);
2825 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
2826 kDebug() << "// resize transpar current in: " << currentIn << ", Track: " << currentTrack << ", id: " << id << 'x' << transitionId ;
2827 if (resource == "composite" && track == currentTrack && currentIn == oldStart && transitionId == id) {
2828 kDebug() << " / / / / /RESIZE TRANS TO: " << newStart << 'x' << newEnd;
2829 mlt_transition_set_in_and_out(tr, newStart, newEnd);
2832 nextservice = mlt_service_producer(nextservice);
2833 if (nextservice == NULL) break;
2834 properties = MLT_SERVICE_PROPERTIES(nextservice);
2835 mlt_type = mlt_properties_get(properties, "mlt_type");
2836 resource = mlt_properties_get(properties, "mlt_service");
2838 mlt_service_unlock(service.get_service());
2840 if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
2844 void Render::mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id)
2846 Mlt::Service service(m_mltProducer->parent().get_service());
2847 Mlt::Tractor tractor(service);
2849 mlt_service_lock(service.get_service());
2850 m_mltConsumer->set("refresh", 0);
2853 mlt_service serv = m_mltProducer->parent().get_service();
2854 mlt_service nextservice = mlt_service_get_producer(serv);
2855 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
2856 QString mlt_type = mlt_properties_get(properties, "mlt_type");
2857 QString resource = mlt_properties_get(properties, "mlt_service");
2859 while (mlt_type == "transition") {
2860 mlt_transition tr = (mlt_transition) nextservice;
2861 int currentTrack = mlt_transition_get_b_track(tr);
2862 int currentaTrack = mlt_transition_get_a_track(tr);
2863 int currentIn = (int) mlt_transition_get_in(tr);
2864 int currentOut = (int) mlt_transition_get_out(tr);
2865 //mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
2866 int transitionId = QString(mlt_properties_get(properties, "transparency")).toInt();
2867 //kDebug()<<" + TRANSITION "<<id<<" == "<<transitionId<<", START TMIE: "<<currentIn<<", LOOK FR: "<<startTime<<", TRACK: "<<currentTrack<<'x'<<startTrack;
2868 if (resource == "composite" && transitionId == id && startTime == currentIn && startTrack == currentTrack) {
2869 kDebug() << "//////MOVING";
2870 mlt_transition_set_in_and_out(tr, endTime, endTime + currentOut - currentIn);
2871 if (endTrack != startTrack) {
2872 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
2873 mlt_properties_set_int(properties, "a_track", currentaTrack + endTrack - currentTrack);
2874 mlt_properties_set_int(properties, "b_track", endTrack);
2878 nextservice = mlt_service_producer(nextservice);
2879 if (nextservice == NULL) break;
2880 properties = MLT_SERVICE_PROPERTIES(nextservice);
2881 mlt_type = mlt_properties_get(properties, "mlt_type");
2882 resource = mlt_properties_get(properties, "mlt_service");
2885 mlt_service_unlock(service.get_service());
2886 m_mltConsumer->set("refresh", 1);
2890 bool Render::mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool /*do_refresh*/)
2892 if (in >= out) return false;
2893 QMap<QString, QString> args = mltGetTransitionParamsFromXml(xml);
2894 Mlt::Service service(m_mltProducer->parent().get_service());
2896 Mlt::Tractor tractor(service);
2897 Mlt::Field *field = tractor.field();
2899 char *transId = decodedString(tag);
2900 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, transId);
2901 if (out != GenTime())
2902 transition->set_in_and_out((int) in.frames(m_fps), (int) out.frames(m_fps) - 1);
2903 QMap<QString, QString>::Iterator it;
2905 if (xml.attribute("automatic") == "1") transition->set("automatic", 1);
2906 //kDebug() << " ------ ADDING TRANSITION PARAMs: " << args.count();
2907 if (xml.hasAttribute("id"))
2908 transition->set("kdenlive_id", xml.attribute("id").toUtf8().constData());
2910 for (it = args.begin(); it != args.end(); ++it) {
2912 char *name = decodedString(key);
2913 char *value = decodedString(it.value());
2914 if (it.value().isEmpty() == false) transition->set(name, value);
2915 //kDebug() << " ------ ADDING TRANS PARAM: " << name << ": " << value;
2919 // attach filter to the clip
2920 field->plant_transition(*transition, a_track, b_track);
2926 void Render::mltSavePlaylist()
2928 kWarning() << "// UPDATING PLAYLIST TO DISK++++++++++++++++";
2929 Mlt::Consumer fileConsumer(*m_mltProfile, "xml");
2930 fileConsumer.set("resource", "/tmp/playlist.mlt");
2932 Mlt::Service service(m_mltProducer->get_service());
2934 fileConsumer.connect(service);
2935 fileConsumer.start();
2938 QList <Mlt::Producer *> Render::producersList()
2940 QList <Mlt::Producer *> prods;
2941 if (m_mltProducer == NULL) return prods;
2942 Mlt::Service service(m_mltProducer->parent().get_service());
2943 if (service.type() != tractor_type) return prods;
2944 Mlt::Tractor tractor(service);
2947 int trackNb = tractor.count();
2948 for (int t = 1; t < trackNb; t++) {
2949 Mlt::Producer *tt = tractor.track(t);
2950 Mlt::Producer trackProducer(tt);
2952 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2953 int clipNb = trackPlaylist.count();
2954 //kDebug() << "// PARSING SCENE TRACK: " << t << ", CLIPS: " << clipNb;
2955 for (int i = 0; i < clipNb; i++) {
2956 Mlt::Producer *c = trackPlaylist.get_clip(i);
2957 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
2959 if (!nprod->is_blank() && !ids.contains(nprod->get("id"))) {
2960 ids.append(nprod->get("id"));
2961 prods.append(nprod);
2962 } else delete nprod;
2970 void Render::fillSlowMotionProducers()
2972 if (m_mltProducer == NULL) return;
2973 Mlt::Service service(m_mltProducer->parent().get_service());
2974 if (service.type() != tractor_type) return;
2976 Mlt::Tractor tractor(service);
2978 int trackNb = tractor.count();
2979 for (int t = 1; t < trackNb; t++) {
2980 Mlt::Producer *tt = tractor.track(t);
2981 Mlt::Producer trackProducer(tt);
2983 Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
2984 int clipNb = trackPlaylist.count();
2985 for (int i = 0; i < clipNb; i++) {
2986 Mlt::Producer *c = trackPlaylist.get_clip(i);
2987 Mlt::Producer *nprod = new Mlt::Producer(c->get_parent());
2989 QString id = nprod->get("id");
2990 if (id.startsWith("slowmotion:") && !nprod->is_blank()) {
2991 // this is a slowmotion producer, add it to the list
2992 QString url = QString::fromUtf8(nprod->get("resource"));
2993 if (!m_slowmotionProducers.contains(url)) {
2994 m_slowmotionProducers.insert(url, nprod);
2996 } else delete nprod;
3003 void Render::mltInsertTrack(int ix, bool videoTrack)
3008 Mlt::Service service(m_mltProducer->parent().get_service());
3009 mlt_service_lock(service.get_service());
3010 if (service.type() != tractor_type) kWarning() << "// TRACTOR PROBLEM";
3012 Mlt::Tractor tractor(service);
3014 Mlt::Playlist playlist;
3015 int ct = tractor.count();
3017 kDebug() << "// ERROR, TRYING TO insert TRACK " << ix << ", max: " << ct;
3023 Mlt::Producer *prodToMove = new Mlt::Producer(tractor.track(pos));
3024 tractor.set_track(playlist, pos);
3025 Mlt::Producer newProd(tractor.track(pos));
3026 if (!videoTrack) newProd.set("hide", 1);
3028 for (; pos <= ct; pos++) {
3029 Mlt::Producer *prodToMove2 = new Mlt::Producer(tractor.track(pos));
3030 tractor.set_track(*prodToMove, pos);
3031 prodToMove = prodToMove2;
3034 tractor.set_track(playlist, ix);
3035 Mlt::Producer newProd(tractor.track(ix));
3036 if (!videoTrack) newProd.set("hide", 1);
3040 mlt_service serv = m_mltProducer->parent().get_service();
3041 mlt_service nextservice = mlt_service_get_producer(serv);
3042 mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
3043 QString mlt_type = mlt_properties_get(properties, "mlt_type");
3044 QString resource = mlt_properties_get(properties, "mlt_service");
3046 while (mlt_type == "transition") {
3047 if (resource != "mix") {
3048 mlt_transition tr = (mlt_transition) nextservice;
3049 int currentTrack = mlt_transition_get_b_track(tr);
3050 int currentaTrack = mlt_transition_get_a_track(tr);
3051 mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
3053 if (currentTrack >= ix) {
3054 mlt_properties_set_int(properties, "b_track", currentTrack + 1);
3055 mlt_properties_set_int(properties, "a_track", currentaTrack + 1);
3058 nextservice = mlt_service_producer(nextservice);
3059 if (nextservice == NULL) break;
3060 properties = MLT_SERVICE_PROPERTIES(nextservice);
3061 mlt_type = mlt_properties_get(properties, "mlt_type");
3062 resource = mlt_properties_get(properties, "mlt_service");
3065 // Add audio mix transition to last track
3066 Mlt::Field *field = tractor.field();
3067 Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "mix");
3068 transition->set("a_track", 1);
3069 transition->set("b_track", ct);
3070 transition->set("always_active", 1);
3071 transition->set("internal_added", 237);
3072 transition->set("combine", 1);
3073 field->plant_transition(*transition, 1, ct);
3074 //mlt_service_unlock(m_mltConsumer->get_service());
3075 mlt_service_unlock(service.get_service());
3076 //tractor.multitrack()->refresh();
3077 //tractor.refresh();
3079 blockSignals(false);
3083 void Render::mltDeleteTrack(int ix)
3086 doc.setContent(sceneList(), false);
3087 int tracksCount = doc.elementsByTagName("track").count() - 1;
3088 QDomNode track = doc.elementsByTagName("track").at(ix);
3089 QDomNode tractor = doc.elementsByTagName("tractor").at(0);
3090 QDomNodeList transitions = doc.elementsByTagName("transition");
3091 for (int i = 0; i < transitions.count(); i++) {
3092 QDomElement e = transitions.at(i).toElement();
3093 QDomNodeList props = e.elementsByTagName("property");
3094 QMap <QString, QString> mappedProps;
3095 for (int j = 0; j < props.count(); j++) {
3096 QDomElement f = props.at(j).toElement();
3097 mappedProps.insert(f.attribute("name"), f.firstChild().nodeValue());
3099 if (mappedProps.value("mlt_service") == "mix" && mappedProps.value("b_track").toInt() == tracksCount) {
3100 tractor.removeChild(transitions.at(i));
3102 } else if (mappedProps.value("mlt_service") != "mix" && (mappedProps.value("b_track").toInt() >= ix || mappedProps.value("a_track").toInt() >= ix)) {
3103 // Transition needs to be moved
3104 int a_track = mappedProps.value("a_track").toInt();
3105 int b_track = mappedProps.value("b_track").toInt();
3106 if (a_track > 0 && a_track >= ix) a_track --;
3107 if (b_track > 0 && b_track >= ix) b_track --;
3108 for (int j = 0; j < props.count(); j++) {
3109 QDomElement f = props.at(j).toElement();
3110 if (f.attribute("name") == "a_track") f.firstChild().setNodeValue(QString::number(a_track));
3111 else if (f.attribute("name") == "b_track") f.firstChild().setNodeValue(QString::number(b_track));
3116 tractor.removeChild(track);
3117 //kDebug() << "/////////// RESULT SCENE: \n" << doc.toString();
3118 setSceneList(doc.toString(), m_framePosition);
3123 void Render::updatePreviewSettings()
3125 kDebug() << "////// RESTARTING CONSUMER";
3126 if (!m_mltConsumer || !m_mltProducer) return;
3127 if (m_mltProducer->get_playtime() == 0) return;
3128 Mlt::Service service(m_mltProducer->parent().get_service());
3129 if (service.type() != tractor_type) return;
3131 //m_mltConsumer->set("refresh", 0);
3132 if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
3133 m_mltConsumer->purge();
3134 QString scene = sceneList();
3136 if (m_mltProducer) {
3137 pos = m_mltProducer->position();
3138 delete m_mltProducer;
3140 m_mltProducer = NULL;
3141 setSceneList(scene, pos);
3144 #include "renderer.moc"