]> git.sesse.net Git - kdenlive/blobdiff - src/kthumb.cpp
Try to fix the concurrency issues causing crash in the avformat producer
[kdenlive] / src / kthumb.cpp
index b26b5f1ae4603744959c3dd649e1cc3e6e28d30d..71f2bfc96dc37d5a20d75f9a2110c28ee7377ff6 100644 (file)
@@ -74,6 +74,7 @@ void KThumb::setProducer(Mlt::Producer *producer)
     m_intraFramesQueue.clear();
     m_future.waitForFinished();
     m_intra.waitForFinished();
+    m_mutex.lock();
     m_producer = producer;
     // FIXME: the profile() call leaks an object, but trying to free
     // it leads to a double-free in Profile::~Profile()
@@ -81,7 +82,7 @@ void KThumb::setProducer(Mlt::Producer *producer)
         m_dar = producer->profile()->dar();
         m_ratio = (double) producer->profile()->width() / producer->profile()->height();
     }
-        
+    m_mutex.unlock();
 }
 
 void KThumb::clearProducer()
@@ -102,8 +103,6 @@ void KThumb::updateThumbUrl(const QString &hash)
 void KThumb::updateClipUrl(KUrl url, const QString &hash)
 {
     m_url = url;
-    //if (m_producer)
-        //m_producer->set("resource", url.path().toUtf8().constData());
     m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
 }
 
@@ -119,7 +118,10 @@ void KThumb::extractImage(int frame, int frame2)
     if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
     if (frame != -1 && !m_requestedThumbs.contains(frame)) m_requestedThumbs.append(frame);
     if (frame2 != -1 && !m_requestedThumbs.contains(frame2)) m_requestedThumbs.append(frame2);
-    if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &KThumb::doGetThumbs);
+    qSort(m_requestedThumbs);
+    if (!m_future.isRunning()) {
+        m_future = QtConcurrent::run(this, &KThumb::doGetThumbs);
+    }
 }
 
 void KThumb::doGetThumbs()
@@ -131,7 +133,7 @@ void KThumb::doGetThumbs()
     while (!m_requestedThumbs.isEmpty()) {
         int frame = m_requestedThumbs.takeFirst();
         if (frame != -1) {
-            QImage img = getFrame(m_producer, frame, swidth, dwidth, theight);
+            QImage img = getProducerFrame(frame, swidth, dwidth, theight);
             emit thumbReady(frame, img);
         }
     }
@@ -139,7 +141,13 @@ void KThumb::doGetThumbs()
 
 QPixmap KThumb::extractImage(int frame, int width, int height)
 {
-    return QPixmap::fromImage(getFrame(m_producer, frame, (int) (height * m_ratio + 0.5), width, height));
+    if (m_producer == NULL) {
+        QPixmap p(width, height);
+        p.fill(Qt::black);
+        return p;
+    }
+    QImage img = getProducerFrame(frame, (int) (height * m_ratio + 0.5), width, height);
+    return QPixmap::fromImage(img);
 }
 
 //static
@@ -158,23 +166,45 @@ QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
     return pix;
 }
 
+
+QImage KThumb::getProducerFrame(int framepos, int frameWidth, int displayWidth, int height)
+{
+    if (m_producer == NULL || !m_producer->is_valid()) {
+        QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
+        p.fill(QColor(Qt::red).rgb());
+        return p;
+    }
+    if (m_producer->is_blank()) {
+        QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
+        p.fill(QColor(Qt::black).rgb());
+        return p;
+    }
+    m_mutex.lock();
+    m_producer->seek(framepos);
+    Mlt::Frame *frame = m_producer->get_frame();
+    QImage p = getFrame(frame, frameWidth, displayWidth, height);
+    delete frame;
+    m_mutex.unlock();
+    return p;
+}
+
 //static
 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int frameWidth, int displayWidth, int height)
 {
-    QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
     if (producer == NULL || !producer->is_valid()) {
-        p.fill(Qt::red);
+        QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
+        p.fill(QColor(Qt::red).rgb());
         return p;
     }
-
     if (producer->is_blank()) {
-        p.fill(Qt::black);
+        QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
+        p.fill(QColor(Qt::black).rgb());
         return p;
     }
 
     producer->seek(framepos);
     Mlt::Frame *frame = producer->get_frame();
-    p = getFrame(frame, frameWidth, displayWidth, height);
+    QImage p = getFrame(frame, frameWidth, displayWidth, height);
     delete frame;
     return p;
 }
@@ -185,30 +215,30 @@ QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int
 {
     QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
     if (frame == NULL || !frame->is_valid()) {
-        p.fill(Qt::red);
+        p.fill(QColor(Qt::red).rgb());
         return p;
     }
 
     int ow = frameWidth;
     int oh = height;
     mlt_image_format format = mlt_image_rgb24a;
-    uint8_t *data = frame->get_image(format, ow, oh, 0);
-    QImage image((uchar *)data, ow, oh, QImage::Format_ARGB32_Premultiplied);
+    
+    const uchar* imagedata = frame->get_image(format, ow, oh);
+    QImage image(imagedata, ow, oh, QImage::Format_ARGB32_Premultiplied);
+    
     if (!image.isNull()) {
         if (ow > (2 * displayWidth)) {
             // there was a scaling problem, do it manually
-            QImage scaled = image.scaled(displayWidth, height);
-            image = scaled.rgbSwapped();
+            image = image.scaled(displayWidth, height).rgbSwapped();
         } else {
             image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
         }
+        p.fill(QColor(Qt::black).rgb());
         QPainter painter(&p);
-        painter.fillRect(p.rect(), Qt::black);
-        painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
         painter.drawImage(p.rect(), image);
         painter.end();
     } else
-        p.fill(Qt::red);
+        p.fill(QColor(Qt::red).rgb());
     return p;
 }
 
@@ -342,7 +372,7 @@ void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int a
         return;
     }
 
-    QMap <int, QMap <int, QByteArray> > storeIn;
+    audioByteArray storeIn;
     //FIXME: Hardcoded!!!
     m_frequency = 48000;
     m_channels = channel;
@@ -469,7 +499,9 @@ void KThumb::queryIntraThumbs(QList <int> missingFrames)
         if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
     }
     qSort(m_intraFramesQueue);
-    if (!m_intra.isRunning()) m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
+    if (!m_intra.isRunning()) {
+        m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
+    }
 }
 
 void KThumb::slotGetIntraThumbs()
@@ -483,7 +515,7 @@ void KThumb::slotGetIntraThumbs()
     while (!m_intraFramesQueue.isEmpty()) {
         int pos = m_intraFramesQueue.takeFirst();
         if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
-            if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getFrame(m_producer, pos, frameWidth, displayWidth, theight))) {
+            if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getProducerFrame(pos, frameWidth, displayWidth, theight))) {
                 addedThumbs = true;
             }
             else kDebug()<<"// INSERT FAILD FOR: "<<pos;