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;
79 // FIXME: the profile() call leaks an object, but trying to free
80 // it leads to a double-free in Profile::~Profile()
82 m_dar = producer->profile()->dar();
83 m_ratio = (double) producer->profile()->width() / producer->profile()->height();
88 void KThumb::clearProducer()
93 bool KThumb::hasProducer() const
95 return m_producer != NULL;
98 void KThumb::updateThumbUrl(const QString &hash)
100 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
103 void KThumb::updateClipUrl(KUrl url, const QString &hash)
106 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
110 QPixmap KThumb::getImage(KUrl url, int width, int height)
112 if (url.isEmpty()) return QPixmap();
113 return getImage(url, 0, width, height);
116 void KThumb::extractImage(int frame, int frame2)
118 if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
119 if (frame != -1 && !m_requestedThumbs.contains(frame)) m_requestedThumbs.append(frame);
120 if (frame2 != -1 && !m_requestedThumbs.contains(frame2)) m_requestedThumbs.append(frame2);
121 qSort(m_requestedThumbs);
122 if (!m_future.isRunning()) {
123 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 QImage img = getProducerFrame(frame, swidth, dwidth, theight);
137 emit thumbReady(frame, img);
142 QPixmap KThumb::extractImage(int frame, int width, int height)
144 if (m_producer == NULL) {
145 QPixmap p(width, height);
149 QImage img = getProducerFrame(frame, (int) (height * m_ratio + 0.5), width, height);
150 return QPixmap::fromImage(img);
154 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
156 Mlt::Profile profile(KdenliveSettings::current_profile().toUtf8().constData());
157 QPixmap pix(width, height);
158 if (url.isEmpty()) return pix;
160 //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
161 //Mlt::Producer producer(profile, "xml-string", tmp);
162 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
163 double swidth = (double) profile.width() / profile.height();
164 pix = QPixmap::fromImage(getFrame(producer, frame, (int) (height * swidth + 0.5), width, height));
170 QImage KThumb::getProducerFrame(int framepos, int frameWidth, int displayWidth, int height)
172 if (m_producer == NULL || !m_producer->is_valid()) {
173 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
174 p.fill(QColor(Qt::red).rgb());
177 if (m_producer->is_blank()) {
178 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
179 p.fill(QColor(Qt::black).rgb());
183 m_producer->seek(framepos);
184 Mlt::Frame *frame = m_producer->get_frame();
185 QImage p = getFrame(frame, frameWidth, displayWidth, height);
192 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int frameWidth, int displayWidth, int height)
194 if (producer == NULL || !producer->is_valid()) {
195 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
196 p.fill(QColor(Qt::red).rgb());
199 if (producer->is_blank()) {
200 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
201 p.fill(QColor(Qt::black).rgb());
205 producer->seek(framepos);
206 Mlt::Frame *frame = producer->get_frame();
207 QImage p = getFrame(frame, frameWidth, displayWidth, height);
214 QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int height)
216 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
217 if (frame == NULL || !frame->is_valid()) {
218 p.fill(QColor(Qt::red).rgb());
224 mlt_image_format format = mlt_image_rgb24a;
225 uint8_t *data = frame->get_image(format, ow, oh, 0);
226 QImage image((uchar *)data, ow, oh, QImage::Format_ARGB32_Premultiplied);
227 if (!image.isNull()) {
228 if (ow > (2 * displayWidth)) {
229 // there was a scaling problem, do it manually
230 QImage scaled = image.scaled(displayWidth, height);
231 image = scaled.rgbSwapped();
233 image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
235 p.fill(QColor(Qt::black).rgb());
236 QPainter painter(&p);
237 painter.drawImage(p.rect(), image);
240 p.fill(QColor(Qt::red).rgb());
245 uint KThumb::imageVariance(QImage image )
249 uint bytes = image.numBytes();
250 uint STEPS = bytes/2;
251 QVarLengthArray<uchar> pivot(STEPS);
252 const uchar *bits=image.bits();
253 // First pass: get pivots and taking average
254 for( uint i=0; i<STEPS ; i++ ){
255 pivot[i] = bits[2 * i];
256 #if QT_VERSION >= 0x040700
263 // Second Step: calculate delta (average?)
264 for (uint i=0; i<STEPS; i++)
266 #if QT_VERSION >= 0x040700
267 int curdelta=abs(int(avg - pivot.at(i)));
269 int curdelta=abs(int(avg - pivot[i]));
277 void KThumb::getImage(KUrl url, int frame, int width, int height)
279 if (url.isEmpty()) return;
280 QPixmap image(width, height);
281 Mlt::Producer m_producer(url.path().toUtf8().constData());
282 image.fill(Qt::black);
284 if (m_producer.is_blank()) {
285 emit thumbReady(frame, image);
288 Mlt::Filter m_convert("avcolour_space");
289 m_convert.set("forced", mlt_image_rgb24a);
290 m_producer.attach(m_convert);
291 m_producer.seek(frame);
292 Mlt::Frame * m_frame = m_producer.get_frame();
293 mlt_image_format format = mlt_image_rgb24a;
296 if (m_frame && m_frame->is_valid()) {
297 uint8_t *thumb = m_frame->get_image(format, width, height);
298 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
299 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
301 if (m_frame) delete m_frame;
302 emit thumbReady(frame, image);
305 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
307 if (url.isEmpty()) return;
308 QPixmap image(width, height);
309 Mlt::Producer m_producer(url.path().toUtf8().constData());
310 image.fill(Qt::black);
312 if (m_producer.is_blank()) {
313 emit thumbReady(startframe, image);
314 emit thumbReady(endframe, image);
317 Mlt::Filter m_convert("avcolour_space");
318 m_convert.set("forced", mlt_image_rgb24a);
319 m_producer.attach(m_convert);
320 m_producer.seek(startframe);
321 Mlt::Frame * m_frame = m_producer.get_frame();
322 mlt_image_format format = mlt_image_rgb24a;
326 if (m_frame && m_frame->is_valid()) {
327 uint8_t *thumb = m_frame->get_image(format, width, height);
328 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
329 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
331 if (m_frame) delete m_frame;
332 emit thumbReady(startframe, image);
334 image.fill(Qt::black);
335 m_producer.seek(endframe);
336 m_frame = m_producer.get_frame();
338 if (m_frame && m_frame->is_valid()) {
339 uint8_t *thumb = m_frame->get_image(format, width, height);
340 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
341 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
343 if (m_frame) delete m_frame;
344 emit thumbReady(endframe, image);
347 void KThumb::stopAudioThumbs()
349 if (m_audioThumbProducer.isRunning()) {
350 m_stopAudioThumbs = true;
351 m_audioThumbProducer.waitForFinished();
352 slotAudioThumbOver();
356 void KThumb::removeAudioThumb()
358 if (m_thumbFile.isEmpty()) return;
360 QFile f(m_thumbFile);
364 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
367 slotAudioThumbOver();
370 if (m_audioThumbProducer.isRunning()) {
374 audioByteArray storeIn;
375 //FIXME: Hardcoded!!!
377 m_channels = channel;
379 QFile f(m_thumbFile);
380 if (f.open(QIODevice::ReadOnly)) {
381 const QByteArray channelarray = f.readAll();
383 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
384 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
386 slotAudioThumbOver();
390 kDebug() << "reading audio thumbs from file";
392 int h1 = arrayWidth * m_channels;
393 int h2 = (int) frame * h1;
395 for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
397 for (int c = 0; c < m_channels; c++) {
398 QByteArray m_array(arrayWidth, '\x00');
399 for (int i = 0; i < arrayWidth; i++) {
400 m_array[i] = channelarray.at(h2 + h3 + i);
403 storeIn[z][c] = m_array;
407 emit audioThumbReady(storeIn);
408 slotAudioThumbOver();
410 if (m_audioThumbProducer.isRunning()) return;
411 m_audioThumbFile.setFileName(m_thumbFile);
413 m_frameLength = frameLength;
414 m_arrayWidth = arrayWidth;
415 m_audioThumbProducer = QtConcurrent::run(this, &KThumb::slotCreateAudioThumbs);
416 /*m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
417 m_audioThumbProducer.start(QThread::LowestPriority);*/
418 // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
422 void KThumb::slotCreateAudioThumbs()
424 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
425 Mlt::Producer producer(prof, m_url.path().toUtf8().data());
426 if (!producer.is_valid()) {
427 kDebug() << "++++++++ INVALID CLIP: " << m_url.path();
430 if (!m_audioThumbFile.open(QIODevice::WriteOnly)) {
431 kDebug() << "++++++++ ERROR WRITING TO FILE: " << m_audioThumbFile.fileName();
432 kDebug() << "++++++++ DISABLING AUDIO THUMBS";
433 KdenliveSettings::setAudiothumbnails(false);
437 if (KdenliveSettings::normaliseaudiothumbs()) {
438 Mlt::Filter m_convert(prof, "volume");
439 m_convert.set("gain", "normalise");
440 producer.attach(m_convert);
445 //kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
446 for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && producer.is_valid(); z++) {
447 if (m_stopAudioThumbs) break;
448 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
449 if (last_val != val && val > 1) {
450 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), val);
454 Mlt::Frame *mlt_frame = producer.get_frame();
455 if (mlt_frame && mlt_frame->is_valid()) {
456 double m_framesPerSecond = mlt_producer_get_fps(producer.get_producer());
457 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
458 mlt_audio_format m_audioFormat = mlt_audio_pcm;
459 qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
461 for (int c = 0; c < m_channels; c++) {
463 m_array.resize(m_arrayWidth);
464 for (int i = 0; i < m_array.size(); i++) {
465 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
467 m_audioThumbFile.write(m_array);
471 m_audioThumbFile.write(QByteArray(m_arrayWidth, '\x00'));
475 m_audioThumbFile.close();
476 if (m_stopAudioThumbs) {
477 m_audioThumbFile.remove();
479 slotAudioThumbOver();
483 void KThumb::slotAudioThumbOver()
485 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
486 m_clipManager->endAudioThumbsGeneration(m_id);
489 void KThumb::askForAudioThumbs(const QString &id)
491 m_clipManager->askForAudioThumb(id);
494 #if KDE_IS_VERSION(4,5,0)
495 void KThumb::queryIntraThumbs(QList <int> missingFrames)
497 foreach (int i, missingFrames) {
498 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
500 qSort(m_intraFramesQueue);
501 if (!m_intra.isRunning()) {
502 m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
506 void KThumb::slotGetIntraThumbs()
508 const int theight = KdenliveSettings::trackheight();
509 const int frameWidth = (int)(theight * m_ratio + 0.5);
510 const int displayWidth = (int)(theight * m_dar + 0.5);
511 QString path = m_url.path() + "_";
512 bool addedThumbs = false;
514 while (!m_intraFramesQueue.isEmpty()) {
515 int pos = m_intraFramesQueue.takeFirst();
516 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
517 if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getProducerFrame(pos, frameWidth, displayWidth, theight))) {
520 else kDebug()<<"// INSERT FAILD FOR: "<<pos;
522 m_intraFramesQueue.removeAll(pos);
524 if (addedThumbs) emit thumbsCached();
527 QImage KThumb::findCachedThumb(const QString path)
530 m_clipManager->pixmapCache->findImage(path, &img);
535 #include "kthumb.moc"