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_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 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 m_dar = producer->profile()->dar();
81 m_ratio = (double) producer->profile()->width() / producer->profile()->height();
86 void KThumb::clearProducer()
91 bool KThumb::hasProducer() const
93 return m_producer != NULL;
96 void KThumb::updateThumbUrl(const QString &hash)
98 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
101 void KThumb::updateClipUrl(KUrl url, const QString &hash)
104 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
108 QPixmap KThumb::getImage(KUrl url, int width, int height)
110 if (url.isEmpty()) return QPixmap();
111 return getImage(url, 0, width, height);
114 void KThumb::extractImage(QList <int>frames)
116 if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
117 m_clipManager->requestThumbs(m_id, frames);
121 void KThumb::getThumb(int frame)
123 const int theight = KdenliveSettings::trackheight();
124 const int swidth = (int)(theight * m_ratio + 0.5);
125 const int dwidth = (int)(theight * m_dar + 0.5);
127 QImage img = getProducerFrame(frame, swidth, dwidth, theight);
128 emit thumbReady(frame, img);
131 QPixmap KThumb::extractImage(int frame, int width, int height)
133 if (m_producer == NULL) {
134 QPixmap p(width, height);
138 QImage img = getProducerFrame(frame, (int) (height * m_ratio + 0.5), width, height);
139 return QPixmap::fromImage(img);
143 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
145 Mlt::Profile profile(KdenliveSettings::current_profile().toUtf8().constData());
146 QPixmap pix(width, height);
147 if (url.isEmpty()) return pix;
149 //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
150 //Mlt::Producer producer(profile, "xml-string", tmp);
151 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
152 double swidth = (double) profile.width() / profile.height();
153 pix = QPixmap::fromImage(getFrame(producer, frame, (int) (height * swidth + 0.5), width, height));
159 QImage KThumb::getProducerFrame(int framepos, int frameWidth, int displayWidth, int height)
161 if (m_producer == NULL || !m_producer->is_valid()) {
162 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
163 p.fill(QColor(Qt::red).rgb());
166 if (m_producer->is_blank()) {
167 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
168 p.fill(QColor(Qt::black).rgb());
172 m_producer->seek(framepos);
173 Mlt::Frame *frame = m_producer->get_frame();
174 QImage p = getFrame(frame, frameWidth, displayWidth, height);
181 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int frameWidth, int displayWidth, int height)
183 if (producer == NULL || !producer->is_valid()) {
184 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
185 p.fill(QColor(Qt::red).rgb());
188 if (producer->is_blank()) {
189 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
190 p.fill(QColor(Qt::black).rgb());
194 producer->seek(framepos);
195 Mlt::Frame *frame = producer->get_frame();
196 QImage p = getFrame(frame, frameWidth, displayWidth, height);
203 QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int height)
205 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
206 if (frame == NULL || !frame->is_valid()) {
207 p.fill(QColor(Qt::red).rgb());
213 mlt_image_format format = mlt_image_rgb24a;
215 const uchar* imagedata = frame->get_image(format, ow, oh);
216 QImage image(ow, oh, QImage::Format_ARGB32_Premultiplied);
217 memcpy(image.bits(), imagedata, ow * oh * 4);//.byteCount());
219 //const uchar* imagedata = frame->get_image(format, ow, oh);
220 //QImage image(imagedata, ow, oh, QImage::Format_ARGB32_Premultiplied);
222 if (!image.isNull()) {
223 if (ow > (2 * displayWidth)) {
224 // there was a scaling problem, do it manually
225 image = image.scaled(displayWidth, height).rgbSwapped();
227 image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
229 p.fill(QColor(Qt::black).rgb());
230 QPainter painter(&p);
231 painter.drawImage(p.rect(), image);
234 p.fill(QColor(Qt::red).rgb());
239 uint KThumb::imageVariance(QImage image )
243 uint bytes = image.numBytes();
244 uint STEPS = bytes/2;
245 QVarLengthArray<uchar> pivot(STEPS);
246 const uchar *bits=image.bits();
247 // First pass: get pivots and taking average
248 for( uint i=0; i<STEPS ; i++ ){
249 pivot[i] = bits[2 * i];
250 #if QT_VERSION >= 0x040700
257 // Second Step: calculate delta (average?)
258 for (uint i=0; i<STEPS; i++)
260 #if QT_VERSION >= 0x040700
261 int curdelta=abs(int(avg - pivot.at(i)));
263 int curdelta=abs(int(avg - pivot[i]));
271 void KThumb::getImage(KUrl url, int frame, int width, int height)
273 if (url.isEmpty()) return;
274 QPixmap image(width, height);
275 Mlt::Producer m_producer(url.path().toUtf8().constData());
276 image.fill(Qt::black);
278 if (m_producer.is_blank()) {
279 emit thumbReady(frame, image);
282 Mlt::Filter m_convert("avcolour_space");
283 m_convert.set("forced", mlt_image_rgb24a);
284 m_producer.attach(m_convert);
285 m_producer.seek(frame);
286 Mlt::Frame * m_frame = m_producer.get_frame();
287 mlt_image_format format = mlt_image_rgb24a;
290 if (m_frame && m_frame->is_valid()) {
291 uint8_t *thumb = m_frame->get_image(format, width, height);
292 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
293 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
295 if (m_frame) delete m_frame;
296 emit thumbReady(frame, image);
299 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
301 if (url.isEmpty()) return;
302 QPixmap image(width, height);
303 Mlt::Producer m_producer(url.path().toUtf8().constData());
304 image.fill(Qt::black);
306 if (m_producer.is_blank()) {
307 emit thumbReady(startframe, image);
308 emit thumbReady(endframe, image);
311 Mlt::Filter m_convert("avcolour_space");
312 m_convert.set("forced", mlt_image_rgb24a);
313 m_producer.attach(m_convert);
314 m_producer.seek(startframe);
315 Mlt::Frame * m_frame = m_producer.get_frame();
316 mlt_image_format format = mlt_image_rgb24a;
320 if (m_frame && m_frame->is_valid()) {
321 uint8_t *thumb = m_frame->get_image(format, width, height);
322 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
323 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
325 if (m_frame) delete m_frame;
326 emit thumbReady(startframe, image);
328 image.fill(Qt::black);
329 m_producer.seek(endframe);
330 m_frame = m_producer.get_frame();
332 if (m_frame && m_frame->is_valid()) {
333 uint8_t *thumb = m_frame->get_image(format, width, height);
334 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
335 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
337 if (m_frame) delete m_frame;
338 emit thumbReady(endframe, image);
341 void KThumb::stopAudioThumbs()
343 if (m_audioThumbProducer.isRunning()) {
344 m_stopAudioThumbs = true;
345 m_audioThumbProducer.waitForFinished();
346 slotAudioThumbOver();
350 void KThumb::removeAudioThumb()
352 if (m_thumbFile.isEmpty()) return;
354 QFile f(m_thumbFile);
358 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
361 slotAudioThumbOver();
364 if (m_audioThumbProducer.isRunning()) {
368 audioByteArray storeIn;
369 //FIXME: Hardcoded!!!
371 m_channels = channel;
373 QFile f(m_thumbFile);
374 if (f.open(QIODevice::ReadOnly)) {
375 const QByteArray channelarray = f.readAll();
377 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
378 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
380 slotAudioThumbOver();
384 kDebug() << "reading audio thumbs from file";
386 int h1 = arrayWidth * m_channels;
387 int h2 = (int) frame * h1;
389 for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
391 for (int c = 0; c < m_channels; c++) {
392 QByteArray m_array(arrayWidth, '\x00');
393 for (int i = 0; i < arrayWidth; i++) {
394 m_array[i] = channelarray.at(h2 + h3 + i);
397 storeIn[z][c] = m_array;
401 emit audioThumbReady(storeIn);
402 slotAudioThumbOver();
404 if (m_audioThumbProducer.isRunning()) return;
405 m_audioThumbFile.setFileName(m_thumbFile);
407 m_frameLength = frameLength;
408 m_arrayWidth = arrayWidth;
409 m_audioThumbProducer = QtConcurrent::run(this, &KThumb::slotCreateAudioThumbs);
410 /*m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
411 m_audioThumbProducer.start(QThread::LowestPriority);*/
412 // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
416 void KThumb::slotCreateAudioThumbs()
418 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().constData());
419 Mlt::Producer producer(prof, m_url.path().toUtf8().constData());
420 if (!producer.is_valid()) {
421 kDebug() << "++++++++ INVALID CLIP: " << m_url.path();
424 if (!m_audioThumbFile.open(QIODevice::WriteOnly)) {
425 kDebug() << "++++++++ ERROR WRITING TO FILE: " << m_audioThumbFile.fileName();
426 kDebug() << "++++++++ DISABLING AUDIO THUMBS";
427 KdenliveSettings::setAudiothumbnails(false);
431 if (KdenliveSettings::normaliseaudiothumbs()) {
432 Mlt::Filter m_convert(prof, "volume");
433 m_convert.set("gain", "normalise");
434 producer.attach(m_convert);
439 mlt_audio_format m_audioFormat = mlt_audio_pcm;
440 double framesPerSecond = mlt_producer_get_fps(producer.get_producer());
441 Mlt::Frame *mlt_frame;
443 for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && producer.is_valid(); z++) {
444 if (m_stopAudioThumbs) break;
445 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
446 if (last_val != val && val > 1) {
447 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), val);
451 mlt_frame = producer.get_frame();
452 if (mlt_frame && mlt_frame->is_valid()) {
453 int m_samples = mlt_sample_calculator(framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
454 qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
455 for (int c = 0; c < m_channels; c++) {
457 m_array.resize(m_arrayWidth);
458 for (int i = 0; i < m_array.size(); i++) {
459 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
461 m_audioThumbFile.write(m_array);
465 m_audioThumbFile.write(QByteArray(m_arrayWidth, '\x00'));
469 m_audioThumbFile.close();
470 if (m_stopAudioThumbs) {
471 m_audioThumbFile.remove();
473 slotAudioThumbOver();
477 void KThumb::slotAudioThumbOver()
479 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
480 m_clipManager->endAudioThumbsGeneration(m_id);
483 void KThumb::askForAudioThumbs(const QString &id)
485 m_clipManager->askForAudioThumb(id);
488 #if KDE_IS_VERSION(4,5,0)
489 void KThumb::queryIntraThumbs(QList <int> missingFrames)
491 foreach (int i, missingFrames) {
492 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
494 qSort(m_intraFramesQueue);
495 if (!m_intra.isRunning()) {
496 m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
500 void KThumb::slotGetIntraThumbs()
502 const int theight = KdenliveSettings::trackheight();
503 const int frameWidth = (int)(theight * m_ratio + 0.5);
504 const int displayWidth = (int)(theight * m_dar + 0.5);
505 QString path = m_url.path() + "_";
506 bool addedThumbs = false;
508 while (!m_intraFramesQueue.isEmpty()) {
509 int pos = m_intraFramesQueue.takeFirst();
510 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
511 if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getProducerFrame(pos, frameWidth, displayWidth, theight))) {
514 else kDebug()<<"// INSERT FAILD FOR: "<<pos;
516 m_intraFramesQueue.removeAll(pos);
518 if (addedThumbs) emit thumbsCached();
521 QImage KThumb::findCachedThumb(const QString path)
524 m_clipManager->pixmapCache->findImage(path, &img);
529 #include "kthumb.moc"