#include <QImage>
#include <QApplication>
#include <QtConcurrentRun>
-
-void MyThread::init(KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth)
-{
- stop_me = false;
- m_isWorking = false;
- f.setFileName(target);
- m_url = url;
- m_frame = frame;
- m_frameLength = frameLength;
- m_frequency = frequency;
- m_channels = channels;
- m_arrayWidth = arrayWidth;
-}
-
-bool MyThread::isWorking()
-{
- return m_isWorking;
-}
-
-void MyThread::run()
-{
- if (!f.open(QIODevice::WriteOnly)) {
- kDebug() << "++++++++ ERROR WRITING TO FILE: " << f.fileName() << endl;
- kDebug() << "++++++++ DISABLING AUDIO THUMBS" << endl;
- KdenliveSettings::setAudiothumbnails(false);
- return;
- }
- m_isWorking = true;
- 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 (stop_me) break;
- val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
- if (last_val != val && val > 1) {
- emit audioThumbProgress(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 ;
- }
- f.write(m_array);
-
- }
- } else {
- f.write(QByteArray(m_arrayWidth, '\x00'));
- }
- delete mlt_frame;
- }
- //kDebug() << "done";
- f.close();
- m_isWorking = false;
- if (stop_me) {
- f.remove();
- } else emit audioThumbOver();
-}
-
+#include <QVarLengthArray>
KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/) :
QObject(parent),
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()));*/
}
KThumb::~KThumb()
{
m_requestedThumbs.clear();
+ 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_requestedThumbs.clear();
+ m_intraFramesQueue.clear();
m_future.waitForFinished();
+ m_intra.waitForFinished();
m_producer = producer;
// FIXME: the profile() call leaks an object, but trying to free
// it leads to a double-free in Profile::~Profile()
- m_dar = producer->profile()->dar();
+ if (producer) m_dar = producer->profile()->dar();
}
void KThumb::clearProducer()
{
- m_producer = NULL;
+ setProducer(NULL);
}
bool KThumb::hasProducer() const
void KThumb::updateClipUrl(KUrl url, const QString &hash)
{
m_url = url;
- if (m_producer)
- m_producer->set("resource", url.path().toUtf8().constData());
+ //if (m_producer)
+ //m_producer->set("resource", url.path().toUtf8().constData());
m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
}
return pix;
}
-
//static
QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
{
QImage p(width, height, QImage::Format_ARGB32_Premultiplied);
- if (producer == NULL) {
+ if (producer == NULL || !producer->is_valid()) {
p.fill(Qt::red);
return p;
}
return p;
}
- /*Mlt::Producer parentProd(producer->parent());
- Mlt::Service service(parentProd.get_service());
- mlt_service_lock(service.get_service());*/
int ow = width;
int oh = height;
mlt_image_format format = mlt_image_rgb24a;
delete frame;
return p;
}
+
+
+//static
+uint KThumb::imageVariance(QImage image )
+{
+ uint delta = 0;
+ uint avg = 0;
+ uint bytes = image.numBytes();
+ uint STEPS = bytes/2;
+ QVarLengthArray<uchar> pivot(STEPS);
+ const uchar *bits=image.bits();
+ // First pass: get pivots and taking average
+ for( uint i=0; i<STEPS ; i++ ){
+ pivot[i] = bits[2 * i];
+#if QT_VERSION >= 0x040700
+ avg+=pivot.at(i);
+#else
+ avg+=pivot[i];
+#endif
+ }
+ avg=avg/STEPS;
+ // Second Step: calculate delta (average?)
+ for (uint i=0; i<STEPS; i++)
+ {
+#if QT_VERSION >= 0x040700
+ int curdelta=abs(int(avg - pivot.at(i)));
+#else
+ int curdelta=abs(int(avg - pivot[i]));
+#endif
+ delta+=curdelta;
+ }
+ return delta/STEPS;
+}
+
/*
void KThumb::getImage(KUrl url, int frame, int width, int height)
{
QFile f(m_thumbFile);
if (f.open(QIODevice::ReadOnly)) {
- QByteArray channelarray = f.readAll();
+ const QByteArray channelarray = f.readAll();
f.close();
if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
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[h2 + h3 + i];
+ for (int i = 0; i < arrayWidth; i++) {
+ m_array[i] = channelarray.at(h2 + h3 + i);
+ }
+ h3 += arrayWidth;
storeIn[z][c] = m_array;
}
+ h2 += h1;
}
emit audioThumbReady(storeIn);
slotAudioThumbOver();
void KThumb::slotCreateAudioThumbs()
{
+ Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
+ Mlt::Producer producer(prof, m_url.path().toUtf8().data());
+ if (!producer.is_valid()) {
+ kDebug() << "++++++++ INVALID CLIP: " << m_url.path();
+ return;
+ }
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);
+ 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++) {
+ //kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
+ 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);
if (last_val != val && val > 1) {
- slotAudioThumbProgress(val);
+ m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), val);
last_val = val;
}
- m_producer.seek(z);
- Mlt::Frame *mlt_frame = m_producer.get_frame();
+ producer.seek(z);
+ Mlt::Frame *mlt_frame = producer.get_frame();
if (mlt_frame && mlt_frame->is_valid()) {
- double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer());
+ 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;
qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
}
}
-void KThumb::slotAudioThumbProgress(const int progress)
-{
- m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
-}
-
void KThumb::slotAudioThumbOver()
{
m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
m_clipManager->askForAudioThumb(id);
}
+#if KDE_IS_VERSION(4,5,0)
+void KThumb::queryIntraThumbs(int start, int end)
+{
+ for (int i = start; i <= end; i++) {
+ if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
+ }
+ qSort(m_intraFramesQueue);
+ if (!m_intra.isRunning()) m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
+}
+
+void KThumb::slotGetIntraThumbs()
+{
+ int theight = KdenliveSettings::trackheight();
+ int twidth = FRAME_SIZE;
+ QString path = m_url.path() + "_";
+ QImage img;
+
+ while (!m_intraFramesQueue.isEmpty()) {
+ int pos = m_intraFramesQueue.takeFirst();
+ if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
+ m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getFrame(m_producer, pos, twidth, theight));
+ }
+ m_intraFramesQueue.removeAll(pos);
+ }
+ emit thumbsCached();
+}
+
+QImage KThumb::findCachedThumb(const QString path)
+{
+ QImage img;
+ m_clipManager->pixmapCache->findImage(path, &img);
+ return img;
+}
+#endif
#include "kthumb.moc"