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)
74 m_requestedThumbs.clear();
75 m_intraFramesQueue.clear();
76 m_future.waitForFinished();
77 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)
107 //m_producer->set("resource", url.path().toUtf8().constData());
108 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
112 QPixmap KThumb::getImage(KUrl url, int width, int height)
114 if (url.isEmpty()) return QPixmap();
115 return getImage(url, 0, width, height);
118 void KThumb::extractImage(int frame, int frame2)
120 if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
121 if (frame != -1 && !m_requestedThumbs.contains(frame)) m_requestedThumbs.append(frame);
122 if (frame2 != -1 && !m_requestedThumbs.contains(frame2)) m_requestedThumbs.append(frame2);
123 qSort(m_requestedThumbs);
124 if (!m_future.isRunning()) {
126 m_future = QtConcurrent::run(this, &KThumb::doGetThumbs);
131 void KThumb::doGetThumbs()
133 const int theight = KdenliveSettings::trackheight();
134 const int swidth = (int)(theight * m_ratio + 0.5);
135 const int dwidth = (int)(theight * m_dar + 0.5);
137 while (!m_requestedThumbs.isEmpty()) {
138 int frame = m_requestedThumbs.takeFirst();
140 emit thumbReady(frame, getFrame(m_producer, frame, swidth, dwidth, theight));
145 QPixmap KThumb::extractImage(int frame, int width, int height)
148 QImage img = getFrame(m_producer, 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::getFrame(Mlt::Producer *producer, int framepos, int frameWidth, int displayWidth, int height)
172 if (producer == NULL || !producer->is_valid()) {
173 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
174 p.fill(QColor(Qt::red).rgb());
178 if (producer->is_blank()) {
179 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
180 p.fill(QColor(Qt::black).rgb());
184 producer->seek(framepos);
185 Mlt::Frame *frame = producer->get_frame();
186 QImage p = getFrame(frame, frameWidth, displayWidth, height);
193 QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int height)
195 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
196 if (frame == NULL || !frame->is_valid()) {
197 p.fill(QColor(Qt::red).rgb());
203 mlt_image_format format = mlt_image_rgb24a;
204 uint8_t *data = frame->get_image(format, ow, oh, 0);
205 QImage image((uchar *)data, ow, oh, QImage::Format_ARGB32_Premultiplied);
206 if (!image.isNull()) {
207 if (ow > (2 * displayWidth)) {
208 // there was a scaling problem, do it manually
209 QImage scaled = image.scaled(displayWidth, height);
210 image = scaled.rgbSwapped();
212 image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
214 p.fill(QColor(Qt::black).rgb());
215 QPainter painter(&p);
216 painter.drawImage(p.rect(), image);
219 p.fill(QColor(Qt::red).rgb());
224 uint KThumb::imageVariance(QImage image )
228 uint bytes = image.numBytes();
229 uint STEPS = bytes/2;
230 QVarLengthArray<uchar> pivot(STEPS);
231 const uchar *bits=image.bits();
232 // First pass: get pivots and taking average
233 for( uint i=0; i<STEPS ; i++ ){
234 pivot[i] = bits[2 * i];
235 #if QT_VERSION >= 0x040700
242 // Second Step: calculate delta (average?)
243 for (uint i=0; i<STEPS; i++)
245 #if QT_VERSION >= 0x040700
246 int curdelta=abs(int(avg - pivot.at(i)));
248 int curdelta=abs(int(avg - pivot[i]));
256 void KThumb::getImage(KUrl url, int frame, int width, int height)
258 if (url.isEmpty()) return;
259 QPixmap image(width, height);
260 Mlt::Producer m_producer(url.path().toUtf8().constData());
261 image.fill(Qt::black);
263 if (m_producer.is_blank()) {
264 emit thumbReady(frame, image);
267 Mlt::Filter m_convert("avcolour_space");
268 m_convert.set("forced", mlt_image_rgb24a);
269 m_producer.attach(m_convert);
270 m_producer.seek(frame);
271 Mlt::Frame * m_frame = m_producer.get_frame();
272 mlt_image_format format = mlt_image_rgb24a;
275 if (m_frame && m_frame->is_valid()) {
276 uint8_t *thumb = m_frame->get_image(format, width, height);
277 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
278 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
280 if (m_frame) delete m_frame;
281 emit thumbReady(frame, image);
284 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
286 if (url.isEmpty()) return;
287 QPixmap image(width, height);
288 Mlt::Producer m_producer(url.path().toUtf8().constData());
289 image.fill(Qt::black);
291 if (m_producer.is_blank()) {
292 emit thumbReady(startframe, image);
293 emit thumbReady(endframe, image);
296 Mlt::Filter m_convert("avcolour_space");
297 m_convert.set("forced", mlt_image_rgb24a);
298 m_producer.attach(m_convert);
299 m_producer.seek(startframe);
300 Mlt::Frame * m_frame = m_producer.get_frame();
301 mlt_image_format format = mlt_image_rgb24a;
305 if (m_frame && m_frame->is_valid()) {
306 uint8_t *thumb = m_frame->get_image(format, width, height);
307 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
308 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
310 if (m_frame) delete m_frame;
311 emit thumbReady(startframe, image);
313 image.fill(Qt::black);
314 m_producer.seek(endframe);
315 m_frame = m_producer.get_frame();
317 if (m_frame && m_frame->is_valid()) {
318 uint8_t *thumb = m_frame->get_image(format, width, height);
319 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
320 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
322 if (m_frame) delete m_frame;
323 emit thumbReady(endframe, image);
326 void KThumb::stopAudioThumbs()
328 if (m_audioThumbProducer.isRunning()) {
329 m_stopAudioThumbs = true;
330 m_audioThumbProducer.waitForFinished();
331 slotAudioThumbOver();
335 void KThumb::removeAudioThumb()
337 if (m_thumbFile.isEmpty()) return;
339 QFile f(m_thumbFile);
343 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
346 slotAudioThumbOver();
349 if (m_audioThumbProducer.isRunning()) {
353 audioByteArray storeIn;
354 //FIXME: Hardcoded!!!
356 m_channels = channel;
358 QFile f(m_thumbFile);
359 if (f.open(QIODevice::ReadOnly)) {
360 const QByteArray channelarray = f.readAll();
362 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
363 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
365 slotAudioThumbOver();
369 kDebug() << "reading audio thumbs from file";
371 int h1 = arrayWidth * m_channels;
372 int h2 = (int) frame * h1;
374 for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
376 for (int c = 0; c < m_channels; c++) {
377 QByteArray m_array(arrayWidth, '\x00');
378 for (int i = 0; i < arrayWidth; i++) {
379 m_array[i] = channelarray.at(h2 + h3 + i);
382 storeIn[z][c] = m_array;
386 emit audioThumbReady(storeIn);
387 slotAudioThumbOver();
389 if (m_audioThumbProducer.isRunning()) return;
390 m_audioThumbFile.setFileName(m_thumbFile);
392 m_frameLength = frameLength;
393 m_arrayWidth = arrayWidth;
394 m_audioThumbProducer = QtConcurrent::run(this, &KThumb::slotCreateAudioThumbs);
395 /*m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
396 m_audioThumbProducer.start(QThread::LowestPriority);*/
397 // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
401 void KThumb::slotCreateAudioThumbs()
403 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
404 Mlt::Producer producer(prof, m_url.path().toUtf8().data());
405 if (!producer.is_valid()) {
406 kDebug() << "++++++++ INVALID CLIP: " << m_url.path();
409 if (!m_audioThumbFile.open(QIODevice::WriteOnly)) {
410 kDebug() << "++++++++ ERROR WRITING TO FILE: " << m_audioThumbFile.fileName();
411 kDebug() << "++++++++ DISABLING AUDIO THUMBS";
412 KdenliveSettings::setAudiothumbnails(false);
416 if (KdenliveSettings::normaliseaudiothumbs()) {
417 Mlt::Filter m_convert(prof, "volume");
418 m_convert.set("gain", "normalise");
419 producer.attach(m_convert);
424 //kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
425 for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && producer.is_valid(); z++) {
426 if (m_stopAudioThumbs) break;
427 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
428 if (last_val != val && val > 1) {
429 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), val);
433 Mlt::Frame *mlt_frame = producer.get_frame();
434 if (mlt_frame && mlt_frame->is_valid()) {
435 double m_framesPerSecond = mlt_producer_get_fps(producer.get_producer());
436 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
437 mlt_audio_format m_audioFormat = mlt_audio_pcm;
438 qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
440 for (int c = 0; c < m_channels; c++) {
442 m_array.resize(m_arrayWidth);
443 for (int i = 0; i < m_array.size(); i++) {
444 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
446 m_audioThumbFile.write(m_array);
450 m_audioThumbFile.write(QByteArray(m_arrayWidth, '\x00'));
454 m_audioThumbFile.close();
455 if (m_stopAudioThumbs) {
456 m_audioThumbFile.remove();
458 slotAudioThumbOver();
462 void KThumb::slotAudioThumbOver()
464 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
465 m_clipManager->endAudioThumbsGeneration(m_id);
468 void KThumb::askForAudioThumbs(const QString &id)
470 m_clipManager->askForAudioThumb(id);
473 #if KDE_IS_VERSION(4,5,0)
474 void KThumb::queryIntraThumbs(QList <int> missingFrames)
476 foreach (int i, missingFrames) {
477 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
479 qSort(m_intraFramesQueue);
480 if (!m_intra.isRunning()) {
482 m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
487 void KThumb::slotGetIntraThumbs()
489 const int theight = KdenliveSettings::trackheight();
490 const int frameWidth = (int)(theight * m_ratio + 0.5);
491 const int displayWidth = (int)(theight * m_dar + 0.5);
492 QString path = m_url.path() + "_";
493 bool addedThumbs = false;
495 while (!m_intraFramesQueue.isEmpty()) {
496 int pos = m_intraFramesQueue.takeFirst();
497 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
498 if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getFrame(m_producer, pos, frameWidth, displayWidth, theight))) {
501 else kDebug()<<"// INSERT FAILD FOR: "<<pos;
503 m_intraFramesQueue.removeAll(pos);
505 if (addedThumbs) emit thumbsCached();
508 QImage KThumb::findCachedThumb(const QString path)
511 m_clipManager->pixmapCache->findImage(path, &img);
516 #include "kthumb.moc"