]> git.sesse.net Git - kdenlive/commitdiff
Make sure we only use one thread to create video thumbnails
authorJean-Baptiste Mardelle <jb@kdenlive.org>
Fri, 28 Oct 2011 21:50:21 +0000 (21:50 +0000)
committerJean-Baptiste Mardelle <jb@kdenlive.org>
Fri, 28 Oct 2011 21:50:21 +0000 (21:50 +0000)
svn path=/trunk/kdenlive/; revision=5997

src/clipitem.cpp
src/clipmanager.cpp
src/clipmanager.h
src/docclipbase.cpp
src/docclipbase.h
src/kthumb.cpp
src/kthumb.h
src/projectlist.cpp

index 5f4cfb0269eb8030e05426f805f1de2a43d0c8de..9f6f04209f962c5c530e1019b16f2c42b75f7bcb 100644 (file)
@@ -517,7 +517,6 @@ void ClipItem::resetThumbs(bool clearExistingThumbs)
         m_endPix = QPixmap();
         m_audioThumbCachePic.clear();
     }
-    //kDebug()<<"...........  RESET THMBS: "<<clearExistingThumbs;
     slotFetchThumbs();
 }
 
@@ -563,30 +562,30 @@ void ClipItem::slotFetchThumbs()
         return;
     }
 
