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*/) :
50 m_clipManager(clipManager),
53 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
58 if (m_producer) m_clipManager->stopThumbs(m_id);
59 m_intraFramesQueue.clear();
60 m_intra.waitForFinished();
63 void KThumb::setProducer(Mlt::Producer *producer)
65 if (m_producer) m_clipManager->stopThumbs(m_id);
66 m_intraFramesQueue.clear();
67 m_intra.waitForFinished();
69 m_producer = producer;
70 // FIXME: the profile() call leaks an object, but trying to free
71 // it leads to a double-free in Profile::~Profile()
73 Mlt::Profile *profile = producer->profile();
74 m_dar = profile->dar();
75 m_ratio = (double) profile->width() / profile->height();
80 void KThumb::clearProducer()
82 if (m_producer) setProducer(NULL);
85 bool KThumb::hasProducer() const
87 return m_producer != NULL;
90 void KThumb::updateThumbUrl(const QString &hash)
92 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
95 void KThumb::updateClipUrl(KUrl url, const QString &hash)
98 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
102 QPixmap KThumb::getImage(KUrl url, int width, int height)
104 if (url.isEmpty()) return QPixmap();
105 return getImage(url, 0, width, height);
108 void KThumb::extractImage(QList <int>frames)
110 if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
111 m_clipManager->requestThumbs(m_id, frames);
115 void KThumb::getThumb(int frame)
117 const int theight = KdenliveSettings::trackheight();
118 const int swidth = (int)(theight * m_ratio + 0.5);
119 const int dwidth = (int)(theight * m_dar + 0.5);
121 QImage img = getProducerFrame(frame, swidth, dwidth, theight);
122 emit thumbReady(frame, img);
125 QImage KThumb::extractImage(int frame, int width, int height)
127 if (m_producer == NULL) {
128 QImage img(width, height, QImage::Format_ARGB32_Premultiplied);
132 return getProducerFrame(frame, (int) (height * m_ratio + 0.5), width, height);
136 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
138 Mlt::Profile profile(KdenliveSettings::current_profile().toUtf8().constData());
139 QPixmap pix(width, height);
140 if (url.isEmpty()) return pix;
142 //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
143 //Mlt::Producer producer(profile, "xml-string", tmp);
144 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
145 double swidth = (double) profile.width() / profile.height();
146 pix = QPixmap::fromImage(getFrame(producer, frame, (int) (height * swidth + 0.5), width, height));
152 QImage KThumb::getProducerFrame(int framepos, int frameWidth, int displayWidth, int height)
154 if (m_producer == NULL || !m_producer->is_valid()) {
155 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
156 p.fill(QColor(Qt::red).rgb());
159 if (m_producer->is_blank()) {
160 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
161 p.fill(QColor(Qt::black).rgb());
165 m_producer->seek(framepos);
166 Mlt::Frame *frame = m_producer->get_frame();
167 QImage p = getFrame(frame, frameWidth, displayWidth, height);
174 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int frameWidth, int displayWidth, int height)
176 if (producer == NULL || !producer->is_valid()) {
177 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
178 p.fill(QColor(Qt::red).rgb());
181 if (producer->is_blank()) {
182 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
183 p.fill(QColor(Qt::black).rgb());
187 producer->seek(framepos);
188 Mlt::Frame *frame = producer->get_frame();
189 QImage p = getFrame(frame, frameWidth, displayWidth, height);
196 QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int height)
198 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
199 if (frame == NULL || !frame->is_valid()) {
200 p.fill(QColor(Qt::red).rgb());
206 mlt_image_format format = mlt_image_rgb24a;
208 const uchar* imagedata = frame->get_image(format, ow, oh);
209 QImage image(ow, oh, QImage::Format_ARGB32_Premultiplied);
210 memcpy(image.bits(), imagedata, ow * oh * 4);//.byteCount());
212 //const uchar* imagedata = frame->get_image(format, ow, oh);
213 //QImage image(imagedata, ow, oh, QImage::Format_ARGB32_Premultiplied);
215 if (!image.isNull()) {
216 if (ow > (2 * displayWidth)) {
217 // there was a scaling problem, do it manually
218 image = image.scaled(displayWidth, height).rgbSwapped();
220 image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
222 p.fill(QColor(Qt::black).rgb());
223 QPainter painter(&p);
224 painter.drawImage(p.rect(), image);
227 p.fill(QColor(Qt::red).rgb());
232 uint KThumb::imageVariance(QImage image )
236 uint bytes = image.numBytes();
237 uint STEPS = bytes/2;
238 QVarLengthArray<uchar> pivot(STEPS);
239 const uchar *bits=image.bits();
240 // First pass: get pivots and taking average
241 for( uint i=0; i<STEPS ; i++ ){
242 pivot[i] = bits[2 * i];
243 #if QT_VERSION >= 0x040700
250 // Second Step: calculate delta (average?)
251 for (uint i=0; i<STEPS; i++)
253 #if QT_VERSION >= 0x040700
254 int curdelta=abs(int(avg - pivot.at(i)));
256 int curdelta=abs(int(avg - pivot[i]));
264 void KThumb::getImage(KUrl url, int frame, int width, int height)
266 if (url.isEmpty()) return;
267 QPixmap image(width, height);
268 Mlt::Producer m_producer(url.path().toUtf8().constData());
269 image.fill(Qt::black);
271 if (m_producer.is_blank()) {
272 emit thumbReady(frame, image);
275 Mlt::Filter m_convert("avcolour_space");
276 m_convert.set("forced", mlt_image_rgb24a);
277 m_producer.attach(m_convert);
278 m_producer.seek(frame);
279 Mlt::Frame * m_frame = m_producer.get_frame();
280 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(frame, image);
292 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
294 if (url.isEmpty()) return;
295 QPixmap image(width, height);
296 Mlt::Producer m_producer(url.path().toUtf8().constData());
297 image.fill(Qt::black);
299 if (m_producer.is_blank()) {
300 emit thumbReady(startframe, image);
301 emit thumbReady(endframe, image);
304 Mlt::Filter m_convert("avcolour_space");
305 m_convert.set("forced", mlt_image_rgb24a);
306 m_producer.attach(m_convert);
307 m_producer.seek(startframe);
308 Mlt::Frame * m_frame = m_producer.get_frame();
309 mlt_image_format format = mlt_image_rgb24a;
313 if (m_frame && m_frame->is_valid()) {
314 uint8_t *thumb = m_frame->get_image(format, width, height);
315 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
316 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
318 if (m_frame) delete m_frame;
319 emit thumbReady(startframe, image);
321 image.fill(Qt::black);
322 m_producer.seek(endframe);
323 m_frame = m_producer.get_frame();
325 if (m_frame && m_frame->is_valid()) {
326 uint8_t *thumb = m_frame->get_image(format, width, height);
327 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
328 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
330 if (m_frame) delete m_frame;
331 emit thumbReady(endframe, image);
335 void KThumb::slotCreateAudioThumbs()
337 m_clipManager->askForAudioThumb(m_id);
340 #if KDE_IS_VERSION(4,5,0)
341 void KThumb::queryIntraThumbs(QList <int> missingFrames)
343 foreach (int i, missingFrames) {
344 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
346 qSort(m_intraFramesQueue);
347 if (!m_intra.isRunning()) {
348 m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
352 void KThumb::slotGetIntraThumbs()
354 const int theight = KdenliveSettings::trackheight();
355 const int frameWidth = (int)(theight * m_ratio + 0.5);
356 const int displayWidth = (int)(theight * m_dar + 0.5);
357 QString path = m_url.path() + "_";
358 bool addedThumbs = false;
360 while (!m_intraFramesQueue.isEmpty()) {
361 int pos = m_intraFramesQueue.takeFirst();
362 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
363 if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getProducerFrame(pos, frameWidth, displayWidth, theight))) {
366 else kDebug()<<"// INSERT FAILD FOR: "<<pos;
368 m_intraFramesQueue.removeAll(pos);
370 if (addedThumbs) emit thumbsCached();
373 QImage KThumb::findCachedThumb(const QString &path)
376 m_clipManager->pixmapCache->findImage(path, &img);
381 #include "kthumb.moc"