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) :
50 m_clipManager(clipManager),
53 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
58 if (m_producer) m_clipManager->stopThumbs(m_id);
60 m_intraFramesQueue.clear();
61 m_intra.waitForFinished();
64 void KThumb::setProducer(Mlt::Producer *producer)
66 if (m_producer) m_clipManager->stopThumbs(m_id);
67 m_intraFramesQueue.clear();
68 m_intra.waitForFinished();
69 QMutexLocker lock(&m_mutex);
70 m_producer = producer;
71 // FIXME: the profile() call leaks an object, but trying to free
72 // it leads to a double-free in Profile::~Profile()
74 Mlt::Profile *profile = producer->profile();
75 m_dar = profile->dar();
76 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);
120 QImage img = getProducerFrame(frame, swidth, dwidth, theight);
121 emit thumbReady(frame, img);
124 void KThumb::getGenericThumb(int frame, int height, int type)
126 const int swidth = (int)(height * m_ratio + 0.5);
127 const int dwidth = (int)(height * m_dar + 0.5);
128 QImage img = getProducerFrame(frame, swidth, dwidth, height);
129 m_clipManager->projectTreeThumbReady(m_id, frame, img, type);
132 QImage KThumb::extractImage(int frame, int width, int height)
134 if (m_producer == NULL) {
135 QImage img(width, height, QImage::Format_ARGB32_Premultiplied);
136 img.fill(QColor(Qt::black).rgb());
139 return getProducerFrame(frame, (int) (height * m_ratio + 0.5), width, height);
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;
148 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
149 double swidth = (double) profile.width() / profile.height();
150 pix = QPixmap::fromImage(getFrame(producer, frame, (int) (height * swidth + 0.5), width, height));
156 QImage KThumb::getProducerFrame(int framepos, int frameWidth, int displayWidth, int height)
158 if (m_producer == NULL || !m_producer->is_valid()) {
159 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
160 p.fill(QColor(Qt::red).rgb());
163 if (m_producer->is_blank()) {
164 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
165 p.fill(QColor(Qt::black).rgb());
168 QMutexLocker lock(&m_mutex);
169 m_producer->seek(framepos);
170 Mlt::Frame *frame = m_producer->get_frame();
171 /*frame->set("rescale.interp", "nearest");
172 frame->set("deinterlace_method", "onefield");
173 frame->set("top_field_first", -1 );*/
174 QImage p = getFrame(frame, frameWidth, displayWidth, height);
180 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int frameWidth, int displayWidth, int height)
182 if (producer == NULL || !producer->is_valid()) {
183 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
184 p.fill(QColor(Qt::red).rgb());
187 if (producer->is_blank()) {
188 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
189 p.fill(QColor(Qt::black).rgb());
193 producer->seek(framepos);
194 Mlt::Frame *frame = producer->get_frame();
195 frame->set("rescale.interp", "nearest");
196 frame->set("deinterlace_method", "onefield");
197 frame->set("top_field_first", -1 );
198 QImage p = getFrame(frame, frameWidth, displayWidth, height);
205 QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int height)
207 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
208 if (frame == NULL || !frame->is_valid()) {
209 p.fill(QColor(Qt::red).rgb());
215 mlt_image_format format = mlt_image_rgb24a;
216 //frame->set("progressive", "1");
217 if (ow % 2 == 1) ow++;
218 QImage image(ow, oh, QImage::Format_ARGB32_Premultiplied);
219 const uchar* imagedata = frame->get_image(format, ow, oh);
220 memcpy(image.bits(), imagedata, ow * oh * 4);//.byteCount());
222 //const uchar* imagedata = frame->get_image(format, ow, oh);
223 //QImage image(imagedata, ow, oh, QImage::Format_ARGB32_Premultiplied);
225 if (!image.isNull()) {
226 if (ow > (2 * displayWidth)) {
227 // there was a scaling problem, do it manually
228 image = image.scaled(displayWidth, height).rgbSwapped();
230 image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
232 #if QT_VERSION >= 0x040800
233 p.fill(QColor(100, 100, 100, 70));
234 QPainter painter(&p);
236 p.fill(Qt::transparent);
237 QPainter painter(&p);
238 painter.fillRect(p.rect(), QColor(100, 100, 100, 70));
240 painter.drawImage(p.rect(), image);
243 p.fill(QColor(Qt::red).rgb());
248 uint KThumb::imageVariance(QImage image )
252 uint bytes = image.numBytes();
253 uint STEPS = bytes/2;
254 QVarLengthArray<uchar> pivot(STEPS);
255 const uchar *bits=image.bits();
256 // First pass: get pivots and taking average
257 for( uint i=0; i<STEPS ; i++ ){
258 pivot[i] = bits[2 * i];
259 #if QT_VERSION >= 0x040700
267 // Second Step: calculate delta (average?)
268 for (uint i=0; i<STEPS; i++)
270 #if QT_VERSION >= 0x040700
271 int curdelta=abs(int(avg - pivot.at(i)));
273 int curdelta=abs(int(avg - pivot[i]));
284 void KThumb::getImage(KUrl url, int frame, int width, int height)
286 if (url.isEmpty()) return;
287 QPixmap image(width, height);
288 Mlt::Producer m_producer(url.path().toUtf8().constData());
289 image.fill(Qt::black);
291 if (m_producer.is_blank()) {
292 emit thumbReady(frame, image);
295 Mlt::Filter m_convert("avcolour_space");
296 m_convert.set("forced", mlt_image_rgb24a);
297 m_producer.attach(m_convert);
298 m_producer.seek(frame);
299 Mlt::Frame * m_frame = m_producer.get_frame();
300 mlt_image_format format = mlt_image_rgb24a;
303 if (m_frame && m_frame->is_valid()) {
304 uint8_t *thumb = m_frame->get_image(format, width, height);
305 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
306 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
308 if (m_frame) delete m_frame;
309 emit thumbReady(frame, image);
312 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
314 if (url.isEmpty()) return;
315 QPixmap image(width, height);
316 Mlt::Producer m_producer(url.path().toUtf8().constData());
317 image.fill(QColor(Qt::black).rgb());
319 if (m_producer.is_blank()) {
320 emit thumbReady(startframe, image);
321 emit thumbReady(endframe, image);
324 Mlt::Filter m_convert("avcolour_space");
325 m_convert.set("forced", mlt_image_rgb24a);
326 m_producer.attach(m_convert);
327 m_producer.seek(startframe);
328 Mlt::Frame * m_frame = m_producer.get_frame();
329 mlt_image_format format = mlt_image_rgb24a;
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(startframe, image);
341 image.fill(Qt::black);
342 m_producer.seek(endframe);
343 m_frame = m_producer.get_frame();
345 if (m_frame && m_frame->is_valid()) {
346 uint8_t *thumb = m_frame->get_image(format, width, height);
347 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
348 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
350 if (m_frame) delete m_frame;
351 emit thumbReady(endframe, image);
355 void KThumb::slotCreateAudioThumbs()
357 m_clipManager->askForAudioThumb(m_id);
360 #if KDE_IS_VERSION(4,5,0)
361 void KThumb::queryIntraThumbs(QList <int> missingFrames)
363 foreach (int i, missingFrames) {
364 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
366 qSort(m_intraFramesQueue);
367 if (!m_intra.isRunning()) {
368 m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
372 void KThumb::slotGetIntraThumbs()
374 const int theight = KdenliveSettings::trackheight();
375 const int frameWidth = (int)(theight * m_ratio + 0.5);
376 const int displayWidth = (int)(theight * m_dar + 0.5);
377 QString path = m_url.path() + '_';
378 bool addedThumbs = false;
380 while (!m_intraFramesQueue.isEmpty()) {
381 int pos = m_intraFramesQueue.takeFirst();
382 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
383 if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getProducerFrame(pos, frameWidth, displayWidth, theight))) {
386 else kDebug()<<"// INSERT FAILD FOR: "<<pos;
388 m_intraFramesQueue.removeAll(pos);
390 if (addedThumbs) emit thumbsCached();
393 QImage KThumb::findCachedThumb(const QString &path)
396 m_clipManager->pixmapCache->findImage(path, &img);
401 #include "kthumb.moc"