-    if (m_endPix.isNull() && m_startPix.isNull()) {
+    QList <int> frames;
+    if (m_startPix.isNull()) {
         m_startThumbRequested = true;
+        frames.append((int)m_speedIndependantInfo.cropStart.frames(m_fps));
+    }
+
+    if (m_endPix.isNull()) {
         m_endThumbRequested = true;
-        m_clip->slotExtractImage((int)m_speedIndependantInfo.cropStart.frames(m_fps), (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
-    } else {
-        if (m_endPix.isNull()) {
-            slotGetEndThumb();
-        }
-        if (m_startPix.isNull()) {
-            slotGetStartThumb();
-        }
+        frames.append((int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
     }
+
+    m_clip->slotExtractImage(frames);
 }
 
 void ClipItem::slotGetStartThumb()
 {
     m_startThumbRequested = true;
-    m_clip->slotExtractImage((int)m_speedIndependantInfo.cropStart.frames(m_fps), -1);
+    m_clip->slotExtractImage(QList<int>() << (int)m_speedIndependantInfo.cropStart.frames(m_fps));
 }
 
 void ClipItem::slotGetEndThumb()
 {
     m_endThumbRequested = true;
-    m_clip->slotExtractImage(-1, (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
+    m_clip->slotExtractImage(QList<int>() << (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
 }
 
 
index 0710fce51dcc85da8bbbfeadd4ddfa70a3acb5d2..41df4c839f25b134488875227daf2a2b9d29ea57 100644 (file)
@@ -26,6 +26,7 @@
 #include "abstractclipitem.h"
 #include "abstractgroupitem.h"
 #include "titledocument.h"
+#include "kthumb.h"
 
 #include <mlt++/Mlt.h>
 
@@ -36,6 +37,7 @@
 #include <kio/netaccess.h>
 
 #include <QGraphicsItemGroup>
+#include <QtConcurrentRun>
 
 #include <KFileMetaInfo>
 
@@ -43,7 +45,8 @@ ClipManager::ClipManager(KdenliveDoc *doc) :
     QObject(),
     m_audioThumbsQueue(),
     m_doc(doc),
-    m_generatingAudioId()
+    m_generatingAudioId(),
+    m_abortThumb(false)
 {
     m_clipIdCounter = 1;
     m_folderIdCounter = 1;
@@ -62,8 +65,16 @@ ClipManager::ClipManager(KdenliveDoc *doc) :
 
 ClipManager::~ClipManager()
 {
+    m_thumbsMutex.lock();
+    m_requestedThumbs.clear();
+    m_thumbsMutex.unlock();
+    m_abortThumb = true;
+    m_thumbsThread.waitForFinished();
     m_audioThumbsQueue.clear();
     m_generatingAudioId.clear();
+    m_thumbsMutex.lock();
+    m_requestedThumbs.clear();
+    m_thumbsMutex.unlock();
     qDeleteAll(m_clipList);
     m_clipList.clear();
 #if KDE_IS_VERSION(4,5,0)
@@ -73,6 +84,12 @@ ClipManager::~ClipManager()
 
 void ClipManager::clear()
 {
+    m_thumbsMutex.lock();
+    m_requestedThumbs.clear();
+    m_thumbsMutex.unlock();
+    m_abortThumb = true;
+    m_thumbsThread.waitForFinished();
+    m_abortThumb = false;
     m_folderList.clear();
     m_audioThumbsQueue.clear();
     m_modifiedClips.clear();
@@ -92,6 +109,58 @@ void ClipManager::clearCache()
 #endif
 }
 
+void ClipManager::requestThumbs(const QString id, QList <int> frames)
+{
+    kDebug()<<"// Request thbs: "<<id<<": "<<frames;
+    m_thumbsMutex.lock();
+    foreach (int frame, frames) {
+        m_requestedThumbs.insertMulti(id, frame);
+    }
+    m_thumbsMutex.unlock();
+    if (!m_thumbsThread.isRunning() && !m_abortThumb) {
+        m_thumbsThread = QtConcurrent::run(this, &ClipManager::slotGetThumbs);
+    }
+}
+
+void ClipManager::stopThumbs(const QString &id)
+{
+    m_abortThumb = true;
+    m_thumbsThread.waitForFinished();
+    m_thumbsMutex.lock();
+    m_requestedThumbs.remove(id);
+    m_thumbsMutex.unlock();
+    m_abortThumb = false;
+    if (!m_thumbsThread.isRunning()) {
+        m_thumbsThread = QtConcurrent::run(this, &ClipManager::slotGetThumbs);
+    }
+}
+
+void ClipManager::slotGetThumbs()
+{
+    QMap<QString, int>::const_iterator i = m_requestedThumbs.constBegin();
+    while (i != m_requestedThumbs.constEnd() && !m_abortThumb) {
+        const QString producerId = i.key();
+        m_thumbsMutex.lock();
+        QList<int> values = m_requestedThumbs.values(producerId);
+        m_requestedThumbs.remove(producerId);
+        m_thumbsMutex.unlock();
+        qSort(values);
+        DocClipBase *clip = getClipById(producerId);
+        if (!clip) continue;
+        while (!values.isEmpty() && clip->thumbProducer() && !m_abortThumb) {
+            clip->thumbProducer()->getThumb(values.takeFirst());
+        }
+        if (m_abortThumb) {
+            // keep the requested frames that were not processed
+            m_thumbsMutex.lock();
+            foreach (int frame, values)
+                m_requestedThumbs.insertMulti(producerId, frame);
+            m_thumbsMutex.unlock();
+        }
+        i = m_requestedThumbs.constBegin();
+    }
+}
+
 void ClipManager::checkAudioThumbs()
 {
     if (!KdenliveSettings::audiothumbnails()) {
index ad01a17c41c6b474a047e6a5eef9b01fdf2a89fb..9535c7744a62d822ca1c8d2c649562745218875c 100644 (file)
@@ -30,6 +30,8 @@
 #include <QPixmap>
 #include <QObject>
 #include <QTimer>
+#include <QMutex>
+#include <QFuture>
 
 #include <KUrl>
 #include <KUndoStack>
@@ -110,6 +112,10 @@ Q_OBJECT public:
     void removeGroup(AbstractGroupItem *group);
     QDomElement groupsXml() const;
     int clipsCount() const;
+    /** @brief Request creation of a clip thumbnail for specified frames. */
+    void requestThumbs(const QString id, QList <int> frames);
+    /** @brief remove a clip id from the queue list. */
+    void stopThumbs(const QString &id);
 
 #if KDE_IS_VERSION(4,5,0)
     KImageCache* pixmapCache;
@@ -122,6 +128,7 @@ private slots:
     void slotClipAvailable(const QString &path);
     /** Check the list of externally modified clips, and process them if they were not modified in the last 1500 milliseconds */
     void slotProcessModifiedClips();
+    void slotGetThumbs();
 
 private:   // Private attributes
     /** the list of clips in the document */
@@ -140,6 +147,12 @@ private:   // Private attributes
     QTimer m_modifiedTimer;
     /** List of the clip IDs that need to be reloaded after being externally modified */
     QMap <QString, QTime> m_modifiedClips;
+    /** Struct containing the list of clip thumbnails to request (clip id and frames) */
+    QMap <QString, int> m_requestedThumbs;
+    QMutex m_thumbsMutex;
+    QFuture<void> m_thumbsThread;
+    /** @brief If true, abort processing of clip thumbs before removing a clip. */
+    bool m_abortThumb;
 
 signals:
     void reloadClip(const QString &);
index adeb59c1fa4284fe14f1d921f8bfd57c7eb36421..8876587221180cf0cb7003eb8c627057123c0f39 100644 (file)
@@ -1207,10 +1207,10 @@ bool DocClipBase::hasAudioCodec(const QString &codec) const
 }
 
 
-void DocClipBase::slotExtractImage(int frame, int frame2)
+void DocClipBase::slotExtractImage(QList <int> frames)
 {
     if (m_thumbProd == NULL) return;
-    m_thumbProd->extractImage(frame, frame2);
+    m_thumbProd->extractImage(frames);
 }
 
 QPixmap DocClipBase::extractImage(int frame, int width, int height)
index 337a137330dab5cd69de9316130f19eb1c682e35..31a9a9825525d219007f8ebd0819d769c4e6c340 100644 (file)
@@ -279,7 +279,7 @@ public slots:
     void setMetadata(QMap <QString, QString> properties);
     QMap <QString, QString> properties() const;
     QMap <QString, QString> metadata() const;
-    void slotExtractImage(int frame, int frame2);
+    void slotExtractImage(QList <int> frames);
 
 signals:
     void gotAudioData();
index 6eb212a7d358eac233f2e889edbb4a1037596601..2ea15906c618f4022be6bb175061b0fd6237137d 100644 (file)
@@ -57,26 +57,20 @@ KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QStr
 
 KThumb::~KThumb()
 {
-    m_listMutex.lock();
-    m_requestedThumbs.clear();
-    m_listMutex.unlock();
+    m_clipManager->stopThumbs(m_id);
     m_intraFramesQueue.clear();
     if (m_audioThumbProducer.isRunning()) {
         m_stopAudioThumbs = true;
         m_audioThumbProducer.waitForFinished();
         slotAudioThumbOver();
     }
-    m_future.waitForFinished();
     m_intra.waitForFinished();
 }
 
 void KThumb::setProducer(Mlt::Producer *producer)
 {
-    m_listMutex.lock();
-    m_requestedThumbs.clear();
-    m_listMutex.unlock();
+    m_clipManager->stopThumbs(m_id);
     m_intraFramesQueue.clear();
-    m_future.waitForFinished();
     m_intra.waitForFinished();
     m_mutex.lock();
     m_producer = producer;
@@ -117,34 +111,21 @@ QPixmap KThumb::getImage(KUrl url, int width, int height)
     return getImage(url, 0, width, height);
 }
 
-void KThumb::extractImage(int frame, int frame2)
+void KThumb::extractImage(QList <int>frames)
 {
     if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
-    m_listMutex.lock();
-    if (frame != -1 && !m_requestedThumbs.contains(frame)) m_requestedThumbs.append(frame);
-    if (frame2 != -1 && !m_requestedThumbs.contains(frame2)) m_requestedThumbs.append(frame2);
-    qSort(m_requestedThumbs);
-    m_listMutex.unlock();
-    if (!m_future.isRunning()) {
-        m_future = QtConcurrent::run(this, &KThumb::doGetThumbs);
-    }
+    m_clipManager->requestThumbs(m_id, frames);
 }
 
-void KThumb::doGetThumbs()
+
+void KThumb::getThumb(int frame)
 {
     const int theight = KdenliveSettings::trackheight();
     const int swidth = (int)(theight * m_ratio + 0.5);
     const int dwidth = (int)(theight * m_dar + 0.5);
 
-    while (!m_requestedThumbs.isEmpty()) {
-        m_listMutex.lock();
-        int frame = m_requestedThumbs.takeFirst();
-        m_listMutex.unlock();
-        if (frame != -1) {
-            QImage img = getProducerFrame(frame, swidth, dwidth, theight);
-            emit thumbReady(frame, img);
-        }
-    }
+    QImage img = getProducerFrame(frame, swidth, dwidth, theight);
+    emit thumbReady(frame, img);
 }
 
 QPixmap KThumb::extractImage(int frame, int width, int height)
@@ -230,9 +211,13 @@ QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int
     int ow = frameWidth;
     int oh = height;
     mlt_image_format format = mlt_image_rgb24a;
-    
+
     const uchar* imagedata = frame->get_image(format, ow, oh);
-    QImage image(imagedata, ow, oh, QImage::Format_ARGB32_Premultiplied);
+    QImage image(ow, oh, QImage::Format_ARGB32_Premultiplied);
+    memcpy(image.bits(), imagedata, ow * oh * 4);//.byteCount());
+    
+    //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)) {
@@ -430,8 +415,8 @@ void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int a
 
 void KThumb::slotCreateAudioThumbs()
 {
-    Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
-    Mlt::Producer producer(prof, m_url.path().toUtf8().data());
+    Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().constData());
+    Mlt::Producer producer(prof, m_url.path().toUtf8().constData());
     if (!producer.is_valid()) {
         kDebug() << "++++++++  INVALID CLIP: " << m_url.path();
         return;
@@ -451,7 +436,10 @@ void KThumb::slotCreateAudioThumbs()
 
     int last_val = 0;
     int val = 0;
-    //kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
+    mlt_audio_format m_audioFormat = mlt_audio_pcm;
+    double framesPerSecond = mlt_producer_get_fps(producer.get_producer());
+    Mlt::Frame *mlt_frame;
+    
     for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && producer.is_valid(); z++) {
         if (m_stopAudioThumbs) break;
         val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
@@ -460,13 +448,10 @@ void KThumb::slotCreateAudioThumbs()
             last_val = val;
         }
         producer.seek(z);
-        Mlt::Frame *mlt_frame = producer.get_frame();
+        mlt_frame = producer.get_frame();
         if (mlt_frame && mlt_frame->is_valid()) {
-            double m_framesPerSecond = mlt_producer_get_fps(producer.get_producer());
-            int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
-            mlt_audio_format m_audioFormat = mlt_audio_pcm;
+            int m_samples = mlt_sample_calculator(framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
             qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
-
             for (int c = 0; c < m_channels; c++) {
                 QByteArray m_array;
                 m_array.resize(m_arrayWidth);
index f90051d7b43a8ea81ac91397fa7c325f41c38b70..7dbc5c986e851c8e14fe4f4db6920a129a6f78ae 100644 (file)
@@ -63,7 +63,7 @@ Q_OBJECT public:
     bool hasProducer() const;
     void clearProducer();
     void updateThumbUrl(const QString &hash);
-    void extractImage(int frame, int frame2);
+    void extractImage(QList <int> frames);
     QPixmap extractImage(int frame, int width, int height);
 #if KDE_IS_VERSION(4,5,0)
     /** @brief Request thumbnails for the frame range. */
@@ -71,6 +71,7 @@ Q_OBJECT public:
     /** @brief Query cached thumbnail. */
     QImage findCachedThumb(const QString path);
 #endif
+    void getThumb(int frame);
 
 public slots:
     void updateClipUrl(KUrl url, const QString &hash);
@@ -106,9 +107,6 @@ private:
     Mlt::Producer *m_producer;
     ClipManager *m_clipManager;
     QString m_id;
-    QList <int> m_requestedThumbs;
-    /** @brief Controls the thumbnails process. */
-    QFuture<void> m_future;
     /** @brief Controls the intra frames thumbnails process (cached thumbnails). */
     QFuture<void> m_intra;
     QFile m_audioThumbFile;
@@ -121,8 +119,6 @@ private:
     /** @brief List of frame numbers from which we want to extract thumbnails. */
     QList <int> m_intraFramesQueue;
     QMutex m_mutex;
-    QMutex m_listMutex;
-    void doGetThumbs();
     QImage getProducerFrame(int framepos, int frameWidth, int displayWidth, int height);
 
 signals:
index 29250d8b2f0ae02462ec8250f93847bc99a60f45..eb17ca2e4e7ddf726ba32d07e27b5acb67cef2f0 100644 (file)
@@ -1646,7 +1646,6 @@ QDomElement ProjectList::producersList()
     QDomDocument doc;
     QDomElement prods = doc.createElement("producerlist");
     doc.appendChild(prods);
-    kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
     QTreeWidgetItemIterator it(m_listView);
     while (*it) {
         if ((*it)->type() != PROJECTCLIPTYPE) {
@@ -1664,10 +1663,9 @@ void ProjectList::slotCheckForEmptyQueue()
 {
     if (m_render->processingItems() == 0 && m_thumbnailQueue.isEmpty()) {
         if (!m_refreshed && m_allClipsProcessed) {
-            kDebug()<<"// LOADING IS OVER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
             m_listView->setEnabled(true);
             slotClipSelected();
-            emit loadingIsOver();
+            QTimer::singleShot(500, this, SIGNAL(loadingIsOver()));
             emit displayMessage(QString(), -1);
             m_refreshed = true;
         }