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