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