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