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();
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();
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(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->requestThumbs(m_id, frames);
116 void KThumb::getThumb(int frame)
118 const int theight = KdenliveSettings::trackheight();
119 const int swidth = (int)(theight * m_ratio + 0.5);
120 const int dwidth = (int)(theight * m_dar + 0.5);
122 QImage img = getProducerFrame(frame, swidth, dwidth, theight);
123 emit thumbReady(frame, img);
126 QImage KThumb::extractImage(int frame, int width, int height)
128 if (m_producer == NULL) {
129 QImage img(width, height, QImage::Format_ARGB32_Premultiplied);
133 return getProducerFrame(frame, (int) (height * m_ratio + 0.5), width, height);
137 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
139 Mlt::Profile profile(KdenliveSettings::current_profile().toUtf8().constData());
140 QPixmap pix(width, height);
141 if (url.isEmpty()) return pix;
143 //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
144 //Mlt::Producer producer(profile, "xml-string", tmp);
145 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
146 double swidth = (double) profile.width() / profile.height();
147 pix = QPixmap::fromImage(getFrame(producer, frame, (int) (height * swidth + 0.5), width, height));
153 QImage KThumb::getProducerFrame(int framepos, int frameWidth, int displayWidth, int height)
155 if (m_producer == NULL || !m_producer->is_valid()) {
156 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
157 p.fill(QColor(Qt::red).rgb());
160 if (m_producer->is_blank()) {
161 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
162 p.fill(QColor(Qt::black).rgb());
166 m_producer->seek(framepos);
167 Mlt::Frame *frame = m_producer->get_frame();
168 QImage p = getFrame(frame, frameWidth, displayWidth, height);
175 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int frameWidth, int displayWidth, int height)
177 if (producer == NULL || !producer->is_valid()) {
178 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
179 p.fill(QColor(Qt::red).rgb());
182 if (producer->is_blank()) {
183 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
184 p.fill(QColor(Qt::black).rgb());
188 producer->seek(framepos);
189 Mlt::Frame *frame = producer->get_frame();
190 QImage p = getFrame(frame, frameWidth, displayWidth, height);
197 QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int height)
199 QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
200 if (frame == NULL || !frame->is_valid()) {
201 p.fill(QColor(Qt::red).rgb());
207 mlt_image_format format = mlt_image_rgb24a;
208 frame->set("rescale.interp", "nearest");
209 frame->set("deinterlace_method", "onefield");
210 frame->set("top_field_first", -1 );
211 //frame->set("progressive", "1");
212 if (ow % 2 == 1) ow++;
213 const uchar* imagedata = frame->get_image(format, ow, oh);
214 QImage image(ow, oh, QImage::Format_ARGB32_Premultiplied);
215 memcpy(image.bits(), imagedata, ow * oh * 4);//.byteCount());
217 //const uchar* imagedata = frame->get_image(format, ow, oh);
218 //QImage image(imagedata, ow, oh, QImage::Format_ARGB32_Premultiplied);
220 if (!image.isNull()) {
221 if (ow > (2 * displayWidth)) {
222 // there was a scaling problem, do it manually
223 image = image.scaled(displayWidth, height).rgbSwapped();
225 image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
227 p.fill(QColor(Qt::black).rgb());
228 QPainter painter(&p);
229 painter.drawImage(p.rect(), image);
232 p.fill(QColor(Qt::red).rgb());
237 uint KThumb::imageVariance(QImage image )
241 uint bytes = image.numBytes();
242 uint STEPS = bytes/2;
243 QVarLengthArray<uchar> pivot(STEPS);
244 const uchar *bits=image.bits();
245 // First pass: get pivots and taking average
246 for( uint i=0; i<STEPS ; i++ ){
247 pivot[i] = bits[2 * i];
248 #if QT_VERSION >= 0x040700
256 // Second Step: calculate delta (average?)
257 for (uint i=0; i<STEPS; i++)
259 #if QT_VERSION >= 0x040700
260 int curdelta=abs(int(avg - pivot.at(i)));
262 int curdelta=abs(int(avg - pivot[i]));
273 void KThumb::getImage(KUrl url, int frame, int width, int height)
275 if (url.isEmpty()) return;
276 QPixmap image(width, height);
277 Mlt::Producer m_producer(url.path().toUtf8().constData());
278 image.fill(Qt::black);
280 if (m_producer.is_blank()) {
281 emit thumbReady(frame, image);
284 Mlt::Filter m_convert("avcolour_space");
285 m_convert.set("forced", mlt_image_rgb24a);
286 m_producer.attach(m_convert);
287 m_producer.seek(frame);
288 Mlt::Frame * m_frame = m_producer.get_frame();
289 mlt_image_format format = mlt_image_rgb24a;
292 if (m_frame && m_frame->is_valid()) {
293 uint8_t *thumb = m_frame->get_image(format, width, height);
294 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
295 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
297 if (m_frame) delete m_frame;
298 emit thumbReady(frame, image);
301 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
303 if (url.isEmpty()) return;
304 QPixmap image(width, height);
305 Mlt::Producer m_producer(url.path().toUtf8().constData());
306 image.fill(Qt::black);
308 if (m_producer.is_blank()) {
309 emit thumbReady(startframe, image);
310 emit thumbReady(endframe, image);
313 Mlt::Filter m_convert("avcolour_space");
314 m_convert.set("forced", mlt_image_rgb24a);
315 m_producer.attach(m_convert);
316 m_producer.seek(startframe);
317 Mlt::Frame * m_frame = m_producer.get_frame();
318 mlt_image_format format = mlt_image_rgb24a;
322 if (m_frame && m_frame->is_valid()) {
323 uint8_t *thumb = m_frame->get_image(format, width, height);
324 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
325 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
327 if (m_frame) delete m_frame;
328 emit thumbReady(startframe, image);
330 image.fill(Qt::black);
331 m_producer.seek(endframe);
332 m_frame = m_producer.get_frame();
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(endframe, image);
344 void KThumb::slotCreateAudioThumbs()
346 m_clipManager->askForAudioThumb(m_id);
349 #if KDE_IS_VERSION(4,5,0)
350 void KThumb::queryIntraThumbs(QList <int> missingFrames)
352 foreach (int i, missingFrames) {
353 if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
355 qSort(m_intraFramesQueue);
356 if (!m_intra.isRunning()) {
357 m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
361 void KThumb::slotGetIntraThumbs()
363 const int theight = KdenliveSettings::trackheight();
364 const int frameWidth = (int)(theight * m_ratio + 0.5);
365 const int displayWidth = (int)(theight * m_dar + 0.5);
366 QString path = m_url.path() + '_';
367 bool addedThumbs = false;
369 while (!m_intraFramesQueue.isEmpty()) {
370 int pos = m_intraFramesQueue.takeFirst();
371 if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
372 if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getProducerFrame(pos, frameWidth, displayWidth, theight))) {
375 else kDebug()<<"// INSERT FAILD FOR: "<<pos;
377 m_intraFramesQueue.removeAll(pos);
379 if (addedThumbs) emit thumbsCached();
382 QImage KThumb::findCachedThumb(const QString &path)
385 m_clipManager->pixmapCache->findImage(path, &img);
390 #include "kthumb.moc"