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(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(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 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 memcpy(image.bits(), imagedata, ow * oh * 4);//.byteCount());
223 //const uchar* imagedata = frame->get_image(format, ow, oh);
224 //QImage image(imagedata, ow, oh, QImage::Format_ARGB32_Premultiplied);
226 if (!image.isNull()) {
227 if (ow > (2 * displayWidth)) {
228 // there was a scaling problem, do it manually
229 image = image.scaled(displayWidth, height).rgbSwapped();
231 image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
233 #if QT_VERSION >= 0x040800
234 p.fill(QColor(100, 100, 100, 70));
235 QPainter painter(&p);
237 p.fill(Qt::transparent);
238 QPainter painter(&p);
239 painter.fillRect(p.rect(), QColor(100, 100, 100, 70));
241 painter.drawImage(p.rect(), image);
244 p.fill(QColor(Qt::red).rgb());
249 uint KThumb::imageVariance(QImage image )
253 uint bytes = image.numBytes();
254 uint STEPS = bytes/2;
255 QVarLengthArray<uchar> pivot(STEPS);
256 const uchar *bits=image.bits();
257 // First pass: get pivots and taking average
258 for( uint i=0; i<STEPS ; ++i ){
259 pivot[i] = bits[2 * i];
260 #if QT_VERSION >= 0x040700
268 // Second Step: calculate delta (average?)
269 for (uint i=0; i<STEPS; ++i)
271 #if QT_VERSION >= 0x040700
272 int curdelta=abs(int(avg - pivot.at(i)));
274 int curdelta=abs(int(avg - pivot[i]));
285 void KThumb::getImage(KUrl url, int frame, int width, int height)
287 if (url.isEmpty()) return;
288 QPixmap image(width, height);
289 Mlt::Producer m_producer(url.path().toUtf8().constData());
290 image.fill(Qt::black);
292 if (m_producer.is_blank()) {
293 emit thumbReady(frame, 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(frame);
300 Mlt::Frame * m_frame = m_producer.get_frame();
301 mlt_image_format format = mlt_image_rgb24a;
304 if (m_frame && m_frame->is_valid()) {
305 uint8_t *thumb = m_frame->get_image(format, width, height);
306 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
307 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
309 if (m_frame) delete m_frame;
310 emit thumbReady(frame, image);
313 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
315 if (url.isEmpty()) return;
316 QPixmap image(width, height);
317 Mlt::Producer m_producer(url.path().toUtf8().constData());
318 image.fill(QColor(Qt::black).rgb());
320 if (m_producer.is_blank()) {
321 emit thumbReady(startframe, image);
322 emit thumbReady(endframe, image);
325 Mlt::Filter m_convert("avcolour_space");
326 m_convert.set("forced", mlt_image_rgb24a);
327 m_producer.attach(m_convert);
328 m_producer.seek(startframe);
329 Mlt::Frame * m_frame = m_producer.get_frame();
330 mlt_image_format format = mlt_image_rgb24a;
334 if (m_frame && m_frame->is_valid()) {
335 uint8_t *thumb = m_frame->get_image(format, width, height);
336 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
337 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
339 if (m_frame) delete m_frame;
340 emit thumbReady(startframe, image);
342 image.fill(Qt::black);
343 m_producer.seek(endframe);
344 m_frame = m_producer.get_frame();
346 if (m_frame && m_frame->is_valid()) {
347 uint8_t *thumb = m_frame->get_image(format, width, height);
348 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
349 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
351 if (m_frame) delete m_frame;
352 emit thumbReady(endframe, image);
356 void KThumb::slotCreateAudioThumbs()
358 m_clipManager->askForAudioThumb(m_id);
361 #if KDE_IS_VERSION(4,5,0)
362 void KThumb::queryIntraThumbs(QList <int> missingFrames)
364 foreach (int i, missingFrames) {
365 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
367 qSort(m_intraFramesQueue);
368 if (!m_intra.isRunning()) {
369 m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
373 void KThumb::slotGetIntraThumbs()
375 const int theight = KdenliveSettings::trackheight();
376 const int frameWidth = (int)(theight * m_ratio + 0.5);
377 const int displayWidth = (int)(theight * m_dar + 0.5);
378 QString path = m_url.path() + '_';
379 bool addedThumbs = false;
381 while (!m_intraFramesQueue.isEmpty()) {
382 int pos = m_intraFramesQueue.takeFirst();
383 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
384 if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getProducerFrame(pos, frameWidth, displayWidth, theight))) {
387 else kDebug()<<"// INSERT FAILD FOR: "<<pos;
389 m_intraFramesQueue.removeAll(pos);
391 if (addedThumbs) emit thumbsCached();
394 QImage KThumb::findCachedThumb(const QString &path)
397 m_clipManager->pixmapCache->findImage(path, &img);
402 #include "kthumb.moc"