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