]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
Cleanup audio thumbnails thread handling
[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 = 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         if (mlt_frame)
110             delete mlt_frame;
111     }
112     //kDebug() << "done";
113     f.close();
114     m_isWorking = false;
115     if (stop_me) {
116         f.remove();
117     } else emit audioThumbOver();
118 }
119
120 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/) :
121         QObject(parent),
122         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         m_mainFrame(-1)
130 {
131     m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
132     connect(&audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
133     connect(&audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));
134
135 }
136
137 KThumb::~KThumb()
138 {
139     if (audioThumbProducer.isRunning()) {
140         audioThumbProducer.stop_me = true;
141         audioThumbProducer.wait();
142         slotAudioThumbOver();
143     }
144 }
145
146 void KThumb::setProducer(Mlt::Producer *producer)
147 {
148     m_producer = producer;
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     if (m_url.isEmpty() || !KdenliveSettings::videothumbnails() || m_producer == NULL) return;
188
189     const int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
190     const int theight = KdenliveSettings::trackheight();
191
192     mlt_image_format format = mlt_image_yuv422;
193     if (m_producer->is_blank()) {
194         QPixmap pix(twidth, theight);
195         pix.fill(Qt::black);
196         emit thumbReady(frame, pix);
197         return;
198     }
199     Mlt::Frame *mltFrame;
200     if (frame != -1) {
201         //videoThumbProducer.getThumb(frame);
202         m_producer->seek(frame);
203         mltFrame = m_producer->get_frame();
204         if (frame2 != -1) m_producer->seek(frame2);
205         if (!mltFrame) {
206             kDebug() << "///// BROKEN FRAME";
207             QPixmap p(twidth, theight);
208             p.fill(Qt::red);
209             emit thumbReady(frame, p);
210             return;
211         } else {
212             int frame_width = 0;
213             int frame_height = 0;
214             mltFrame->set("normalised_height", theight);
215             mltFrame->set("normalised_width", twidth);
216             QPixmap pix(twidth, theight);
217             uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
218             uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
219             mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
220
221             QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
222
223             if (!image.isNull()) {
224                 pix = QPixmap::fromImage(image.rgbSwapped());
225             } else
226                 pix.fill(Qt::red);
227
228             mlt_pool_release(new_image);
229             delete mltFrame;
230             emit thumbReady(frame, pix);
231         }
232     } else if (frame2 != -1) m_producer->seek(frame2);
233     if (frame2 != -1) {
234         mltFrame = m_producer->get_frame();
235         if (!mltFrame) {
236             kDebug() << "///// BROKEN FRAME";
237             QPixmap p(twidth, theight);
238             p.fill(Qt::red);
239             emit thumbReady(frame, p);
240             return;
241         } else {
242             int frame_width = 0;
243             int frame_height = 0;
244             mltFrame->set("normalised_height", theight);
245             mltFrame->set("normalised_width", twidth);
246             QPixmap pix(twidth, theight);
247             uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
248             uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
249             mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
250
251             QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
252
253             if (!image.isNull()) {
254                 pix = QPixmap::fromImage(image.rgbSwapped());
255             } else
256                 pix.fill(Qt::red);
257
258             mlt_pool_release(new_image);
259             delete mltFrame;
260             emit thumbReady(frame2, pix);
261         }
262     }
263 }
264
265 QPixmap KThumb::extractImage(int frame, int width, int height)
266 {
267     return getFrame(m_producer, frame, width, height);
268 }
269
270 //static
271 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
272 {
273     Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
274     QPixmap pix(width, height);
275     if (url.isEmpty()) return pix;
276
277     char *tmp = Render::decodedString(url.path());
278     //"<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
279     //Mlt::Producer producer(profile, "westley-xml", tmp);
280     Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
281     delete[] tmp;
282
283     if (producer->is_blank()) {
284         pix.fill(Qt::black);
285         delete producer;
286         return pix;
287     }
288     pix = getFrame(producer, frame, width, height);
289     delete producer;
290     return pix;
291 }
292
293 //static
294 /*
295 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
296     Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
297     QPixmap pix(width, height);
298     QDomDocument doc;
299     QDomElement westley = doc.createElement("westley");
300     QDomElement play = doc.createElement("playlist");
301     doc.appendChild(westley);
302     westley.appendChild(play);
303     play.appendChild(doc.importNode(xml, true));
304     char *tmp = Render::decodedString(doc.toString());
305     Mlt::Producer producer(profile, "westley-xml", tmp);
306     delete[] tmp;
307
308     if (producer.is_blank()) {
309         pix.fill(Qt::black);
310         return pix;
311     }
312     return getFrame(producer, frame, width, height);
313 }*/
314
315 //static
316 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
317 {
318     if (producer == NULL) {
319         QPixmap p(width, height);
320         p.fill(Qt::red);
321         return p;
322     }
323
324     producer->seek(framepos);
325     Mlt::Frame *frame = producer->get_frame();
326     if (!frame) {
327         kDebug() << "///// BROKEN FRAME";
328         QPixmap p(width, height);
329         p.fill(Qt::red);
330         return p;
331     }
332
333     mlt_image_format format = mlt_image_yuv422;
334     int frame_width = 0;
335     int frame_height = 0;
336     frame->set("normalised_height", height);
337     frame->set("normalised_width", width);
338     QPixmap pix(width, height);
339     uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
340     uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
341     mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
342
343     QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
344
345     if (!image.isNull()) {
346         pix = QPixmap::fromImage(image.rgbSwapped());
347     } else
348         pix.fill(Qt::red);
349
350     mlt_pool_release(new_image);
351     delete frame;
352     return pix;
353 }
354 /*
355 void KThumb::getImage(KUrl url, int frame, int width, int height)
356 {
357     if (url.isEmpty()) return;
358     QPixmap image(width, height);
359     char *tmp = KRender::decodedString(url.path());
360     Mlt::Producer m_producer(tmp);
361     delete tmp;
362     image.fill(Qt::black);
363
364     if (m_producer.is_blank()) {
365  emit thumbReady(frame, image);
366  return;
367     }
368     Mlt::Filter m_convert("avcolour_space");
369     m_convert.set("forced", mlt_image_rgb24a);
370     m_producer.attach(m_convert);
371     m_producer.seek(frame);
372     Mlt::Frame * m_frame = m_producer.get_frame();
373     mlt_image_format format = mlt_image_rgb24a;
374     width = width - 2;
375     height = height - 2;
376     if (m_frame && m_frame->is_valid()) {
377      uint8_t *thumb = m_frame->get_image(format, width, height);
378      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
379      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
380     }
381     if (m_frame) delete m_frame;
382     emit thumbReady(frame, image);
383 }
384
385 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
386 {
387     if (url.isEmpty()) return;
388     QPixmap image(width, height);
389     char *tmp = KRender::decodedString(url.path());
390     Mlt::Producer m_producer(tmp);
391     delete tmp;
392     image.fill(Qt::black);
393
394     if (m_producer.is_blank()) {
395  emit thumbReady(startframe, image);
396  emit thumbReady(endframe, image);
397  return;
398     }
399     Mlt::Filter m_convert("avcolour_space");
400     m_convert.set("forced", mlt_image_rgb24a);
401     m_producer.attach(m_convert);
402     m_producer.seek(startframe);
403     Mlt::Frame * m_frame = m_producer.get_frame();
404     mlt_image_format format = mlt_image_rgb24a;
405     width = width - 2;
406     height = height - 2;
407
408     if (m_frame && m_frame->is_valid()) {
409      uint8_t *thumb = m_frame->get_image(format, width, height);
410      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
411      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
412     }
413     if (m_frame) delete m_frame;
414     emit thumbReady(startframe, image);
415
416     image.fill(Qt::black);
417     m_producer.seek(endframe);
418     m_frame = m_producer.get_frame();
419
420     if (m_frame && m_frame->is_valid()) {
421      uint8_t *thumb = m_frame->get_image(format, width, height);
422      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
423      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
424     }
425     if (m_frame) delete m_frame;
426     emit thumbReady(endframe, image);
427 }
428 */
429 void KThumb::stopAudioThumbs()
430 {
431     if (audioThumbProducer.isRunning()) {
432         audioThumbProducer.stop_me = true;
433         slotAudioThumbOver();
434     }
435 }
436
437 void KThumb::removeAudioThumb()
438 {
439     if (m_thumbFile.isEmpty()) return;
440     stopAudioThumbs();
441     QFile f(m_thumbFile);
442     f.remove();
443 }
444
445 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
446 {
447     if (channel == 0) {
448         slotAudioThumbOver();
449         return;
450     }
451     if ((audioThumbProducer.isRunning() && audioThumbProducer.isWorking())) {
452         return;
453     }
454
455     QMap <int, QMap <int, QByteArray> > storeIn;
456     //FIXME: Hardcoded!!!
457     int m_frequency = 48000;
458     int m_channels = channel;
459
460     QFile f(m_thumbFile);
461     if (f.open(QIODevice::ReadOnly)) {
462         QByteArray channelarray = f.readAll();
463         f.close();
464         if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
465             kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
466             f.remove();
467             slotAudioThumbOver();
468             return;
469         }
470         kDebug() << "reading audio thumbs from file";
471         for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
472             for (int c = 0;c < m_channels;c++) {
473                 QByteArray m_array(arrayWidth, '\x00');
474                 for (int i = 0; i < arrayWidth; i++)
475                     m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
476                 storeIn[z][c] = m_array;
477             }
478         }
479         emit audioThumbReady(storeIn);
480         slotAudioThumbOver();
481     } else {
482         if (audioThumbProducer.isRunning()) return;
483         audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
484         audioThumbProducer.start(QThread::LowestPriority);
485         kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
486     }
487 }
488
489 void KThumb::slotAudioThumbProgress(const int progress)
490 {
491     m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
492 }
493
494 void KThumb::slotAudioThumbOver()
495 {
496     m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
497     m_clipManager->endAudioThumbsGeneration(m_id);
498 }
499
500 void KThumb::askForAudioThumbs(const QString &id)
501 {
502     m_clipManager->askForAudioThumb(id);
503 }
504
505
506 #include "kthumb.moc"
507