]> git.sesse.net Git - kdenlive/blobdiff - src/kthumb.cpp
Use QtConcurrent instead of custom thread to create audio thumbs, in an attempt to...
[kdenlive] / src / kthumb.cpp
index e67425d2321081fa96a5aeed5776d7bbaa7de0b1..46e4acc5769d63293c988fce953384db507ce300 100644 (file)
@@ -118,26 +118,27 @@ void MyThread::run()
 
 
 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/) :
-        QObject(parent),
-        m_audioThumbProducer(),
-        m_url(url),
-        m_thumbFile(),
-        m_dar(1),
-        m_producer(NULL),
-        m_clipManager(clipManager),
-        m_id(id)
+    QObject(parent),
+    m_audioThumbProducer(),
+    m_url(url),
+    m_thumbFile(),
+    m_dar(1),
+    m_producer(NULL),
+    m_clipManager(clipManager),
+    m_id(id),
+    m_stopAudioThumbs(false)
 {
     m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
-    connect(&m_audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
-    connect(&m_audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));
+    /*connect(&m_audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
+    connect(&m_audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));*/
 }
 
 KThumb::~KThumb()
 {
     m_requestedThumbs.clear();
     if (m_audioThumbProducer.isRunning()) {
-        m_audioThumbProducer.stop_me = true;
-        m_audioThumbProducer.wait();
+        m_stopAudioThumbs = true;
+        m_audioThumbProducer.waitForFinished();
         slotAudioThumbOver();
     }
     m_future.waitForFinished();
@@ -171,11 +172,8 @@ void KThumb::updateThumbUrl(const QString &hash)
 void KThumb::updateClipUrl(KUrl url, const QString &hash)
 {
     m_url = url;
-    if (m_producer) {
-        char *tmp = Render::decodedString(url.path());
-        m_producer->set("resource", tmp);
-        delete[] tmp;
-    }
+    if (m_producer)
+        m_producer->set("resource", url.path().toUtf8().constData());
     m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
 }
 
@@ -216,17 +214,13 @@ QPixmap KThumb::extractImage(int frame, int width, int height)
 //static
 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
 {
-    char *tmp = Render::decodedString(KdenliveSettings::current_profile());
-    Mlt::Profile profile(tmp);
-    delete[] tmp;
+    Mlt::Profile profile(KdenliveSettings::current_profile().toUtf8().constData());
     QPixmap pix(width, height);
     if (url.isEmpty()) return pix;
 
-    tmp = Render::decodedString(url.path());
     //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
     //Mlt::Producer producer(profile, "xml-string", tmp);
-    Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
-    delete[] tmp;
+    Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
 
     pix = QPixmap::fromImage(getFrame(producer, frame, width, height));
     delete producer;
@@ -237,7 +231,7 @@ QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
 //static
 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
 {
-    QImage p(width, height, QImage::Format_ARGB32);
+    QImage p(width, height, QImage::Format_ARGB32_Premultiplied);
     if (producer == NULL) {
         p.fill(Qt::red);
         return p;
@@ -263,7 +257,7 @@ QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int he
     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);
+    QImage image((uchar *)data, ow, oh, QImage::Format_ARGB32_Premultiplied);
     //mlt_service_unlock(service.get_service());
 
     if (!image.isNull()) {
@@ -283,9 +277,7 @@ void KThumb::getImage(KUrl url, int frame, int width, int height)
 {
     if (url.isEmpty()) return;
     QPixmap image(width, height);
-    char *tmp = KRender::decodedString(url.path());
-    Mlt::Producer m_producer(tmp);
-    delete tmp;
+    Mlt::Producer m_producer(url.path().toUtf8().constData());
     image.fill(Qt::black);
 
     if (m_producer.is_blank()) {
@@ -313,9 +305,7 @@ void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int he
 {
     if (url.isEmpty()) return;
     QPixmap image(width, height);
-    char *tmp = KRender::decodedString(url.path());
-    Mlt::Producer m_producer(tmp);
-    delete tmp;
+    Mlt::Producer m_producer(url.path().toUtf8().constData());
     image.fill(Qt::black);
 
     if (m_producer.is_blank()) {
@@ -356,7 +346,8 @@ void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int he
 void KThumb::stopAudioThumbs()
 {
     if (m_audioThumbProducer.isRunning()) {
-        m_audioThumbProducer.stop_me = true;
+        m_stopAudioThumbs = true;
+        m_audioThumbProducer.waitForFinished();
         slotAudioThumbOver();
     }
 }
@@ -375,14 +366,14 @@ void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int a
         slotAudioThumbOver();
         return;
     }
-    if ((m_audioThumbProducer.isRunning() && m_audioThumbProducer.isWorking())) {
+    if (m_audioThumbProducer.isRunning()) {
         return;
     }
 
     QMap <int, QMap <int, QByteArray> > storeIn;
     //FIXME: Hardcoded!!!
-    int m_frequency = 48000;
-    int m_channels = channel;
+    m_frequency = 48000;
+    m_channels = channel;
 
     QFile f(m_thumbFile);
     if (f.open(QIODevice::ReadOnly)) {
@@ -394,12 +385,20 @@ void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int a
             slotAudioThumbOver();
             return;
         }
+
         kDebug() << "reading audio thumbs from file";
+
+        int h1 = arrayWidth * m_channels;
+        int h2 = (int) frame * h1;
+        int h3;
         for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
+            h2 += h1;
+            h3 = 0;
             for (int c = 0; c < m_channels; c++) {
+                h3 += arrayWidth;
                 QByteArray m_array(arrayWidth, '\x00');
                 for (int i = 0; i < arrayWidth; i++)
-                    m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
+                    m_array[i] = channelarray[h2 + h3 + i];
                 storeIn[z][c] = m_array;
             }
         }
@@ -407,12 +406,74 @@ void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int a
         slotAudioThumbOver();
     } else {
         if (m_audioThumbProducer.isRunning()) return;
-        m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
-        m_audioThumbProducer.start(QThread::LowestPriority);
+        m_audioThumbFile.setFileName(m_thumbFile);
+        m_frame = frame;
+        m_frameLength = frameLength;
+        m_arrayWidth = arrayWidth;
+        m_audioThumbProducer = QtConcurrent::run(this, &KThumb::slotCreateAudioThumbs);
+        /*m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
+        m_audioThumbProducer.start(QThread::LowestPriority);*/
         // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
     }
 }
 
+void KThumb::slotCreateAudioThumbs()
+{
+    if (!m_audioThumbFile.open(QIODevice::WriteOnly)) {
+        kDebug() << "++++++++  ERROR WRITING TO FILE: " << m_audioThumbFile.fileName();
+        kDebug() << "++++++++  DISABLING AUDIO THUMBS";
+        KdenliveSettings::setAudiothumbnails(false);
+        return;
+    }
+    Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
+    Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
+
+    if (KdenliveSettings::normaliseaudiothumbs()) {
+        Mlt::Filter m_convert(prof, "volume");
+        m_convert.set("gain", "normalise");
+        m_producer.attach(m_convert);
+    }
+
+    int last_val = 0;
+    int val = 0;
+    kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
+    for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && m_producer.is_valid(); z++) {
+        if (m_stopAudioThumbs) break;
+        val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
+        if (last_val != val && val > 1) {
+            slotAudioThumbProgress(val);
+            last_val = val;
+        }
+        m_producer.seek(z);
+        Mlt::Frame *mlt_frame = m_producer.get_frame();
+        if (mlt_frame && mlt_frame->is_valid()) {
+            double m_framesPerSecond = mlt_producer_get_fps(m_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;
+            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);
+                for (int i = 0; i < m_array.size(); i++) {
+                    m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
+                }
+                m_audioThumbFile.write(m_array);
+
+            }
+        } else {
+            m_audioThumbFile.write(QByteArray(m_arrayWidth, '\x00'));
+        }
+        delete mlt_frame;
+    }
+    m_audioThumbFile.close();
+    if (m_stopAudioThumbs) {
+        m_audioThumbFile.remove();
+    } else {
+        slotAudioThumbOver();
+    }
+}
+
 void KThumb::slotAudioThumbProgress(const int progress)
 {
     m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);