1 /***************************************************************************
2 krender.cpp - description
4 begin : Fri Nov 22 2002
5 copyright : (C) 2002 by Jason Wood
6 email : jasonwood@blueyonder.co.uk
7 copyright : (C) 2005 Lcio Fl�io Corr�
8 email : lucio.correa@gmail.com
9 copyright : (C) Marco Gittler
10 email : g.marco@freenet.de
12 ***************************************************************************/
14 /***************************************************************************
16 * This program is free software; you can redistribute it and/or modify *
17 * it under the terms of the GNU General Public License as published by *
18 * the Free Software Foundation; either version 2 of the License, or *
19 * (at your option) any later version. *
21 ***************************************************************************/
24 #include "clipmanager.h"
26 #include "kdenlivesettings.h"
28 #include <mlt++/Mlt.h>
30 #include <kio/netaccess.h>
33 #include <kfileitem.h>
34 #include <kmessagebox.h>
35 #include <KStandardDirs>
39 #include <QApplication>
40 #include <QtConcurrentRun>
41 #include <QVarLengthArray>
43 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/) :
45 m_audioThumbProducer(),
50 m_clipManager(clipManager),
52 m_stopAudioThumbs(false)
54 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
59 m_requestedThumbs.clear();
60 m_intraFramesQueue.clear();
61 if (m_audioThumbProducer.isRunning()) {
62 m_stopAudioThumbs = true;
63 m_audioThumbProducer.waitForFinished();
66 m_future.waitForFinished();
67 m_intra.waitForFinished();
70 void KThumb::setProducer(Mlt::Producer *producer)
72 m_requestedThumbs.clear();
73 m_intraFramesQueue.clear();
74 m_future.waitForFinished();
75 m_intra.waitForFinished();
76 m_producer = producer;
77 // FIXME: the profile() call leaks an object, but trying to free
78 // it leads to a double-free in Profile::~Profile()
79 if (producer) m_dar = producer->profile()->dar();
82 void KThumb::clearProducer()
87 bool KThumb::hasProducer() const
89 return m_producer != NULL;
92 void KThumb::updateThumbUrl(const QString &hash)
94 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
97 void KThumb::updateClipUrl(KUrl url, const QString &hash)
101 //m_producer->set("resource", url.path().toUtf8().constData());
102 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
106 QPixmap KThumb::getImage(KUrl url, int width, int height)
108 if (url.isEmpty()) return QPixmap();
109 return getImage(url, 0, width, height);
112 void KThumb::extractImage(int frame, int frame2)
114 if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
115 if (frame != -1 && !m_requestedThumbs.contains(frame)) m_requestedThumbs.append(frame);
116 if (frame2 != -1 && !m_requestedThumbs.contains(frame2)) m_requestedThumbs.append(frame2);
117 if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &KThumb::doGetThumbs);
120 void KThumb::doGetThumbs()
122 const int theight = KdenliveSettings::trackheight();
123 const int twidth = FRAME_SIZE;//(int)(theight * m_dar + 0.5);
125 while (!m_requestedThumbs.isEmpty()) {
126 int frame = m_requestedThumbs.takeFirst();
128 QImage img = getFrame(m_producer, frame, twidth, theight);
129 emit thumbReady(frame, img);
134 QPixmap KThumb::extractImage(int frame, int width, int height)
136 return QPixmap::fromImage(getFrame(m_producer, frame, width, height));
140 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
142 Mlt::Profile profile(KdenliveSettings::current_profile().toUtf8().constData());
143 QPixmap pix(width, height);
144 if (url.isEmpty()) return pix;
146 //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
147 //Mlt::Producer producer(profile, "xml-string", tmp);
148 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
150 pix = QPixmap::fromImage(getFrame(producer, frame, width, height));
156 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
158 QImage p(width, height, QImage::Format_ARGB32_Premultiplied);
159 if (producer == NULL || !producer->is_valid()) {
164 if (producer->is_blank()) {
169 producer->seek(framepos);
170 Mlt::Frame *frame = producer->get_frame();
172 kDebug() << "///// BROKEN FRAME";
179 mlt_image_format format = mlt_image_rgb24a;
180 uint8_t *data = frame->get_image(format, ow, oh, 0);
181 QImage image((uchar *)data, ow, oh, QImage::Format_ARGB32_Premultiplied);
182 //mlt_service_unlock(service.get_service());
184 if (!image.isNull()) {
185 if (ow > (2 * width)) {
186 // there was a scaling problem, do it manually
187 QImage scaled = image.scaled(width, height);
188 p = scaled.rgbSwapped();
189 } else p = image.rgbSwapped();
199 uint KThumb::imageVariance(QImage image )
203 uint bytes = image.numBytes();
204 uint STEPS = bytes/2;
205 QVarLengthArray<uchar> pivot(STEPS);
206 const uchar *bits=image.bits();
207 // First pass: get pivots and taking average
208 for( uint i=0; i<STEPS ; i++ ){
209 pivot[i] = bits[2 * i];
210 #if QT_VERSION >= 0x040700
217 // Second Step: calculate delta (average?)
218 for (uint i=0; i<STEPS; i++)
220 #if QT_VERSION >= 0x040700
221 int curdelta=abs(int(avg - pivot.at(i)));
223 int curdelta=abs(int(avg - pivot[i]));
231 void KThumb::getImage(KUrl url, int frame, int width, int height)
233 if (url.isEmpty()) return;
234 QPixmap image(width, height);
235 Mlt::Producer m_producer(url.path().toUtf8().constData());
236 image.fill(Qt::black);
238 if (m_producer.is_blank()) {
239 emit thumbReady(frame, image);
242 Mlt::Filter m_convert("avcolour_space");
243 m_convert.set("forced", mlt_image_rgb24a);
244 m_producer.attach(m_convert);
245 m_producer.seek(frame);
246 Mlt::Frame * m_frame = m_producer.get_frame();
247 mlt_image_format format = mlt_image_rgb24a;
250 if (m_frame && m_frame->is_valid()) {
251 uint8_t *thumb = m_frame->get_image(format, width, height);
252 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
253 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
255 if (m_frame) delete m_frame;
256 emit thumbReady(frame, image);
259 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
261 if (url.isEmpty()) return;
262 QPixmap image(width, height);
263 Mlt::Producer m_producer(url.path().toUtf8().constData());
264 image.fill(Qt::black);
266 if (m_producer.is_blank()) {
267 emit thumbReady(startframe, image);
268 emit thumbReady(endframe, image);
271 Mlt::Filter m_convert("avcolour_space");
272 m_convert.set("forced", mlt_image_rgb24a);
273 m_producer.attach(m_convert);
274 m_producer.seek(startframe);
275 Mlt::Frame * m_frame = m_producer.get_frame();
276 mlt_image_format format = mlt_image_rgb24a;
280 if (m_frame && m_frame->is_valid()) {
281 uint8_t *thumb = m_frame->get_image(format, width, height);
282 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
283 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
285 if (m_frame) delete m_frame;
286 emit thumbReady(startframe, image);
288 image.fill(Qt::black);
289 m_producer.seek(endframe);
290 m_frame = m_producer.get_frame();
292 if (m_frame && m_frame->is_valid()) {
293 uint8_t *thumb = m_frame->get_image(format, width, height);
294 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
295 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
297 if (m_frame) delete m_frame;
298 emit thumbReady(endframe, image);
301 void KThumb::stopAudioThumbs()
303 if (m_audioThumbProducer.isRunning()) {
304 m_stopAudioThumbs = true;
305 m_audioThumbProducer.waitForFinished();
306 slotAudioThumbOver();
310 void KThumb::removeAudioThumb()
312 if (m_thumbFile.isEmpty()) return;
314 QFile f(m_thumbFile);
318 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
321 slotAudioThumbOver();
324 if (m_audioThumbProducer.isRunning()) {
328 QMap <int, QMap <int, QByteArray> > storeIn;
329 //FIXME: Hardcoded!!!
331 m_channels = channel;
333 QFile f(m_thumbFile);
334 if (f.open(QIODevice::ReadOnly)) {
335 const QByteArray channelarray = f.readAll();
337 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
338 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
340 slotAudioThumbOver();
344 kDebug() << "reading audio thumbs from file";
346 int h1 = arrayWidth * m_channels;
347 int h2 = (int) frame * h1;
349 for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
351 for (int c = 0; c < m_channels; c++) {
352 QByteArray m_array(arrayWidth, '\x00');
353 for (int i = 0; i < arrayWidth; i++) {
354 m_array[i] = channelarray.at(h2 + h3 + i);
357 storeIn[z][c] = m_array;
361 emit audioThumbReady(storeIn);
362 slotAudioThumbOver();
364 if (m_audioThumbProducer.isRunning()) return;
365 m_audioThumbFile.setFileName(m_thumbFile);
367 m_frameLength = frameLength;
368 m_arrayWidth = arrayWidth;
369 m_audioThumbProducer = QtConcurrent::run(this, &KThumb::slotCreateAudioThumbs);
370 /*m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
371 m_audioThumbProducer.start(QThread::LowestPriority);*/
372 // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
376 void KThumb::slotCreateAudioThumbs()
378 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
379 Mlt::Producer producer(prof, m_url.path().toUtf8().data());
380 if (!producer.is_valid()) {
381 kDebug() << "++++++++ INVALID CLIP: " << m_url.path();
384 if (!m_audioThumbFile.open(QIODevice::WriteOnly)) {
385 kDebug() << "++++++++ ERROR WRITING TO FILE: " << m_audioThumbFile.fileName();
386 kDebug() << "++++++++ DISABLING AUDIO THUMBS";
387 KdenliveSettings::setAudiothumbnails(false);
391 if (KdenliveSettings::normaliseaudiothumbs()) {
392 Mlt::Filter m_convert(prof, "volume");
393 m_convert.set("gain", "normalise");
394 producer.attach(m_convert);
399 //kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
400 for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && producer.is_valid(); z++) {
401 if (m_stopAudioThumbs) break;
402 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
403 if (last_val != val && val > 1) {
404 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), val);
408 Mlt::Frame *mlt_frame = producer.get_frame();
409 if (mlt_frame && mlt_frame->is_valid()) {
410 double m_framesPerSecond = mlt_producer_get_fps(producer.get_producer());
411 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
412 mlt_audio_format m_audioFormat = mlt_audio_pcm;
413 qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
415 for (int c = 0; c < m_channels; c++) {
417 m_array.resize(m_arrayWidth);
418 for (int i = 0; i < m_array.size(); i++) {
419 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
421 m_audioThumbFile.write(m_array);
425 m_audioThumbFile.write(QByteArray(m_arrayWidth, '\x00'));
429 m_audioThumbFile.close();
430 if (m_stopAudioThumbs) {
431 m_audioThumbFile.remove();
433 slotAudioThumbOver();
437 void KThumb::slotAudioThumbOver()
439 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
440 m_clipManager->endAudioThumbsGeneration(m_id);
443 void KThumb::askForAudioThumbs(const QString &id)
445 m_clipManager->askForAudioThumb(id);
448 #if KDE_IS_VERSION(4,5,0)
449 void KThumb::queryIntraThumbs(int start, int end)
451 for (int i = start; i <= end; i++) {
452 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
454 qSort(m_intraFramesQueue);
455 if (!m_intra.isRunning()) m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
458 void KThumb::slotGetIntraThumbs()
460 int theight = KdenliveSettings::trackheight();
461 int twidth = FRAME_SIZE;
462 QString path = m_url.path() + "_";
465 while (!m_intraFramesQueue.isEmpty()) {
466 int pos = m_intraFramesQueue.takeFirst();
467 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
468 m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getFrame(m_producer, pos, twidth, theight));
470 m_intraFramesQueue.removeAll(pos);
475 QImage KThumb::findCachedThumb(const QString path)
478 m_clipManager->pixmapCache->findImage(path, &img);
483 #include "kthumb.moc"