]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
3d660b551bf18a7841c1b70560fd845b720a28a6
[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     memcpy(image.bits(), imagedata, ow * oh * 4);//.byteCount());
222     
223     //const uchar* imagedata = frame->get_image(format, ow, oh);
224     //QImage image(imagedata, ow, oh, QImage::Format_ARGB32_Premultiplied);
225     
226     if (!image.isNull()) {
227         if (ow > (2 * displayWidth)) {
228             // there was a scaling problem, do it manually
229             image = image.scaled(displayWidth, height).rgbSwapped();
230         } else {
231             image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
232         }
233 #if QT_VERSION >= 0x040800
234         p.fill(QColor(100, 100, 100, 70));
235         QPainter painter(&p);
236 #else
237         p.fill(Qt::transparent);
238         QPainter painter(&p);
239         painter.fillRect(p.rect(), QColor(100, 100, 100, 70));
240 #endif
241         painter.drawImage(p.rect(), image);
242         painter.end();
243     } else
244         p.fill(QColor(Qt::red).rgb());
245     return p;
246 }
247
248 //static
249 uint KThumb::imageVariance(const QImage &image )
250 {
251     uint delta = 0;
252     uint avg = 0;
253     uint bytes = image.numBytes();
254     uint STEPS = bytes/2;
255     QVarLengthArray<uchar> pivot(STEPS);
256     const uchar *bits=image.bits();
257     // First pass: get pivots and taking average
258     for( uint i=0; i<STEPS ; ++i ){
259         pivot[i] = bits[2 * i];
260 #if QT_VERSION >= 0x040700
261         avg+=pivot.at(i);
262 #else
263         avg+=pivot[i];
264 #endif
265     }
266     if (STEPS)
267         avg=avg/STEPS;
268     // Second Step: calculate delta (average?)
269     for (uint i=0; i<STEPS; ++i)
270     {
271 #if QT_VERSION >= 0x040700
272         int curdelta=abs(int(avg - pivot.at(i)));
273 #else
274         int curdelta=abs(int(avg - pivot[i]));
275 #endif
276         delta+=curdelta;
277     }
278     if (STEPS)
279         return delta/STEPS;
280     else
281         return 0;
282 }
283
284 /*
285 void KThumb::getImage(KUrl url, int frame, int width, int height)
286 {
287     if (url.isEmpty()) return;
288     QPixmap image(width, height);
289     Mlt::Producer m_producer(url.path().toUtf8().constData());
290     image.fill(Qt::black);
291
292     if (m_producer.is_blank()) {
293  emit thumbReady(frame, image);
294  return;
295     }
296     Mlt::Filter m_convert("avcolour_space");
297     m_convert.set("forced", mlt_image_rgb24a);
298     m_producer.attach(m_convert);
299     m_producer.seek(frame);
300     Mlt::Frame * m_frame = m_producer.get_frame();
301     mlt_image_format format = mlt_image_rgb24a;
302     width = width - 2;
303     height = height - 2;
304     if (m_frame && m_frame->is_valid()) {
305      uint8_t *thumb = m_frame->get_image(format, width, height);
306      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
307      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
308     }
309     if (m_frame) delete m_frame;
310     emit thumbReady(frame, image);
311 }
312
313 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
314 {
315     if (url.isEmpty()) return;
316     QPixmap image(width, height);
317     Mlt::Producer m_producer(url.path().toUtf8().constData());
318     image.fill(QColor(Qt::black).rgb());
319
320     if (m_producer.is_blank()) {
321  emit thumbReady(startframe, image);
322  emit thumbReady(endframe, image);
323  return;
324     }
325     Mlt::Filter m_convert("avcolour_space");
326     m_convert.set("forced", mlt_image_rgb24a);
327     m_producer.attach(m_convert);
328     m_producer.seek(startframe);
329     Mlt::Frame * m_frame = m_producer.get_frame();
330     mlt_image_format format = mlt_image_rgb24a;
331     width = width - 2;
332     height = height - 2;
333
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);
338     }
339     if (m_frame) delete m_frame;
340     emit thumbReady(startframe, image);
341
342     image.fill(Qt::black);
343     m_producer.seek(endframe);
344     m_frame = m_producer.get_frame();
345
346     if (m_frame && m_frame->is_valid()) {
347      uint8_t *thumb = m_frame->get_image(format, width, height);
348      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
349      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
350     }
351     if (m_frame) delete m_frame;
352     emit thumbReady(endframe, image);
353 }
354 */
355
356 void KThumb::slotCreateAudioThumbs()
357 {
358     m_clipManager->askForAudioThumb(m_id);
359 }
360
361 #if KDE_IS_VERSION(4,5,0)
362 void KThumb::queryIntraThumbs(const QList <int> &missingFrames)
363 {
364     foreach (int i, missingFrames) {
365         if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
366     }
367     qSort(m_intraFramesQueue);
368     if (!m_intra.isRunning()) {
369         m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
370     }
371 }
372
373 void KThumb::slotGetIntraThumbs()
374 {
375     const int theight = KdenliveSettings::trackheight();
376     const int frameWidth = (int)(theight * m_ratio + 0.5);
377     const int displayWidth = (int)(theight * m_dar + 0.5);
378     QString path = m_url.path() + '_';
379     bool addedThumbs = false;
380
381     while (!m_intraFramesQueue.isEmpty()) {
382         int pos = m_intraFramesQueue.takeFirst();
383         if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
384             if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getProducerFrame(pos, frameWidth, displayWidth, theight))) {
385                 addedThumbs = true;
386             }
387             else kDebug()<<"// INSERT FAILD FOR: "<<pos;
388         }
389         m_intraFramesQueue.removeAll(pos);
390     }
391     if (addedThumbs) emit thumbsCached();
392 }
393
394 QImage KThumb::findCachedThumb(const QString &path)
395 {
396     QImage img;
397     m_clipManager->pixmapCache->findImage(path, &img);
398     return img;
399 }
400 #endif
401
402 #include "kthumb.moc"
403