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>
44 KThumb::KThumb(ClipManager *clipManager, const KUrl &url, const QString &id, const QString &hash, QObject * parent) :
51 m_clipManager(clipManager),
54 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
59 if (m_producer) m_clipManager->stopThumbs(m_id);
61 m_intraFramesQueue.clear();
62 m_intra.waitForFinished();
65 void KThumb::setProducer(Mlt::Producer *producer)
67 if (m_producer) m_clipManager->stopThumbs(m_id);
68 m_intraFramesQueue.clear();
69 m_intra.waitForFinished();
70 QMutexLocker lock(&m_mutex);
71 m_producer = producer;
72 // FIXME: the profile() call leaks an object, but trying to free
73 // it leads to a double-free in Profile::~Profile()
75 Mlt::Profile *profile = producer->profile();
76 m_dar = profile->dar();
77 m_ratio = (double) profile->width() / profile->height();
81 void KThumb::clearProducer()
83 if (m_producer) setProducer(NULL);
86 bool KThumb::hasProducer() const
88 return m_producer != NULL;
91 void KThumb::updateThumbUrl(const QString &hash)
93 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
96 void KThumb::updateClipUrl(const KUrl &url, const QString &hash)
99 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
103 QPixmap KThumb::getImage(const KUrl& url, int width, int height)
105 if (url.isEmpty()) return QPixmap();
106 return getImage(url, 0, width, height);
109 void KThumb::extractImage(const QList<int> &frames)
111 if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
112 m_clipManager->slotRequestThumbs(m_id, frames);
116 void KThumb::getThumb(int frame)
118 const int theight = Kdenlive::DefaultThumbHeight;
119 const int swidth = (int)(theight * m_ratio + 0.5);
120 const int dwidth = (int)(theight * m_dar + 0.5);
121 QImage img = getProducerFrame(frame, swidth, dwidth, theight);
122 emit thumbReady(frame, img);
125 void KThumb::getGenericThumb(int frame, int height, int type)
127 const int swidth = (int)(height * m_ratio + 0.5);
128 const int dwidth = (int)(height * m_dar + 0.5);
129 QImage img = getProducerFrame(frame, swidth, dwidth, height);
130 m_clipManager->projectTreeThumbReady(m_id, frame, img, type);
133 QImage KThumb::extractImage(int frame, int width, int height)
135 if (m_producer == NULL) {
136 QImage img(width, height, QImage::Format_ARGB32_Premultiplied);
137 img.fill(QColor(Qt::black).rgb());
140 return getProducerFrame(frame, (int) (height * m_ratio + 0.5), width, height);
144 QPixmap KThumb::getImage(const 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;
149 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
150 double swidth = (double) profile.width() / profile.height();
151 pix = QPixmap::fromImage(getFrame(producer, frame, (int) (height * swidth + 0.5), width, height));
157 QImage KThumb::getProducerFrame(int framepos, int frameWidth, int displayWidth, int height)
159 if (m_producer == NULL || !m_producer->is_valid()) {
160 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
161 p.fill(QColor(Qt::red).rgb());
164 if (m_producer->is_blank()) {
165 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
166 p.fill(QColor(Qt::black).rgb());
169 QMutexLocker lock(&m_mutex);
170 m_producer->seek(framepos);
171 Mlt::Frame *frame = m_producer->get_frame();
172 /*frame->set("rescale.interp", "nearest");
173 frame->set("deinterlace_method", "onefield");
174 frame->set("top_field_first", -1 );*/
175 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 frame->set("rescale.interp", "nearest");
197 frame->set("deinterlace_method", "onefield");
198 frame->set("top_field_first", -1 );
199 const QImage p = getFrame(frame, frameWidth, displayWidth, height);
206 QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int height)
208 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
209 if (frame == NULL || !frame->is_valid()) {
210 p.fill(QColor(Qt::red).rgb());
216 mlt_image_format format = mlt_image_rgb24a;
217 //frame->set("progressive", "1");
218 if (ow % 2 == 1) ow++;
219 QImage image(ow, oh, QImage::Format_ARGB32_Premultiplied);
220 const uchar* imagedata = frame->get_image(format, ow, oh);
221 if (imagedata == NULL) {
222 p.fill(QColor(Qt::red).rgb());
225 memcpy(image.bits(), imagedata, ow * oh * 4);//.byteCount());
227 //const uchar* imagedata = frame->get_image(format, ow, oh);
228 //QImage image(imagedata, ow, oh, QImage::Format_ARGB32_Premultiplied);
230 if (!image.isNull()) {
231 if (ow > (2 * displayWidth)) {
232 // there was a scaling problem, do it manually
233 image = image.scaled(displayWidth, height).rgbSwapped();
235 image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
237 #if QT_VERSION >= 0x040800
238 p.fill(QColor(100, 100, 100, 70));
239 QPainter painter(&p);
241 p.fill(Qt::transparent);
242 QPainter painter(&p);
243 painter.fillRect(p.rect(), QColor(100, 100, 100, 70));
245 painter.drawImage(p.rect(), image);
248 p.fill(QColor(Qt::red).rgb());
253 uint KThumb::imageVariance(const QImage &image )
257 uint bytes = image.numBytes();
258 uint STEPS = bytes/2;
259 QVarLengthArray<uchar> pivot(STEPS);
260 const uchar *bits=image.bits();
261 // First pass: get pivots and taking average
262 for( uint i=0; i<STEPS ; ++i ){
263 pivot[i] = bits[2 * i];
264 #if QT_VERSION >= 0x040700
272 // Second Step: calculate delta (average?)
273 for (uint i=0; i<STEPS; ++i)
275 #if QT_VERSION >= 0x040700
276 int curdelta=abs(int(avg - pivot.at(i)));
278 int curdelta=abs(int(avg - pivot[i]));
289 void KThumb::getImage(KUrl url, int frame, int width, int height)
291 if (url.isEmpty()) return;
292 QPixmap image(width, height);
293 Mlt::Producer m_producer(url.path().toUtf8().constData());
294 image.fill(Qt::black);
296 if (m_producer.is_blank()) {
297 emit thumbReady(frame, image);
300 Mlt::Filter m_convert("avcolour_space");
301 m_convert.set("forced", mlt_image_rgb24a);
302 m_producer.attach(m_convert);
303 m_producer.seek(frame);
304 Mlt::Frame * m_frame = m_producer.get_frame();
305 mlt_image_format format = mlt_image_rgb24a;
308 if (m_frame && m_frame->is_valid()) {
309 uint8_t *thumb = m_frame->get_image(format, width, height);
310 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
311 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
313 if (m_frame) delete m_frame;
314 emit thumbReady(frame, image);
317 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
319 if (url.isEmpty()) return;
320 QPixmap image(width, height);
321 Mlt::Producer m_producer(url.path().toUtf8().constData());
322 image.fill(QColor(Qt::black).rgb());
324 if (m_producer.is_blank()) {
325 emit thumbReady(startframe, image);
326 emit thumbReady(endframe, image);
329 Mlt::Filter m_convert("avcolour_space");
330 m_convert.set("forced", mlt_image_rgb24a);
331 m_producer.attach(m_convert);
332 m_producer.seek(startframe);
333 Mlt::Frame * m_frame = m_producer.get_frame();
334 mlt_image_format format = mlt_image_rgb24a;
338 if (m_frame && m_frame->is_valid()) {
339 uint8_t *thumb = m_frame->get_image(format, width, height);
340 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
341 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
343 if (m_frame) delete m_frame;
344 emit thumbReady(startframe, image);
346 image.fill(Qt::black);
347 m_producer.seek(endframe);
348 m_frame = m_producer.get_frame();
350 if (m_frame && m_frame->is_valid()) {
351 uint8_t *thumb = m_frame->get_image(format, width, height);
352 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
353 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
355 if (m_frame) delete m_frame;
356 emit thumbReady(endframe, image);
360 void KThumb::slotCreateAudioThumbs()
362 m_clipManager->askForAudioThumb(m_id);
365 #if KDE_IS_VERSION(4,5,0)
366 void KThumb::queryIntraThumbs(const QList <int> &missingFrames)
368 foreach (int i, missingFrames) {
369 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
371 qSort(m_intraFramesQueue);
372 if (!m_intra.isRunning()) {
373 m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
377 void KThumb::slotGetIntraThumbs()
379 const int theight = KdenliveSettings::trackheight();
380 const int frameWidth = (int)(theight * m_ratio + 0.5);
381 const int displayWidth = (int)(theight * m_dar + 0.5);
382 QString path = m_url.path() + '_';
383 bool addedThumbs = false;
385 while (!m_intraFramesQueue.isEmpty()) {
386 int pos = m_intraFramesQueue.takeFirst();
387 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
388 if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getProducerFrame(pos, frameWidth, displayWidth, theight))) {
391 else kDebug()<<"// INSERT FAILD FOR: "<<pos;
393 m_intraFramesQueue.removeAll(pos);
395 if (addedThumbs) emit thumbsCached();
398 QImage KThumb::findCachedThumb(const QString &path)
401 m_clipManager->pixmapCache->findImage(path, &img);
406 #include "kthumb.moc"