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(),
51 m_clipManager(clipManager),
53 m_stopAudioThumbs(false)
55 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
60 m_requestedThumbs.clear();
61 m_intraFramesQueue.clear();
62 if (m_audioThumbProducer.isRunning()) {
63 m_stopAudioThumbs = true;
64 m_audioThumbProducer.waitForFinished();
67 m_future.waitForFinished();
68 m_intra.waitForFinished();
71 void KThumb::setProducer(Mlt::Producer *producer)
73 m_requestedThumbs.clear();
74 m_intraFramesQueue.clear();
75 m_future.waitForFinished();
76 m_intra.waitForFinished();
78 m_producer = producer;
80 // FIXME: the profile() call leaks an object, but trying to free
81 // it leads to a double-free in Profile::~Profile()
83 m_dar = producer->profile()->dar();
84 m_ratio = (double) producer->profile()->width() / producer->profile()->height();
89 void KThumb::clearProducer()
94 bool KThumb::hasProducer() const
96 return m_producer != NULL;
99 void KThumb::updateThumbUrl(const QString &hash)
101 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
104 void KThumb::updateClipUrl(KUrl url, const QString &hash)
108 //m_producer->set("resource", url.path().toUtf8().constData());
109 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
113 QPixmap KThumb::getImage(KUrl url, int width, int height)
115 if (url.isEmpty()) return QPixmap();
116 return getImage(url, 0, width, height);
119 void KThumb::extractImage(int frame, int frame2)
121 if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
122 if (frame != -1 && !m_requestedThumbs.contains(frame)) m_requestedThumbs.append(frame);
123 if (frame2 != -1 && !m_requestedThumbs.contains(frame2)) m_requestedThumbs.append(frame2);
124 if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &KThumb::doGetThumbs);
127 void KThumb::doGetThumbs()
129 const int theight = KdenliveSettings::trackheight();
130 const int swidth = (int)(theight * m_ratio + 0.5);
131 const int dwidth = (int)(theight * m_dar + 0.5);
133 while (!m_requestedThumbs.isEmpty()) {
134 int frame = m_requestedThumbs.takeFirst();
136 emit thumbReady(frame, getFrame(m_producer, frame, swidth, dwidth, theight));
141 QPixmap KThumb::extractImage(int frame, int width, int height)
144 QImage img = getFrame(m_producer, frame, (int) (height * m_ratio + 0.5), width, height);
146 return QPixmap::fromImage(img);
150 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
152 Mlt::Profile profile(KdenliveSettings::current_profile().toUtf8().constData());
153 QPixmap pix(width, height);
154 if (url.isEmpty()) return pix;
156 //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
157 //Mlt::Producer producer(profile, "xml-string", tmp);
158 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
159 double swidth = (double) profile.width() / profile.height();
160 pix = QPixmap::fromImage(getFrame(producer, frame, (int) (height * swidth + 0.5), width, height));
166 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int frameWidth, int displayWidth, int height)
168 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
169 if (producer == NULL || !producer->is_valid()) {
174 if (producer->is_blank()) {
179 producer->seek(framepos);
180 Mlt::Frame *frame = producer->get_frame();
181 p = getFrame(frame, frameWidth, displayWidth, height);
188 QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int height)
190 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
191 if (frame == NULL || !frame->is_valid()) {
198 mlt_image_format format = mlt_image_rgb24a;
199 uint8_t *data = frame->get_image(format, ow, oh, 0);
200 QImage image((uchar *)data, ow, oh, QImage::Format_ARGB32_Premultiplied);
201 if (!image.isNull()) {
202 if (ow > (2 * displayWidth)) {
203 // there was a scaling problem, do it manually
204 QImage scaled = image.scaled(displayWidth, height);
205 image = scaled.rgbSwapped();
207 image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
209 QPainter painter(&p);
210 painter.fillRect(p.rect(), Qt::black);
211 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
212 painter.drawImage(p.rect(), image);
220 uint KThumb::imageVariance(QImage image )
224 uint bytes = image.numBytes();
225 uint STEPS = bytes/2;
226 QVarLengthArray<uchar> pivot(STEPS);
227 const uchar *bits=image.bits();
228 // First pass: get pivots and taking average
229 for( uint i=0; i<STEPS ; i++ ){
230 pivot[i] = bits[2 * i];
231 #if QT_VERSION >= 0x040700
238 // Second Step: calculate delta (average?)
239 for (uint i=0; i<STEPS; i++)
241 #if QT_VERSION >= 0x040700
242 int curdelta=abs(int(avg - pivot.at(i)));
244 int curdelta=abs(int(avg - pivot[i]));
252 void KThumb::getImage(KUrl url, int frame, int width, int height)
254 if (url.isEmpty()) return;
255 QPixmap image(width, height);
256 Mlt::Producer m_producer(url.path().toUtf8().constData());
257 image.fill(Qt::black);
259 if (m_producer.is_blank()) {
260 emit thumbReady(frame, image);
263 Mlt::Filter m_convert("avcolour_space");
264 m_convert.set("forced", mlt_image_rgb24a);
265 m_producer.attach(m_convert);
266 m_producer.seek(frame);
267 Mlt::Frame * m_frame = m_producer.get_frame();
268 mlt_image_format format = mlt_image_rgb24a;
271 if (m_frame && m_frame->is_valid()) {
272 uint8_t *thumb = m_frame->get_image(format, width, height);
273 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
274 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
276 if (m_frame) delete m_frame;
277 emit thumbReady(frame, image);
280 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
282 if (url.isEmpty()) return;
283 QPixmap image(width, height);
284 Mlt::Producer m_producer(url.path().toUtf8().constData());
285 image.fill(Qt::black);
287 if (m_producer.is_blank()) {
288 emit thumbReady(startframe, image);
289 emit thumbReady(endframe, image);
292 Mlt::Filter m_convert("avcolour_space");
293 m_convert.set("forced", mlt_image_rgb24a);
294 m_producer.attach(m_convert);
295 m_producer.seek(startframe);
296 Mlt::Frame * m_frame = m_producer.get_frame();
297 mlt_image_format format = mlt_image_rgb24a;
301 if (m_frame && m_frame->is_valid()) {
302 uint8_t *thumb = m_frame->get_image(format, width, height);
303 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
304 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
306 if (m_frame) delete m_frame;
307 emit thumbReady(startframe, image);
309 image.fill(Qt::black);
310 m_producer.seek(endframe);
311 m_frame = m_producer.get_frame();
313 if (m_frame && m_frame->is_valid()) {
314 uint8_t *thumb = m_frame->get_image(format, width, height);
315 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
316 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
318 if (m_frame) delete m_frame;
319 emit thumbReady(endframe, image);
322 void KThumb::stopAudioThumbs()
324 if (m_audioThumbProducer.isRunning()) {
325 m_stopAudioThumbs = true;
326 m_audioThumbProducer.waitForFinished();
327 slotAudioThumbOver();
331 void KThumb::removeAudioThumb()
333 if (m_thumbFile.isEmpty()) return;
335 QFile f(m_thumbFile);
339 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
342 slotAudioThumbOver();
345 if (m_audioThumbProducer.isRunning()) {
349 QMap <int, QMap <int, QByteArray> > storeIn;
350 //FIXME: Hardcoded!!!
352 m_channels = channel;
354 QFile f(m_thumbFile);
355 if (f.open(QIODevice::ReadOnly)) {
356 const QByteArray channelarray = f.readAll();
358 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
359 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
361 slotAudioThumbOver();
365 kDebug() << "reading audio thumbs from file";
367 int h1 = arrayWidth * m_channels;
368 int h2 = (int) frame * h1;
370 for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
372 for (int c = 0; c < m_channels; c++) {
373 QByteArray m_array(arrayWidth, '\x00');
374 for (int i = 0; i < arrayWidth; i++) {
375 m_array[i] = channelarray.at(h2 + h3 + i);
378 storeIn[z][c] = m_array;
382 emit audioThumbReady(storeIn);
383 slotAudioThumbOver();
385 if (m_audioThumbProducer.isRunning()) return;
386 m_audioThumbFile.setFileName(m_thumbFile);
388 m_frameLength = frameLength;
389 m_arrayWidth = arrayWidth;
390 m_audioThumbProducer = QtConcurrent::run(this, &KThumb::slotCreateAudioThumbs);
391 /*m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
392 m_audioThumbProducer.start(QThread::LowestPriority);*/
393 // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
397 void KThumb::slotCreateAudioThumbs()
399 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
400 Mlt::Producer producer(prof, m_url.path().toUtf8().data());
401 if (!producer.is_valid()) {
402 kDebug() << "++++++++ INVALID CLIP: " << m_url.path();
405 if (!m_audioThumbFile.open(QIODevice::WriteOnly)) {
406 kDebug() << "++++++++ ERROR WRITING TO FILE: " << m_audioThumbFile.fileName();
407 kDebug() << "++++++++ DISABLING AUDIO THUMBS";
408 KdenliveSettings::setAudiothumbnails(false);
412 if (KdenliveSettings::normaliseaudiothumbs()) {
413 Mlt::Filter m_convert(prof, "volume");
414 m_convert.set("gain", "normalise");
415 producer.attach(m_convert);
420 //kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
421 for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && producer.is_valid(); z++) {
422 if (m_stopAudioThumbs) break;
423 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
424 if (last_val != val && val > 1) {
425 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), val);
429 Mlt::Frame *mlt_frame = producer.get_frame();
430 if (mlt_frame && mlt_frame->is_valid()) {
431 double m_framesPerSecond = mlt_producer_get_fps(producer.get_producer());
432 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
433 mlt_audio_format m_audioFormat = mlt_audio_pcm;
434 qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
436 for (int c = 0; c < m_channels; c++) {
438 m_array.resize(m_arrayWidth);
439 for (int i = 0; i < m_array.size(); i++) {
440 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
442 m_audioThumbFile.write(m_array);
446 m_audioThumbFile.write(QByteArray(m_arrayWidth, '\x00'));
450 m_audioThumbFile.close();
451 if (m_stopAudioThumbs) {
452 m_audioThumbFile.remove();
454 slotAudioThumbOver();
458 void KThumb::slotAudioThumbOver()
460 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
461 m_clipManager->endAudioThumbsGeneration(m_id);
464 void KThumb::askForAudioThumbs(const QString &id)
466 m_clipManager->askForAudioThumb(id);
469 #if KDE_IS_VERSION(4,5,0)
470 void KThumb::queryIntraThumbs(QList <int> missingFrames)
472 foreach (int i, missingFrames) {
473 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
475 qSort(m_intraFramesQueue);
476 if (!m_intra.isRunning()) m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
479 void KThumb::slotGetIntraThumbs()
481 const int theight = KdenliveSettings::trackheight();
482 const int frameWidth = (int)(theight * m_ratio + 0.5);
483 const int displayWidth = (int)(theight * m_dar + 0.5);
484 QString path = m_url.path() + "_";
485 bool addedThumbs = false;
487 while (!m_intraFramesQueue.isEmpty()) {
488 int pos = m_intraFramesQueue.takeFirst();
489 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
490 if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getFrame(m_producer, pos, frameWidth, displayWidth, theight))) {
493 else kDebug()<<"// INSERT FAILD FOR: "<<pos;
495 m_intraFramesQueue.removeAll(pos);
497 if (addedThumbs) emit thumbsCached();
500 QImage KThumb::findCachedThumb(const QString path)
503 m_clipManager->pixmapCache->findImage(path, &img);
508 #include "kthumb.moc"