]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
cleanup
[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
42
43 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/) :
44     QObject(parent),
45     m_audioThumbProducer(),
46     m_url(url),
47     m_thumbFile(),
48     m_dar(1),
49     m_producer(NULL),
50     m_clipManager(clipManager),
51     m_id(id),
52     m_stopAudioThumbs(false)
53 {
54     m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
55 }
56
57 KThumb::~KThumb()
58 {
59     m_requestedThumbs.clear();
60     if (m_audioThumbProducer.isRunning()) {
61         m_stopAudioThumbs = true;
62         m_audioThumbProducer.waitForFinished();
63         slotAudioThumbOver();
64     }
65     m_future.waitForFinished();
66 }
67
68 void KThumb::setProducer(Mlt::Producer *producer)
69 {
70     m_requestedThumbs.clear();
71     m_future.waitForFinished();
72     m_producer = producer;
73     // FIXME: the profile() call leaks an object, but trying to free
74     // it leads to a double-free in Profile::~Profile()
75     m_dar = producer->profile()->dar();
76 }
77
78 void KThumb::clearProducer()
79 {
80     m_producer = NULL;
81 }
82
83 bool KThumb::hasProducer() const
84 {
85     return m_producer != NULL;
86 }
87
88 void KThumb::updateThumbUrl(const QString &hash)
89 {
90     m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
91 }
92
93 void KThumb::updateClipUrl(KUrl url, const QString &hash)
94 {
95     m_url = url;
96     if (m_producer)
97         m_producer->set("resource", url.path().toUtf8().constData());
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(int frame, int frame2)
109 {
110     if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
111     if (frame != -1 && !m_requestedThumbs.contains(frame)) m_requestedThumbs.append(frame);
112     if (frame2 != -1 && !m_requestedThumbs.contains(frame2)) m_requestedThumbs.append(frame2);
113     if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &KThumb::doGetThumbs);
114 }
115
116 void KThumb::doGetThumbs()
117 {
118     const int theight = KdenliveSettings::trackheight();
119     const int twidth = FRAME_SIZE;//(int)(theight * m_dar + 0.5);
120
121     while (!m_requestedThumbs.isEmpty()) {
122         int frame = m_requestedThumbs.takeFirst();
123         if (frame != -1) {
124             QImage img = getFrame(m_producer, frame, twidth, theight);
125             emit thumbReady(frame, img);
126         }
127     }
128 }
129
130 QPixmap KThumb::extractImage(int frame, int width, int height)
131 {
132     return QPixmap::fromImage(getFrame(m_producer, frame, width, height));
133 }
134
135 //static
136 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
137 {
138     Mlt::Profile profile(KdenliveSettings::current_profile().toUtf8().constData());
139     QPixmap pix(width, height);
140     if (url.isEmpty()) return pix;
141
142     //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
143     //Mlt::Producer producer(profile, "xml-string", tmp);
144     Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
145
146     pix = QPixmap::fromImage(getFrame(producer, frame, width, height));
147     delete producer;
148     return pix;
149 }
150
151
152 //static
153 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
154 {
155     QImage p(width, height, QImage::Format_ARGB32_Premultiplied);
156     if (producer == NULL) {
157         p.fill(Qt::red);
158         return p;
159     }
160
161     if (producer->is_blank()) {
162         p.fill(Qt::black);
163         return p;
164     }
165
166     producer->seek(framepos);
167     Mlt::Frame *frame = producer->get_frame();
168     if (!frame) {
169         kDebug() << "///// BROKEN FRAME";
170         p.fill(Qt::red);
171         return p;
172     }
173
174     /*Mlt::Producer parentProd(producer->parent());
175     Mlt::Service service(parentProd.get_service());
176     mlt_service_lock(service.get_service());*/
177     int ow = width;
178     int oh = height;
179     mlt_image_format format = mlt_image_rgb24a;
180     uint8_t *data = frame->get_image(format, ow, oh, 0);
181     QImage image((uchar *)data, ow, oh, QImage::Format_ARGB32_Premultiplied);
182     //mlt_service_unlock(service.get_service());
183
184     if (!image.isNull()) {
185         if (ow > (2 * width)) {
186             // there was a scaling problem, do it manually
187             QImage scaled = image.scaled(width, height);
188             p = scaled.rgbSwapped();
189         } else p = image.rgbSwapped();
190     } else
191         p.fill(Qt::red);
192
193     delete frame;
194     return p;
195 }
196 /*
197 void KThumb::getImage(KUrl url, int frame, int width, int height)
198 {
199     if (url.isEmpty()) return;
200     QPixmap image(width, height);
201     Mlt::Producer m_producer(url.path().toUtf8().constData());
202     image.fill(Qt::black);
203
204     if (m_producer.is_blank()) {
205  emit thumbReady(frame, image);
206  return;
207     }
208     Mlt::Filter m_convert("avcolour_space");
209     m_convert.set("forced", mlt_image_rgb24a);
210     m_producer.attach(m_convert);
211     m_producer.seek(frame);
212     Mlt::Frame * m_frame = m_producer.get_frame();
213     mlt_image_format format = mlt_image_rgb24a;
214     width = width - 2;
215     height = height - 2;
216     if (m_frame && m_frame->is_valid()) {
217      uint8_t *thumb = m_frame->get_image(format, width, height);
218      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
219      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
220     }
221     if (m_frame) delete m_frame;
222     emit thumbReady(frame, image);
223 }
224
225 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
226 {
227     if (url.isEmpty()) return;
228     QPixmap image(width, height);
229     Mlt::Producer m_producer(url.path().toUtf8().constData());
230     image.fill(Qt::black);
231
232     if (m_producer.is_blank()) {
233  emit thumbReady(startframe, image);
234  emit thumbReady(endframe, image);
235  return;
236     }
237     Mlt::Filter m_convert("avcolour_space");
238     m_convert.set("forced", mlt_image_rgb24a);
239     m_producer.attach(m_convert);
240     m_producer.seek(startframe);
241     Mlt::Frame * m_frame = m_producer.get_frame();
242     mlt_image_format format = mlt_image_rgb24a;
243     width = width - 2;
244     height = height - 2;
245
246     if (m_frame && m_frame->is_valid()) {
247      uint8_t *thumb = m_frame->get_image(format, width, height);
248      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
249      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
250     }
251     if (m_frame) delete m_frame;
252     emit thumbReady(startframe, image);
253
254     image.fill(Qt::black);
255     m_producer.seek(endframe);
256     m_frame = m_producer.get_frame();
257
258     if (m_frame && m_frame->is_valid()) {
259      uint8_t *thumb = m_frame->get_image(format, width, height);
260      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
261      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
262     }
263     if (m_frame) delete m_frame;
264     emit thumbReady(endframe, image);
265 }
266 */
267 void KThumb::stopAudioThumbs()
268 {
269     if (m_audioThumbProducer.isRunning()) {
270         m_stopAudioThumbs = true;
271         m_audioThumbProducer.waitForFinished();
272         slotAudioThumbOver();
273     }
274 }
275
276 void KThumb::removeAudioThumb()
277 {
278     if (m_thumbFile.isEmpty()) return;
279     stopAudioThumbs();
280     QFile f(m_thumbFile);
281     f.remove();
282 }
283
284 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
285 {
286     if (channel == 0) {
287         slotAudioThumbOver();
288         return;
289     }
290     if (m_audioThumbProducer.isRunning()) {
291         return;
292     }
293
294     QMap <int, QMap <int, QByteArray> > storeIn;
295     //FIXME: Hardcoded!!!
296     m_frequency = 48000;
297     m_channels = channel;
298
299     QFile f(m_thumbFile);
300     if (f.open(QIODevice::ReadOnly)) {
301         QByteArray channelarray = f.readAll();
302         f.close();
303         if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
304             kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
305             f.remove();
306             slotAudioThumbOver();
307             return;
308         }
309
310         kDebug() << "reading audio thumbs from file";
311
312         int h1 = arrayWidth * m_channels;
313         int h2 = (int) frame * h1;
314         int h3;
315         for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
316             h2 += h1;
317             h3 = 0;
318             for (int c = 0; c < m_channels; c++) {
319                 h3 += arrayWidth;
320                 QByteArray m_array(arrayWidth, '\x00');
321                 for (int i = 0; i < arrayWidth; i++)
322                     m_array[i] = channelarray[h2 + h3 + i];
323                 storeIn[z][c] = m_array;
324             }
325         }
326         emit audioThumbReady(storeIn);
327         slotAudioThumbOver();
328     } else {
329         if (m_audioThumbProducer.isRunning()) return;
330         m_audioThumbFile.setFileName(m_thumbFile);
331         m_frame = frame;
332         m_frameLength = frameLength;
333         m_arrayWidth = arrayWidth;
334         m_audioThumbProducer = QtConcurrent::run(this, &KThumb::slotCreateAudioThumbs);
335         /*m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
336         m_audioThumbProducer.start(QThread::LowestPriority);*/
337         // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
338     }
339 }
340
341 void KThumb::slotCreateAudioThumbs()
342 {
343     if (!m_audioThumbFile.open(QIODevice::WriteOnly)) {
344         kDebug() << "++++++++  ERROR WRITING TO FILE: " << m_audioThumbFile.fileName();
345         kDebug() << "++++++++  DISABLING AUDIO THUMBS";
346         KdenliveSettings::setAudiothumbnails(false);
347         return;
348     }
349     Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
350     Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
351
352     if (KdenliveSettings::normaliseaudiothumbs()) {
353         Mlt::Filter m_convert(prof, "volume");
354         m_convert.set("gain", "normalise");
355         m_producer.attach(m_convert);
356     }
357
358     int last_val = 0;
359     int val = 0;
360     kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
361     for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && m_producer.is_valid(); z++) {
362         if (m_stopAudioThumbs) break;
363         val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
364         if (last_val != val && val > 1) {
365             m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), val);
366             last_val = val;
367         }
368         m_producer.seek(z);
369         Mlt::Frame *mlt_frame = m_producer.get_frame();
370         if (mlt_frame && mlt_frame->is_valid()) {
371             double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer());
372             int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
373             mlt_audio_format m_audioFormat = mlt_audio_pcm;
374             qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
375
376             for (int c = 0; c < m_channels; c++) {
377                 QByteArray m_array;
378                 m_array.resize(m_arrayWidth);
379                 for (int i = 0; i < m_array.size(); i++) {
380                     m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
381                 }
382                 m_audioThumbFile.write(m_array);
383
384             }
385         } else {
386             m_audioThumbFile.write(QByteArray(m_arrayWidth, '\x00'));
387         }
388         delete mlt_frame;
389     }
390     m_audioThumbFile.close();
391     if (m_stopAudioThumbs) {
392         m_audioThumbFile.remove();
393     } else {
394         slotAudioThumbOver();
395     }
396 }
397
398 void KThumb::slotAudioThumbOver()
399 {
400     m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
401     m_clipManager->endAudioThumbsGeneration(m_id);
402 }
403
404 void KThumb::askForAudioThumbs(const QString &id)
405 {
406     m_clipManager->askForAudioThumb(id);
407 }
408
409
410 #include "kthumb.moc"
411