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";
177 /*Mlt::Producer parentProd(producer->parent());
178 Mlt::Service service(parentProd.get_service());
179 mlt_service_lock(service.get_service());*/
182 mlt_image_format format = mlt_image_rgb24a;
183 uint8_t *data = frame->get_image(format, ow, oh, 0);
184 QImage image((uchar *)data, ow, oh, QImage::Format_ARGB32_Premultiplied);
185 //mlt_service_unlock(service.get_service());
187 if (!image.isNull()) {
188 if (ow > (2 * width)) {
189 // there was a scaling problem, do it manually
190 QImage scaled = image.scaled(width, height);
191 p = scaled.rgbSwapped();
192 } else p = image.rgbSwapped();
202 uint KThumb::imageVariance(QImage image )
206 uint bytes = image.numBytes();
207 uint STEPS = bytes/2;
208 QVarLengthArray<uchar> pivot(STEPS);
209 const uchar *bits=image.bits();
210 // First pass: get pivots and taking average
211 for( uint i=0; i<STEPS ; i++ ){
212 pivot[i] = bits[2 * i];
213 #if QT_VERSION >= 0x040700
220 // Second Step: calculate delta (average?)
221 for (uint i=0; i<STEPS; i++)
223 #if QT_VERSION >= 0x040700
224 int curdelta=abs(int(avg - pivot.at(i)));
226 int curdelta=abs(int(avg - pivot[i]));
234 void KThumb::getImage(KUrl url, int frame, int width, int height)
236 if (url.isEmpty()) return;
237 QPixmap image(width, height);
238 Mlt::Producer m_producer(url.path().toUtf8().constData());
239 image.fill(Qt::black);
241 if (m_producer.is_blank()) {
242 emit thumbReady(frame, image);
245 Mlt::Filter m_convert("avcolour_space");
246 m_convert.set("forced", mlt_image_rgb24a);
247 m_producer.attach(m_convert);
248 m_producer.seek(frame);
249 Mlt::Frame * m_frame = m_producer.get_frame();
250 mlt_image_format format = mlt_image_rgb24a;
253 if (m_frame && m_frame->is_valid()) {
254 uint8_t *thumb = m_frame->get_image(format, width, height);
255 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
256 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
258 if (m_frame) delete m_frame;
259 emit thumbReady(frame, image);
262 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
264 if (url.isEmpty()) return;
265 QPixmap image(width, height);
266 Mlt::Producer m_producer(url.path().toUtf8().constData());
267 image.fill(Qt::black);
269 if (m_producer.is_blank()) {
270 emit thumbReady(startframe, image);
271 emit thumbReady(endframe, image);
274 Mlt::Filter m_convert("avcolour_space");
275 m_convert.set("forced", mlt_image_rgb24a);
276 m_producer.attach(m_convert);
277 m_producer.seek(startframe);
278 Mlt::Frame * m_frame = m_producer.get_frame();
279 mlt_image_format format = mlt_image_rgb24a;
283 if (m_frame && m_frame->is_valid()) {
284 uint8_t *thumb = m_frame->get_image(format, width, height);
285 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
286 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
288 if (m_frame) delete m_frame;
289 emit thumbReady(startframe, image);
291 image.fill(Qt::black);
292 m_producer.seek(endframe);
293 m_frame = m_producer.get_frame();
295 if (m_frame && m_frame->is_valid()) {
296 uint8_t *thumb = m_frame->get_image(format, width, height);
297 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
298 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
300 if (m_frame) delete m_frame;
301 emit thumbReady(endframe, image);
304 void KThumb::stopAudioThumbs()
306 if (m_audioThumbProducer.isRunning()) {
307 m_stopAudioThumbs = true;
308 m_audioThumbProducer.waitForFinished();
309 slotAudioThumbOver();
313 void KThumb::removeAudioThumb()
315 if (m_thumbFile.isEmpty()) return;
317 QFile f(m_thumbFile);
321 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
324 slotAudioThumbOver();
327 if (m_audioThumbProducer.isRunning()) {
331 QMap <int, QMap <int, QByteArray> > storeIn;
332 //FIXME: Hardcoded!!!
334 m_channels = channel;
336 QFile f(m_thumbFile);
337 if (f.open(QIODevice::ReadOnly)) {
338 const QByteArray channelarray = f.readAll();
340 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
341 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
343 slotAudioThumbOver();
347 kDebug() << "reading audio thumbs from file";
349 int h1 = arrayWidth * m_channels;
350 int h2 = (int) frame * h1;
352 for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
354 for (int c = 0; c < m_channels; c++) {
355 QByteArray m_array(arrayWidth, '\x00');
356 for (int i = 0; i < arrayWidth; i++) {
357 m_array[i] = channelarray.at(h2 + h3 + i);
360 storeIn[z][c] = m_array;
364 emit audioThumbReady(storeIn);
365 slotAudioThumbOver();
367 if (m_audioThumbProducer.isRunning()) return;
368 m_audioThumbFile.setFileName(m_thumbFile);
370 m_frameLength = frameLength;
371 m_arrayWidth = arrayWidth;
372 m_audioThumbProducer = QtConcurrent::run(this, &KThumb::slotCreateAudioThumbs);
373 /*m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
374 m_audioThumbProducer.start(QThread::LowestPriority);*/
375 // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
379 void KThumb::slotCreateAudioThumbs()
381 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
382 Mlt::Producer producer(prof, m_url.path().toUtf8().data());
383 if (!producer.is_valid()) {
384 kDebug() << "++++++++ INVALID CLIP: " << m_url.path();
387 if (!m_audioThumbFile.open(QIODevice::WriteOnly)) {
388 kDebug() << "++++++++ ERROR WRITING TO FILE: " << m_audioThumbFile.fileName();
389 kDebug() << "++++++++ DISABLING AUDIO THUMBS";
390 KdenliveSettings::setAudiothumbnails(false);
394 if (KdenliveSettings::normaliseaudiothumbs()) {
395 Mlt::Filter m_convert(prof, "volume");
396 m_convert.set("gain", "normalise");
397 producer.attach(m_convert);
402 //kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
403 for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && producer.is_valid(); z++) {
404 if (m_stopAudioThumbs) break;
405 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
406 if (last_val != val && val > 1) {
407 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), val);
411 Mlt::Frame *mlt_frame = producer.get_frame();
412 if (mlt_frame && mlt_frame->is_valid()) {
413 double m_framesPerSecond = mlt_producer_get_fps(producer.get_producer());
414 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
415 mlt_audio_format m_audioFormat = mlt_audio_pcm;
416 qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
418 for (int c = 0; c < m_channels; c++) {
420 m_array.resize(m_arrayWidth);
421 for (int i = 0; i < m_array.size(); i++) {
422 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
424 m_audioThumbFile.write(m_array);
428 m_audioThumbFile.write(QByteArray(m_arrayWidth, '\x00'));
432 m_audioThumbFile.close();
433 if (m_stopAudioThumbs) {
434 m_audioThumbFile.remove();
436 slotAudioThumbOver();
440 void KThumb::slotAudioThumbOver()
442 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
443 m_clipManager->endAudioThumbsGeneration(m_id);
446 void KThumb::askForAudioThumbs(const QString &id)
448 m_clipManager->askForAudioThumb(id);
451 #if KDE_IS_VERSION(4,5,0)
452 void KThumb::queryIntraThumbs(int start, int end)
454 for (int i = start; i <= end; i++) {
455 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
457 qSort(m_intraFramesQueue);
458 if (!m_intra.isRunning()) m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
461 void KThumb::slotGetIntraThumbs()
463 int theight = KdenliveSettings::trackheight();
464 int twidth = FRAME_SIZE;
465 QString path = m_url.path() + "%";
468 while (!m_intraFramesQueue.isEmpty()) {
469 int pos = m_intraFramesQueue.takeFirst();
470 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
471 m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getFrame(m_producer, pos, twidth, theight));
473 m_intraFramesQueue.removeAll(pos);
478 QImage KThumb::findCachedThumb(const QString path)
481 m_clipManager->pixmapCache->findImage(path, &img);
486 #include "kthumb.moc"