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