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;
226 const uchar* imagedata = frame->get_image(format, ow, oh);
227 QImage image(imagedata, ow, oh, QImage::Format_ARGB32_Premultiplied);
229 if (!image.isNull()) {
230 if (ow > (2 * displayWidth)) {
231 // there was a scaling problem, do it manually
232 image = image.scaled(displayWidth, height).rgbSwapped();
234 image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
236 p.fill(QColor(Qt::black).rgb());
237 QPainter painter(&p);
238 painter.drawImage(p.rect(), image);
241 p.fill(QColor(Qt::red).rgb());
246 uint KThumb::imageVariance(QImage image )
250 uint bytes = image.numBytes();
251 uint STEPS = bytes/2;
252 QVarLengthArray<uchar> pivot(STEPS);
253 const uchar *bits=image.bits();
254 // First pass: get pivots and taking average
255 for( uint i=0; i<STEPS ; i++ ){
256 pivot[i] = bits[2 * i];
257 #if QT_VERSION >= 0x040700
264 // Second Step: calculate delta (average?)
265 for (uint i=0; i<STEPS; i++)
267 #if QT_VERSION >= 0x040700
268 int curdelta=abs(int(avg - pivot.at(i)));
270 int curdelta=abs(int(avg - pivot[i]));
278 void KThumb::getImage(KUrl url, int frame, int width, int height)
280 if (url.isEmpty()) return;
281 QPixmap image(width, height);
282 Mlt::Producer m_producer(url.path().toUtf8().constData());
283 image.fill(Qt::black);
285 if (m_producer.is_blank()) {
286 emit thumbReady(frame, image);
289 Mlt::Filter m_convert("avcolour_space");
290 m_convert.set("forced", mlt_image_rgb24a);
291 m_producer.attach(m_convert);
292 m_producer.seek(frame);
293 Mlt::Frame * m_frame = m_producer.get_frame();
294 mlt_image_format format = mlt_image_rgb24a;
297 if (m_frame && m_frame->is_valid()) {
298 uint8_t *thumb = m_frame->get_image(format, width, height);
299 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
300 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
302 if (m_frame) delete m_frame;
303 emit thumbReady(frame, image);
306 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
308 if (url.isEmpty()) return;
309 QPixmap image(width, height);
310 Mlt::Producer m_producer(url.path().toUtf8().constData());
311 image.fill(Qt::black);
313 if (m_producer.is_blank()) {
314 emit thumbReady(startframe, image);
315 emit thumbReady(endframe, image);
318 Mlt::Filter m_convert("avcolour_space");
319 m_convert.set("forced", mlt_image_rgb24a);
320 m_producer.attach(m_convert);
321 m_producer.seek(startframe);
322 Mlt::Frame * m_frame = m_producer.get_frame();
323 mlt_image_format format = mlt_image_rgb24a;
327 if (m_frame && m_frame->is_valid()) {
328 uint8_t *thumb = m_frame->get_image(format, width, height);
329 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
330 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
332 if (m_frame) delete m_frame;
333 emit thumbReady(startframe, image);
335 image.fill(Qt::black);
336 m_producer.seek(endframe);
337 m_frame = m_producer.get_frame();
339 if (m_frame && m_frame->is_valid()) {
340 uint8_t *thumb = m_frame->get_image(format, width, height);
341 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
342 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
344 if (m_frame) delete m_frame;
345 emit thumbReady(endframe, image);
348 void KThumb::stopAudioThumbs()
350 if (m_audioThumbProducer.isRunning()) {
351 m_stopAudioThumbs = true;
352 m_audioThumbProducer.waitForFinished();
353 slotAudioThumbOver();
357 void KThumb::removeAudioThumb()
359 if (m_thumbFile.isEmpty()) return;
361 QFile f(m_thumbFile);
365 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
368 slotAudioThumbOver();
371 if (m_audioThumbProducer.isRunning()) {
375 audioByteArray storeIn;
376 //FIXME: Hardcoded!!!
378 m_channels = channel;
380 QFile f(m_thumbFile);
381 if (f.open(QIODevice::ReadOnly)) {
382 const QByteArray channelarray = f.readAll();
384 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
385 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
387 slotAudioThumbOver();
391 kDebug() << "reading audio thumbs from file";
393 int h1 = arrayWidth * m_channels;
394 int h2 = (int) frame * h1;
396 for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
398 for (int c = 0; c < m_channels; c++) {
399 QByteArray m_array(arrayWidth, '\x00');
400 for (int i = 0; i < arrayWidth; i++) {
401 m_array[i] = channelarray.at(h2 + h3 + i);
404 storeIn[z][c] = m_array;
408 emit audioThumbReady(storeIn);
409 slotAudioThumbOver();
411 if (m_audioThumbProducer.isRunning()) return;
412 m_audioThumbFile.setFileName(m_thumbFile);
414 m_frameLength = frameLength;
415 m_arrayWidth = arrayWidth;
416 m_audioThumbProducer = QtConcurrent::run(this, &KThumb::slotCreateAudioThumbs);
417 /*m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
418 m_audioThumbProducer.start(QThread::LowestPriority);*/
419 // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
423 void KThumb::slotCreateAudioThumbs()
425 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
426 Mlt::Producer producer(prof, m_url.path().toUtf8().data());
427 if (!producer.is_valid()) {
428 kDebug() << "++++++++ INVALID CLIP: " << m_url.path();
431 if (!m_audioThumbFile.open(QIODevice::WriteOnly)) {
432 kDebug() << "++++++++ ERROR WRITING TO FILE: " << m_audioThumbFile.fileName();
433 kDebug() << "++++++++ DISABLING AUDIO THUMBS";
434 KdenliveSettings::setAudiothumbnails(false);
438 if (KdenliveSettings::normaliseaudiothumbs()) {
439 Mlt::Filter m_convert(prof, "volume");
440 m_convert.set("gain", "normalise");
441 producer.attach(m_convert);
446 //kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
447 for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && producer.is_valid(); z++) {
448 if (m_stopAudioThumbs) break;
449 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
450 if (last_val != val && val > 1) {
451 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), val);
455 Mlt::Frame *mlt_frame = producer.get_frame();
456 if (mlt_frame && mlt_frame->is_valid()) {
457 double m_framesPerSecond = mlt_producer_get_fps(producer.get_producer());
458 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
459 mlt_audio_format m_audioFormat = mlt_audio_pcm;
460 qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
462 for (int c = 0; c < m_channels; c++) {
464 m_array.resize(m_arrayWidth);
465 for (int i = 0; i < m_array.size(); i++) {
466 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
468 m_audioThumbFile.write(m_array);
472 m_audioThumbFile.write(QByteArray(m_arrayWidth, '\x00'));
476 m_audioThumbFile.close();
477 if (m_stopAudioThumbs) {
478 m_audioThumbFile.remove();
480 slotAudioThumbOver();
484 void KThumb::slotAudioThumbOver()
486 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
487 m_clipManager->endAudioThumbsGeneration(m_id);
490 void KThumb::askForAudioThumbs(const QString &id)
492 m_clipManager->askForAudioThumb(id);
495 #if KDE_IS_VERSION(4,5,0)
496 void KThumb::queryIntraThumbs(QList <int> missingFrames)
498 foreach (int i, missingFrames) {
499 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
501 qSort(m_intraFramesQueue);
502 if (!m_intra.isRunning()) {
503 m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
507 void KThumb::slotGetIntraThumbs()
509 const int theight = KdenliveSettings::trackheight();
510 const int frameWidth = (int)(theight * m_ratio + 0.5);
511 const int displayWidth = (int)(theight * m_dar + 0.5);
512 QString path = m_url.path() + "_";
513 bool addedThumbs = false;
515 while (!m_intraFramesQueue.isEmpty()) {
516 int pos = m_intraFramesQueue.takeFirst();
517 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
518 if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getProducerFrame(pos, frameWidth, displayWidth, theight))) {
521 else kDebug()<<"// INSERT FAILD FOR: "<<pos;
523 m_intraFramesQueue.removeAll(pos);
525 if (addedThumbs) emit thumbsCached();
528 QImage KThumb::findCachedThumb(const QString path)
531 m_clipManager->pixmapCache->findImage(path, &img);
536 #include "kthumb.moc"