]> git.sesse.net Git - kdenlive/blobdiff - src/renderer.cpp
Increase the lookahead to 25 frames.
[kdenlive] / src / renderer.cpp
index 87c03c48be155a3c6565f65c3275e08ab7514bb5..fb61b814e43d2f711e0aadc790a0106f60fa101f 100644 (file)
@@ -22,7 +22,6 @@
  *                                                                         *
  ***************************************************************************/
 
-
 #include "renderer.h"
 #include "kdenlivesettings.h"
 #include "kthumb.h"
 #include <QString>
 #include <QApplication>
 #include <QtConcurrentRun>
+#include <QGLWidget>
 
 #include <cstdlib>
 #include <cstdarg>
 
 #include <QDebug>
+#include <assert.h>
 
 #define SEEK_INACTIVE (-1)
 
@@ -81,6 +82,32 @@ void Render::consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_pt
     }
 }
 
+void Render::consumer_thread_started(mlt_consumer, Render * self, mlt_frame)
+{
+    pthread_t thr = pthread_self();
+    if (self->m_renderThreadGLContexts.count(thr) == 0) {
+        QGLWidget *ctx = new QGLWidget(0, self->m_mainGLContext);
+        ctx->resize(0, 0);
+        self->m_renderThreadGLContexts.insert(thr, ctx);
+    }
+    self->m_renderThreadGLContexts[thr]->makeCurrent();
+    self->m_glslManager->fire_event("init glsl");
+    if (!self->m_glslManager->get_int("glsl_supported")) {
+        QMessageBox::critical(NULL, i18n("Movit failed initialization"),
+                              i18n("Initialization of OpenGL filters failed. Exiting."));
+        qApp->quit();
+    }
+}
+
+void Render::consumer_thread_stopped(mlt_consumer, Render * self, mlt_frame)
+{
+    pthread_t thr = pthread_self();
+    assert(self->m_renderThreadGLContexts.count(thr) != 0);
+    self->m_renderThreadGLContexts[thr]->makeCurrent();
+    delete self->m_renderThreadGLContexts[thr];
+    self->m_renderThreadGLContexts.remove(thr);
+}
+
 /*
 static void consumer_paused(mlt_consumer, Render * self, mlt_frame frame_ptr)
 {
@@ -111,7 +138,7 @@ void Render::consumer_gl_frame_show(mlt_consumer consumer, Render * self, mlt_fr
     emit self->mltFrameReceived(new Mlt::Frame(frame_ptr));
 }
 
-Render::Render(Kdenlive::MONITORID rendererName, int winid, QString profile, QWidget *parent) :
+Render::Render(Kdenlive::MonitorId rendererName, int winid, QString profile, QWidget *parent, QGLWidget *mainGLContext) :
     AbstractRender(rendererName, parent),
     requestedSeekPosition(SEEK_INACTIVE),
     showFrameSemaphore(1),
@@ -121,6 +148,8 @@ Render::Render(Kdenlive::MONITORID rendererName, int winid, QString profile, QWi
     m_mltProducer(NULL),
     m_mltProfile(NULL),
     m_showFrameEvent(NULL),
+    m_consumerThreadStartedEvent(NULL),
+    m_consumerThreadStoppedEvent(NULL),
     m_pauseEvent(NULL),
     m_isZoneMode(false),
     m_isLoopMode(false),
@@ -128,7 +157,9 @@ Render::Render(Kdenlive::MONITORID rendererName, int winid, QString profile, QWi
     m_blackClip(NULL),
     m_winid(winid),
     m_paused(true),
-    m_isActive(false)
+    m_isActive(false),
+    m_mainGLContext(mainGLContext),
+    m_GLContext(NULL)
 {
     qRegisterMetaType<stringMap> ("stringMap");
     analyseAudio = KdenliveSettings::monitor_audio();
@@ -140,16 +171,21 @@ Render::Render(Kdenlive::MONITORID rendererName, int winid, QString profile, QWi
     m_mltProducer->set_speed(0.0);
     m_refreshTimer.setSingleShot(true);
     m_refreshTimer.setInterval(100);
+    m_glslManager = new Mlt::Filter(*m_mltProfile, "glsl.manager");
     connect(&m_refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
     connect(this, SIGNAL(multiStreamFound(QString,QList<int>,QList<int>,stringMap)), this, SLOT(slotMultiStreamProducerFound(QString,QList<int>,QList<int>,stringMap)));
     connect(this, SIGNAL(checkSeeking()), this, SLOT(slotCheckSeeking()));
     connect(this, SIGNAL(mltFrameReceived(Mlt::Frame*)), this, SLOT(showFrame(Mlt::Frame*)), Qt::UniqueConnection);
+
+    m_GLContext = new QGLWidget(0, m_mainGLContext);
+    m_GLContext->resize(0, 0);
 }
 
 Render::~Render()
 {
     closeMlt();
     delete m_mltProfile;
+    delete m_GLContext;
 }
 
 
@@ -159,6 +195,8 @@ void Render::closeMlt()
     m_requestList.clear();
     m_infoThread.waitForFinished();
     delete m_showFrameEvent;
+    delete m_consumerThreadStartedEvent;
+    delete m_consumerThreadStoppedEvent;
     delete m_pauseEvent;
     delete m_mltConsumer;
     delete m_mltProducer;
@@ -227,7 +265,7 @@ void Render::buildConsumer(const QString &profileName)
     m_blackClip = new Mlt::Producer(*m_mltProfile, "colour:black");
     m_blackClip->set("id", "black");
     m_blackClip->set("mlt_type", "producer");
-    if (KdenliveSettings::external_display() && m_name != Kdenlive::clipMonitor && m_winid != 0) {
+    if (KdenliveSettings::external_display() && m_name != Kdenlive::ClipMonitor && m_winid != 0) {
         // Use blackmagic card for video output
         int device = KdenliveSettings::blackmagic_output_device();
         if (device >= 0) {
@@ -242,7 +280,7 @@ void Render::buildConsumer(const QString &profileName)
                 m_mltConsumer->set("terminate_on_pause", 0);
                 m_mltConsumer->set("deinterlace_method", KdenliveSettings::mltdeinterlacer().toUtf8().constData());
                 m_mltConsumer->set("rescale", KdenliveSettings::mltinterpolation().toUtf8().constData());
-                m_mltConsumer->set("buffer", "1");
+                m_mltConsumer->set("buffer", "25");
                 m_mltConsumer->set("real_time", KdenliveSettings::mltthreads());
             }
             if (m_mltConsumer && m_mltConsumer->is_valid()) {
@@ -267,7 +305,7 @@ void Render::buildConsumer(const QString &profileName)
     if (m_winid == 0) {
         // OpenGL monitor
         if (!m_mltConsumer) {
-            if (KdenliveSettings::external_display() && m_name != Kdenlive::clipMonitor) {
+            if (KdenliveSettings::external_display() && m_name != Kdenlive::ClipMonitor) {
                 int device = KdenliveSettings::blackmagic_output_device();
                 if (device >= 0) {
                     QString decklink = "decklink:" + QString::number(KdenliveSettings::blackmagic_output_device());
@@ -285,9 +323,11 @@ void Render::buildConsumer(const QString &profileName)
                 m_mltConsumer->set("scrub_audio", 1);
                 m_mltConsumer->set("preview_off", 1);
                 m_mltConsumer->set("audio_buffer", 512);
-                m_mltConsumer->set("preview_format", mlt_image_rgb24a);
+                m_mltConsumer->set("mlt_image_format", "glsl");
+                m_consumerThreadStartedEvent = m_mltConsumer->listen("consumer-thread-started", this, (mlt_listener) consumer_thread_started);
+                m_consumerThreadStoppedEvent = m_mltConsumer->listen("consumer-thread-stopped", this, (mlt_listener) consumer_thread_stopped);
             }
-            m_mltConsumer->set("buffer", "1");
+            m_mltConsumer->set("buffer", "25");
             m_showFrameEvent = m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_gl_frame_show);
         }
     } else {
@@ -704,6 +744,10 @@ bool Render::isProcessing(const QString &id)
 
 void Render::processFileProperties()
 {
+    // We are in a new thread, so we need a new OpenGL context for the remainder of the function.
+    QGLWidget ctx(0, m_mainGLContext);
+    ctx.makeCurrent();
+
     requestClipInfo info;
     QLocale locale;
     while (!m_requestList.isEmpty()) {
@@ -731,10 +775,10 @@ void Render::processFileProperties()
         }
         KUrl url(path);
         Mlt::Producer *producer = NULL;
-        CLIPTYPE type = (CLIPTYPE)info.xml.attribute("type").toInt();
-        if (type == COLOR) {
+        ClipType type = (ClipType)info.xml.attribute("type").toInt();
+        if (type == Color) {
             producer = new Mlt::Producer(*m_mltProfile, 0, ("colour:" + info.xml.attribute("colour")).toUtf8().constData());
-        } else if (type == TEXT) {
+        } else if (type == Text) {
             producer = new Mlt::Producer(*m_mltProfile, 0, ("kdenlivetitle:" + info.xml.attribute("resource")).toUtf8().constData());
             if (producer && producer->is_valid() && info.xml.hasAttribute("xmldata"))
                 producer->set("xmldata", info.xml.attribute("xmldata").toUtf8().constData());
@@ -837,7 +881,7 @@ void Render::processFileProperties()
         if (info.xml.hasAttribute("out")) clipOut = info.xml.attribute("out").toInt();
 
         // setup length here as otherwise default length (currently 15000 frames in MLT) will be taken even if outpoint is larger
-        if (type == COLOR || type == TEXT || type == IMAGE || type == SLIDESHOW) {
+        if (type == Color || type == Text || type == Image || type == SlideShow) {
             int length;
             if (info.xml.hasAttribute("length")) {
                 length = info.xml.attribute("length").toInt();
@@ -892,7 +936,7 @@ void Render::processFileProperties()
         filePropertyMap["duration"] = QString::number(duration);
         //kDebug() << "///////  PRODUCER: " << url.path() << " IS: " << producer->get_playtime();
 
-        if (type == SLIDESHOW) {
+        if (type == SlideShow) {
             int ttl = info.xml.hasAttribute("ttl") ? info.xml.attribute("ttl").toInt() : 0;
             if (ttl) producer->set("ttl", ttl);
             if (!info.xml.attribute("animation").isEmpty()) {
@@ -1249,6 +1293,10 @@ void Render::startConsumer() {
         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."));
         if (m_showFrameEvent) delete m_showFrameEvent;
         m_showFrameEvent = NULL;
+        if (m_consumerThreadStartedEvent) delete m_consumerThreadStartedEvent;
+        m_consumerThreadStartedEvent = NULL;
+        if (m_consumerThreadStoppedEvent) delete m_consumerThreadStoppedEvent;
+        m_consumerThreadStoppedEvent = NULL;
         if (m_pauseEvent) delete m_pauseEvent;
         m_pauseEvent = NULL;
         delete m_mltConsumer;
@@ -1413,6 +1461,7 @@ void Render::checkMaxThreads()
 
 const QString Render::sceneList()
 {
+    if (!m_mltProducer) return QString();
     QString playlist;
     Mlt::Profile profile((mlt_profile) 0);
     Mlt::Consumer xmlConsumer(profile, "xml:kdenlive_playlist");
@@ -1459,7 +1508,7 @@ void Render::saveZone(KUrl url, QString desc, QPoint zone)
     Mlt::Consumer xmlConsumer(*m_mltProfile, ("xml:" + url.path()).toUtf8().constData());
     m_mltProducer->optimise();
     xmlConsumer.set("terminate_on_pause", 1);
-    if (m_name == Kdenlive::clipMonitor) {
+    if (m_name == Kdenlive::ClipMonitor) {
         Mlt::Producer *prod = m_mltProducer->cut(zone.x(), zone.y());
         Mlt::Playlist list;
         list.insert_at(0, prod, 0);
@@ -1632,7 +1681,7 @@ void Render::switchPlay(bool play)
         return;
     if (m_isZoneMode) resetZoneMode();
     if (play && m_paused) {
-        if (m_name == Kdenlive::clipMonitor && m_mltConsumer->position() == m_mltProducer->get_out()) m_mltProducer->seek(0);
+        if (m_name == Kdenlive::ClipMonitor && m_mltConsumer->position() == m_mltProducer->get_out()) m_mltProducer->seek(0);
         m_paused = false;
         m_mltProducer->set_speed(1.0);
         if (m_mltConsumer->is_stopped()) {
@@ -1813,12 +1862,12 @@ int Render::seekFramePosition() const
 
 void Render::emitFrameUpdated(Mlt::Frame& frame)
 {
-    mlt_image_format format = mlt_image_rgb24;
+    mlt_image_format format = mlt_image_rgb24a;
     int width = 0;
     int height = 0;
     const uchar* image = frame.get_image(format, width, height);
-    QImage qimage(width, height, QImage::Format_RGB888);  //Format_ARGB32_Premultiplied);
-    memcpy(qimage.scanLine(0), image, width * height * 3);
+    QImage qimage(width, height, QImage::Format_ARGB32_Premultiplied);
+    memcpy(qimage.scanLine(0), image, width * height * 4);
     emit frameUpdated(qimage);
 }
 
@@ -1896,18 +1945,25 @@ void Render::showFrame(Mlt::Frame* frame)
     if (currentPos == requestedSeekPosition) requestedSeekPosition = SEEK_INACTIVE;
     emit rendererPosition(currentPos);
     if (frame->is_valid()) {
-        mlt_image_format format = mlt_image_rgb24;
+        mlt_image_format format = mlt_image_glsl_texture;
         int width = 0;
         int height = 0;
-        const uchar* image = frame->get_image(format, width, height);
-        QImage qimage(width, height, QImage::Format_RGB888); //Format_ARGB32_Premultiplied);
-        memcpy(qimage.scanLine(0), image, width * height * 3);
-        if (analyseAudio) showAudio(*frame);
-        delete frame;
-        emit showImageSignal(qimage);
-        if (sendFrameForAnalysis) {
-            emit frameUpdated(qimage);
-        }
+        m_GLContext->makeCurrent();
+        frame->set("movit.convert.use_texture", 1);
+        const uint8_t* image = frame->get_image(format, width, height);
+        const GLuint* texnum = (GLuint *)image;
+        if (format == mlt_image_glsl_texture) {
+          emit showImageSignal(frame, *texnum);
+        } else {
+          QImage qimage(width, height, QImage::Format_ARGB32_Premultiplied);
+          memcpy(qimage.scanLine(0), image, width * height * 4);
+          if (analyseAudio) showAudio(*frame);
+          delete frame;
+          emit showImageSignal(qimage);
+          if (sendFrameForAnalysis) {
+              emit frameUpdated(qimage);
+          }
+       }
     } else delete frame;
     showFrameSemaphore.release();
     emit checkSeeking();
@@ -2907,8 +2963,8 @@ bool Render::addFilterToService(Mlt::Service service, EffectsParameterList param
             Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, qstrdup(tag.toUtf8().constData()));
             if (filter && filter->is_valid()) {
                 filter->set("kdenlive_id", qstrdup(params.paramValue("id").toUtf8().constData()));
-                int x1 = keyFrames.at(0).section(':', 0, 0).toInt();
-                double y1 = keyFrames.at(0).section(':', 1, 1).toDouble();
+                int x1 = keyFrames.at(0).section('=', 0, 0).toInt();
+                double y1 = keyFrames.at(0).section('=', 1, 1).toDouble();
                 for (int j = 0; j < params.count(); j++) {
                     filter->set(params.at(j).name().toUtf8().constData(), params.at(j).value().toUtf8().constData());
                 }
@@ -2927,10 +2983,10 @@ bool Render::addFilterToService(Mlt::Service service, EffectsParameterList param
             Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, qstrdup(tag.toUtf8().constData()));
             if (filter && filter->is_valid()) {
                 filter->set("kdenlive_id", qstrdup(params.paramValue("id").toUtf8().constData()));
-                int x1 = keyFrames.at(i).section(':', 0, 0).toInt() + offset;
-                double y1 = keyFrames.at(i).section(':', 1, 1).toDouble();
-                int x2 = keyFrames.at(i + 1).section(':', 0, 0).toInt();
-                double y2 = keyFrames.at(i + 1).section(':', 1, 1).toDouble();
+                int x1 = keyFrames.at(i).section('=', 0, 0).toInt() + offset;
+                double y1 = keyFrames.at(i).section('=', 1, 1).toDouble();
+                int x2 = keyFrames.at(i + 1).section('=', 0, 0).toInt();
+                double y2 = keyFrames.at(i + 1).section('=', 1, 1).toDouble();
                 if (x2 == -1) x2 = duration;
 
                 for (int j = 0; j < params.count(); j++) {
@@ -3170,6 +3226,7 @@ bool Render::mltEnableEffects(int track, const GenTime &position, const QList <i
     int ct = 0;
 
     Mlt::Filter *filter = clip->filter(ct);
+    service.lock();
     while (filter) {
         if (effectIndexes.contains(filter->get_int("kdenlive_ix"))) {
             filter->set("disable", (int) disable);
@@ -3195,6 +3252,7 @@ bool Render::mltEnableTrackEffects(int track, const QList <int> &effectIndexes,
     int ct = 0;
 
     Mlt::Filter *filter = clipService.filter(ct);
+    service.lock();
     while (filter) {
         if (effectIndexes.contains(filter->get_int("kdenlive_ix"))) {
             filter->set("disable", (int) disable);