]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
Nicer light gray background for thumbnail when transparent, remove unnecessary frame...
[kdenlive] / src / kthumb.cpp
1 /***************************************************************************
2                         krender.cpp  -  description
3                            -------------------
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
11
12 ***************************************************************************/
13
14 /***************************************************************************
15  *                                                                         *
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.                                   *
20  *                                                                         *
21  ***************************************************************************/
22
23 #include "kthumb.h"
24 #include "clipmanager.h"
25 #include "renderer.h"
26 #include "kdenlivesettings.h"
27
28 #include <mlt++/Mlt.h>
29
30 #include <kio/netaccess.h>
31 #include <kdebug.h>
32 #include <klocale.h>
33 #include <kfileitem.h>
34 #include <kmessagebox.h>
35 #include <KStandardDirs>
36
37 #include <qxml.h>
38 #include <QImage>
39 #include <QApplication>
40 #include <QtConcurrentRun>
41 #include <QVarLengthArray>
42
43 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent) :
44     QObject(parent),
45     m_url(url),
46     m_thumbFile(),
47     m_dar(1),
48     m_ratio(1),
49     m_producer(NULL),
50     m_clipManager(clipManager),
51     m_id(id)
52 {
53     m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
54 }
55
56 KThumb::~KThumb()
57 {
58     if (m_producer) m_clipManager->stopThumbs(m_id);
59     m_producer = NULL;
60     m_intraFramesQueue.clear();
61     m_intra.waitForFinished();
62 }
63
64 void KThumb::setProducer(Mlt::Producer *producer)
65 {
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()
73     if (producer) {
74         Mlt::Profile *profile = producer->profile();
75         m_dar = profile->dar();
76         m_ratio = (double) profile->width() / profile->height();
77     }
78 }
79
80 void KThumb::clearProducer()
81 {
82     if (m_producer) setProducer(NULL);
83 }
84
85 bool KThumb::hasProducer() const
86 {
87     return m_producer != NULL;
88 }
89
90 void KThumb::updateThumbUrl(const QString &hash)
91 {
92     m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
93 }
94
95 void KThumb::updateClipUrl(KUrl url, const QString &hash)
96 {
97     m_url = url;
98     m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
99 }
100
101 //static
102 QPixmap KThumb::getImage(KUrl url, int width, int height)
103 {
104     if (url.isEmpty()) return QPixmap();
105     return getImage(url, 0, width, height);
106 }
107
108 void KThumb::extractImage(QList <int>frames)
109 {
110     if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
111     m_clipManager->requestThumbs(m_id, frames);
112 }
113
114
115 void KThumb::getThumb(int frame)
116 {
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);
122 }
123
124 void KThumb::getGenericThumb(int frame, int height, int type)
125 {
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);
130 }
131
132 QImage KThumb::extractImage(int frame, int width, int height)
133 {
134     if (m_producer == NULL) {
135         QImage img(width, height, QImage::Format_ARGB32_Premultiplied);
136         img.fill(Qt::black);
137         return img;
138     }
139     return getProducerFrame(frame, (int) (height * m_ratio + 0.5), width, height);
140 }
141
142 //static
143 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
144 {
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));
151     delete producer;
152     return pix;
153 }
154
155
156 QImage KThumb::getProducerFrame(int framepos, int frameWidth, int displayWidth, int height)
157 {
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());
161         return p;
162     }
163     if (m_producer->is_blank()) {
164         QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
165         p.fill(QColor(Qt::black).rgb());
166         return p;
167     }
168     QMutexLocker lock(&m_mutex);
169     m_producer->seek(framepos);
170     Mlt::Frame *frame = m_producer->get_frame();
171     QImage p = getFrame(frame, frameWidth, displayWidth, height);
172     delete frame;
173     return p;
174 }
175
176 //static
177 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int frameWidth, int displayWidth, int height)
178 {
179     if (producer == NULL || !producer->is_valid()) {
180         QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
181         p.fill(QColor(Qt::red).rgb());
182         return p;
183     }
184     if (producer->is_blank()) {
185         QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
186         p.fill(QColor(Qt::black).rgb());
187         return p;
188     }
189
190     producer->seek(framepos);
191     Mlt::Frame *frame = producer->get_frame();
192     frame->set("rescale.interp", "nearest");
193     frame->set("deinterlace_method", "onefield");
194     frame->set("top_field_first", -1 );
195     QImage p = getFrame(frame, frameWidth, displayWidth, height);
196     delete frame;
197     return p;
198 }
199
200
201 //static
202 QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int height)
203 {
204     QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
205     if (frame == NULL || !frame->is_valid()) {
206         p.fill(QColor(Qt::red).rgb());
207         return p;
208     }
209     
210     int ow = frameWidth;
211     int oh = height;
212     mlt_image_format format = mlt_image_rgb24a;
213     //frame->set("progressive", "1");
214     if (ow % 2 == 1) ow++;
215     QImage image(ow, oh, QImage::Format_ARGB32_Premultiplied);
216     const uchar* imagedata = frame->get_image(format, ow, oh);
217     memcpy(image.bits(), imagedata, ow * oh * 4);//.byteCount());
218     
219     //const uchar* imagedata = frame->get_image(format, ow, oh);
220     //QImage image(imagedata, ow, oh, QImage::Format_ARGB32_Premultiplied);
221     
222     if (!image.isNull()) {
223         if (ow > (2 * displayWidth)) {
224             // there was a scaling problem, do it manually
225             image = image.scaled(displayWidth, height).rgbSwapped();
226         } else {
227             image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
228         }
229         p.fill(QColor(100, 100, 100, 70));
230         QPainter painter(&p);
231         painter.drawImage(p.rect(), image);
232         painter.end();
233     } else
234         p.fill(QColor(Qt::red).rgb());
235     return p;
236 }
237
238 //static
239 uint KThumb::imageVariance(QImage image )
240 {
241     uint delta = 0;
242     uint avg = 0;
243     uint bytes = image.numBytes();
244     uint STEPS = bytes/2;
245     QVarLengthArray<uchar> pivot(STEPS);
246     const uchar *bits=image.bits();
247     // First pass: get pivots and taking average
248     for( uint i=0; i<STEPS ; i++ ){
249         pivot[i] = bits[2 * i];
250 #if QT_VERSION >= 0x040700
251         avg+=pivot.at(i);
252 #else
253         avg+=pivot[i];
254 #endif
255     }
256     if (STEPS)
257         avg=avg/STEPS;
258     // Second Step: calculate delta (average?)
259     for (uint i=0; i<STEPS; i++)
260     {
261 #if QT_VERSION >= 0x040700
262         int curdelta=abs(int(avg - pivot.at(i)));
263 #else
264         int curdelta=abs(int(avg - pivot[i]));
265 #endif
266         delta+=curdelta;
267     }
268     if (STEPS)
269         return delta/STEPS;
270     else
271         return 0;
272 }
273
274 /*
275 void KThumb::getImage(KUrl url, int frame, int width, int height)
276 {
277     if (url.isEmpty()) return;
278     QPixmap image(width, height);
279     Mlt::Producer m_producer(url.path().toUtf8().constData());
280     image.fill(Qt::black);
281
282     if (m_producer.is_blank()) {
283  emit thumbReady(frame, image);
284  return;
285     }
286     Mlt::Filter m_convert("avcolour_space");
287     m_convert.set("forced", mlt_image_rgb24a);
288     m_producer.attach(m_convert);
289     m_producer.seek(frame);
290     Mlt::Frame * m_frame = m_producer.get_frame();
291     mlt_image_format format = mlt_image_rgb24a;
292     width = width - 2;
293     height = height - 2;
294     if (m_frame && m_frame->is_valid()) {
295      uint8_t *thumb = m_frame->get_image(format, width, height);
296      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
297      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
298     }
299     if (m_frame) delete m_frame;
300     emit thumbReady(frame, image);
301 }
302
303 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
304 {
305     if (url.isEmpty()) return;
306     QPixmap image(width, height);
307     Mlt::Producer m_producer(url.path().toUtf8().constData());
308     image.fill(Qt::black);
309
310     if (m_producer.is_blank()) {
311  emit thumbReady(startframe, image);
312  emit thumbReady(endframe, image);
313  return;
314     }
315     Mlt::Filter m_convert("avcolour_space");
316     m_convert.set("forced", mlt_image_rgb24a);
317     m_producer.attach(m_convert);
318     m_producer.seek(startframe);
319     Mlt::Frame * m_frame = m_producer.get_frame();
320     mlt_image_format format = mlt_image_rgb24a;
321     width = width - 2;
322     height = height - 2;
323
324     if (m_frame && m_frame->is_valid()) {
325      uint8_t *thumb = m_frame->get_image(format, width, height);
326      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
327      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
328     }
329     if (m_frame) delete m_frame;
330     emit thumbReady(startframe, image);
331
332     image.fill(Qt::black);
333     m_producer.seek(endframe);
334     m_frame = m_producer.get_frame();
335
336     if (m_frame && m_frame->is_valid()) {
337      uint8_t *thumb = m_frame->get_image(format, width, height);
338      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
339      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
340     }
341     if (m_frame) delete m_frame;
342     emit thumbReady(endframe, image);
343 }
344 */
345
346 void KThumb::slotCreateAudioThumbs()
347 {
348     m_clipManager->askForAudioThumb(m_id);
349 }
350
351 #if KDE_IS_VERSION(4,5,0)
352 void KThumb::queryIntraThumbs(QList <int> missingFrames)
353 {
354     foreach (int i, missingFrames) {
355         if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
356     }
357     qSort(m_intraFramesQueue);
358     if (!m_intra.isRunning()) {
359         m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
360     }
361 }
362
363 void KThumb::slotGetIntraThumbs()
364 {
365     const int theight = KdenliveSettings::trackheight();
366     const int frameWidth = (int)(theight * m_ratio + 0.5);
367     const int displayWidth = (int)(theight * m_dar + 0.5);
368     QString path = m_url.path() + '_';
369     bool addedThumbs = false;
370
371     while (!m_intraFramesQueue.isEmpty()) {
372         int pos = m_intraFramesQueue.takeFirst();
373         if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
374             if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getProducerFrame(pos, frameWidth, displayWidth, theight))) {
375                 addedThumbs = true;
376             }
377             else kDebug()<<"// INSERT FAILD FOR: "<<pos;
378         }
379         m_intraFramesQueue.removeAll(pos);
380     }
381     if (addedThumbs) emit thumbsCached();
382 }
383
384 QImage KThumb::findCachedThumb(const QString &path)
385 {
386     QImage img;
387     m_clipManager->pixmapCache->findImage(path, &img);
388     return img;
389 }
390 #endif
391
392 #include "kthumb.moc"
393