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