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 if (m_producer) m_clipManager->stopThumbs(m_id);
61 m_intraFramesQueue.clear();
62 if (m_audioThumbProducer.isRunning()) {
63 m_stopAudioThumbs = true;
64 m_audioThumbProducer.waitForFinished();
67 m_intra.waitForFinished();
70 void KThumb::setProducer(Mlt::Producer *producer)
72 if (m_producer) m_clipManager->stopThumbs(m_id);
73 m_intraFramesQueue.clear();
74 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()
80 Mlt::Profile *profile = producer->profile();
81 m_dar = profile->dar();
82 m_ratio = (double) profile->width() / profile->height();
87 void KThumb::clearProducer()
89 if (m_producer) setProducer(NULL);
92 bool KThumb::hasProducer() const
94 return m_producer != NULL;
97 void KThumb::updateThumbUrl(const QString &hash)
99 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
102 void KThumb::updateClipUrl(KUrl url, const QString &hash)
105 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
109 QPixmap KThumb::getImage(KUrl url, int width, int height)
111 if (url.isEmpty()) return QPixmap();
112 return getImage(url, 0, width, height);
115 void KThumb::extractImage(QList <int>frames)
117 if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
118 m_clipManager->requestThumbs(m_id, frames);
122 void KThumb::getThumb(int frame)
124 const int theight = KdenliveSettings::trackheight();
125 const int swidth = (int)(theight * m_ratio + 0.5);
126 const int dwidth = (int)(theight * m_dar + 0.5);
128 QImage img = getProducerFrame(frame, swidth, dwidth, theight);
129 emit thumbReady(frame, img);
132 QPixmap KThumb::extractImage(int frame, int width, int height)
134 if (m_producer == NULL) {
135 QPixmap p(width, height);
139 QImage img = getProducerFrame(frame, (int) (height * m_ratio + 0.5), width, height);
140 return QPixmap::fromImage(img);
144 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
146 Mlt::Profile profile(KdenliveSettings::current_profile().toUtf8().constData());
147 QPixmap pix(width, height);
148 if (url.isEmpty()) return pix;
150 //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
151 //Mlt::Producer producer(profile, "xml-string", tmp);
152 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
153 double swidth = (double) profile.width() / profile.height();
154 pix = QPixmap::fromImage(getFrame(producer, frame, (int) (height * swidth + 0.5), width, height));
160 QImage KThumb::getProducerFrame(int framepos, int frameWidth, int displayWidth, int height)
162 if (m_producer == NULL || !m_producer->is_valid()) {
163 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
164 p.fill(QColor(Qt::red).rgb());
167 if (m_producer->is_blank()) {
168 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
169 p.fill(QColor(Qt::black).rgb());
173 m_producer->seek(framepos);
174 Mlt::Frame *frame = m_producer->get_frame();
175 QImage p = getFrame(frame, frameWidth, displayWidth, height);
182 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int frameWidth, int displayWidth, int height)
184 if (producer == NULL || !producer->is_valid()) {
185 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
186 p.fill(QColor(Qt::red).rgb());
189 if (producer->is_blank()) {
190 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
191 p.fill(QColor(Qt::black).rgb());
195 producer->seek(framepos);
196 Mlt::Frame *frame = producer->get_frame();
197 QImage p = getFrame(frame, frameWidth, displayWidth, height);
204 QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int height)
206 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
207 if (frame == NULL || !frame->is_valid()) {
208 p.fill(QColor(Qt::red).rgb());
214 mlt_image_format format = mlt_image_rgb24a;
216 const uchar* imagedata = frame->get_image(format, ow, oh);
217 QImage image(ow, oh, QImage::Format_ARGB32_Premultiplied);
218 memcpy(image.bits(), imagedata, ow * oh * 4);//.byteCount());
220 //const uchar* imagedata = frame->get_image(format, ow, oh);
221 //QImage image(imagedata, ow, oh, QImage::Format_ARGB32_Premultiplied);
223 if (!image.isNull()) {
224 if (ow > (2 * displayWidth)) {
225 // there was a scaling problem, do it manually
226 image = image.scaled(displayWidth, height).rgbSwapped();
228 image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
230 p.fill(QColor(Qt::black).rgb());
231 QPainter painter(&p);
232 painter.drawImage(p.rect(), image);
235 p.fill(QColor(Qt::red).rgb());
240 uint KThumb::imageVariance(QImage image )
244 uint bytes = image.numBytes();
245 uint STEPS = bytes/2;
246 QVarLengthArray<uchar> pivot(STEPS);
247 const uchar *bits=image.bits();
248 // First pass: get pivots and taking average
249 for( uint i=0; i<STEPS ; i++ ){
250 pivot[i] = bits[2 * i];
251 #if QT_VERSION >= 0x040700
258 // Second Step: calculate delta (average?)
259 for (uint i=0; i<STEPS; i++)
261 #if QT_VERSION >= 0x040700
262 int curdelta=abs(int(avg - pivot.at(i)));
264 int curdelta=abs(int(avg - pivot[i]));
272 void KThumb::getImage(KUrl url, int frame, int width, int height)
274 if (url.isEmpty()) return;
275 QPixmap image(width, height);
276 Mlt::Producer m_producer(url.path().toUtf8().constData());
277 image.fill(Qt::black);
279 if (m_producer.is_blank()) {
280 emit thumbReady(frame, image);
283 Mlt::Filter m_convert("avcolour_space");
284 m_convert.set("forced", mlt_image_rgb24a);
285 m_producer.attach(m_convert);
286 m_producer.seek(frame);
287 Mlt::Frame * m_frame = m_producer.get_frame();
288 mlt_image_format format = mlt_image_rgb24a;
291 if (m_frame && m_frame->is_valid()) {
292 uint8_t *thumb = m_frame->get_image(format, width, height);
293 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
294 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
296 if (m_frame) delete m_frame;
297 emit thumbReady(frame, image);
300 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
302 if (url.isEmpty()) return;
303 QPixmap image(width, height);
304 Mlt::Producer m_producer(url.path().toUtf8().constData());
305 image.fill(Qt::black);
307 if (m_producer.is_blank()) {
308 emit thumbReady(startframe, image);
309 emit thumbReady(endframe, image);
312 Mlt::Filter m_convert("avcolour_space");
313 m_convert.set("forced", mlt_image_rgb24a);
314 m_producer.attach(m_convert);
315 m_producer.seek(startframe);
316 Mlt::Frame * m_frame = m_producer.get_frame();
317 mlt_image_format format = mlt_image_rgb24a;
321 if (m_frame && m_frame->is_valid()) {
322 uint8_t *thumb = m_frame->get_image(format, width, height);
323 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
324 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
326 if (m_frame) delete m_frame;
327 emit thumbReady(startframe, image);
329 image.fill(Qt::black);
330 m_producer.seek(endframe);
331 m_frame = m_producer.get_frame();
333 if (m_frame && m_frame->is_valid()) {
334 uint8_t *thumb = m_frame->get_image(format, width, height);
335 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
336 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
338 if (m_frame) delete m_frame;
339 emit thumbReady(endframe, image);
342 void KThumb::stopAudioThumbs()
344 if (m_audioThumbProducer.isRunning()) {
345 m_stopAudioThumbs = true;
346 m_audioThumbProducer.waitForFinished();
347 slotAudioThumbOver();
351 void KThumb::removeAudioThumb()
353 if (m_thumbFile.isEmpty()) return;
355 QFile f(m_thumbFile);
359 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
362 slotAudioThumbOver();
365 if (m_audioThumbProducer.isRunning()) {
369 audioByteArray storeIn;
370 //FIXME: Hardcoded!!!
372 m_channels = channel;
374 QFile f(m_thumbFile);
375 if (f.open(QIODevice::ReadOnly)) {
376 const QByteArray channelarray = f.readAll();
378 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
379 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
381 slotAudioThumbOver();
385 kDebug() << "reading audio thumbs from file";
387 int h1 = arrayWidth * m_channels;
388 int h2 = (int) frame * h1;
390 for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
392 for (int c = 0; c < m_channels; c++) {
393 QByteArray m_array(arrayWidth, '\x00');
394 for (int i = 0; i < arrayWidth; i++) {
395 m_array[i] = channelarray.at(h2 + h3 + i);
398 storeIn[z][c] = m_array;
402 emit audioThumbReady(storeIn);
403 slotAudioThumbOver();
405 if (m_audioThumbProducer.isRunning()) return;
406 m_audioThumbFile.setFileName(m_thumbFile);
408 m_frameLength = frameLength;
409 m_arrayWidth = arrayWidth;
410 m_audioThumbProducer = QtConcurrent::run(this, &KThumb::slotCreateAudioThumbs);
411 /*m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
412 m_audioThumbProducer.start(QThread::LowestPriority);*/
413 // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
417 void KThumb::slotCreateAudioThumbs()
419 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().constData());
420 Mlt::Producer producer(prof, m_url.path().toUtf8().constData());
421 if (!producer.is_valid()) {
422 kDebug() << "++++++++ INVALID CLIP: " << m_url.path();
425 if (!m_audioThumbFile.open(QIODevice::WriteOnly)) {
426 kDebug() << "++++++++ ERROR WRITING TO FILE: " << m_audioThumbFile.fileName();
427 kDebug() << "++++++++ DISABLING AUDIO THUMBS";
428 KdenliveSettings::setAudiothumbnails(false);
432 if (KdenliveSettings::normaliseaudiothumbs()) {
433 Mlt::Filter m_convert(prof, "volume");
434 m_convert.set("gain", "normalise");
435 producer.attach(m_convert);
440 mlt_audio_format m_audioFormat = mlt_audio_pcm;
441 double framesPerSecond = mlt_producer_get_fps(producer.get_producer());
442 Mlt::Frame *mlt_frame;
444 for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && producer.is_valid(); z++) {
445 if (m_stopAudioThumbs) break;
446 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
447 if (last_val != val && val > 1) {
448 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), val);
452 mlt_frame = producer.get_frame();
453 if (mlt_frame && mlt_frame->is_valid()) {
454 int m_samples = mlt_sample_calculator(framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
455 qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
456 for (int c = 0; c < m_channels; c++) {
458 m_array.resize(m_arrayWidth);
459 for (int i = 0; i < m_array.size(); i++) {
460 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
462 m_audioThumbFile.write(m_array);
466 m_audioThumbFile.write(QByteArray(m_arrayWidth, '\x00'));
470 m_audioThumbFile.close();
471 if (m_stopAudioThumbs) {
472 m_audioThumbFile.remove();
474 slotAudioThumbOver();
478 void KThumb::slotAudioThumbOver()
480 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
481 m_clipManager->endAudioThumbsGeneration(m_id);
484 void KThumb::askForAudioThumbs(const QString &id)
486 m_clipManager->askForAudioThumb(id);
489 #if KDE_IS_VERSION(4,5,0)
490 void KThumb::queryIntraThumbs(QList <int> missingFrames)
492 foreach (int i, missingFrames) {
493 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
495 qSort(m_intraFramesQueue);
496 if (!m_intra.isRunning()) {
497 m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
501 void KThumb::slotGetIntraThumbs()
503 const int theight = KdenliveSettings::trackheight();
504 const int frameWidth = (int)(theight * m_ratio + 0.5);
505 const int displayWidth = (int)(theight * m_dar + 0.5);
506 QString path = m_url.path() + "_";
507 bool addedThumbs = false;
509 while (!m_intraFramesQueue.isEmpty()) {
510 int pos = m_intraFramesQueue.takeFirst();
511 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
512 if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getProducerFrame(pos, frameWidth, displayWidth, theight))) {
515 else kDebug()<<"// INSERT FAILD FOR: "<<pos;
517 m_intraFramesQueue.removeAll(pos);
519 if (addedThumbs) emit thumbsCached();
522 QImage KThumb::findCachedThumb(const QString &path)
525 m_clipManager->pixmapCache->findImage(path, &img);
530 #include "kthumb.moc"