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