]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
clean up reindentation
[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_yuv422;
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(frame2, 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     char *tmp = Render::decodedString(KdenliveSettings::current_profile());
276     Mlt::Profile profile(tmp);
277     delete[] tmp;
278     QPixmap pix(width, height);
279     if (url.isEmpty()) return pix;
280
281     tmp = Render::decodedString(url.path());
282     //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
283     //Mlt::Producer producer(profile, "xml-string", tmp);
284     Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
285     delete[] tmp;
286
287     if (producer->is_blank()) {
288         pix.fill(Qt::black);
289         delete producer;
290         return pix;
291     }
292     pix = getFrame(producer, frame, width, height);
293     delete producer;
294     return pix;
295 }
296
297 //static
298 /*
299 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
300     Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
301     QPixmap pix(width, height);
302     QDomDocument doc;
303     QDomElement mlt = doc.createElement("mlt");
304     QDomElement play = doc.createElement("playlist");
305     doc.appendChild(mlt);
306     mlt.appendChild(play);
307     play.appendChild(doc.importNode(xml, true));
308     char *tmp = Render::decodedString(doc.toString());
309     Mlt::Producer producer(profile, "xml-string", tmp);
310     delete[] tmp;
311
312     if (producer.is_blank()) {
313         pix.fill(Qt::black);
314         return pix;
315     }
316     return getFrame(producer, frame, width, height);
317 }*/
318
319 //static
320 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
321 {
322     if (producer == NULL) {
323         QPixmap p(width, height);
324         p.fill(Qt::red);
325         return p;
326     }
327
328     producer->seek(framepos);
329     Mlt::Frame *frame = producer->get_frame();
330     if (!frame) {
331         kDebug() << "///// BROKEN FRAME";
332         QPixmap p(width, height);
333         p.fill(Qt::red);
334         return p;
335     }
336
337     mlt_image_format format = mlt_image_yuv422;
338     int frame_width = 0;
339     int frame_height = 0;
340     frame->set("normalised_height", height);
341     frame->set("normalised_width", width);
342     QPixmap pix(width, height);
343     uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
344     uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
345     mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
346
347     QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
348
349     if (!image.isNull()) {
350         pix = QPixmap::fromImage(image.rgbSwapped());
351     } else
352         pix.fill(Qt::red);
353
354     mlt_pool_release(new_image);
355     delete frame;
356     return pix;
357 }
358 /*
359 void KThumb::getImage(KUrl url, int frame, int width, int height)
360 {
361     if (url.isEmpty()) return;
362     QPixmap image(width, height);
363     char *tmp = KRender::decodedString(url.path());
364     Mlt::Producer m_producer(tmp);
365     delete tmp;
366     image.fill(Qt::black);
367
368     if (m_producer.is_blank()) {
369  emit thumbReady(frame, image);
370  return;
371     }
372     Mlt::Filter m_convert("avcolour_space");
373     m_convert.set("forced", mlt_image_rgb24a);
374     m_producer.attach(m_convert);
375     m_producer.seek(frame);
376     Mlt::Frame * m_frame = m_producer.get_frame();
377     mlt_image_format format = mlt_image_rgb24a;
378     width = width - 2;
379     height = height - 2;
380     if (m_frame && m_frame->is_valid()) {
381      uint8_t *thumb = m_frame->get_image(format, width, height);
382      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
383      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
384     }
385     if (m_frame) delete m_frame;
386     emit thumbReady(frame, image);
387 }
388
389 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
390 {
391     if (url.isEmpty()) return;
392     QPixmap image(width, height);
393     char *tmp = KRender::decodedString(url.path());
394     Mlt::Producer m_producer(tmp);
395     delete tmp;
396     image.fill(Qt::black);
397
398     if (m_producer.is_blank()) {
399  emit thumbReady(startframe, image);
400  emit thumbReady(endframe, image);
401  return;
402     }
403     Mlt::Filter m_convert("avcolour_space");
404     m_convert.set("forced", mlt_image_rgb24a);
405     m_producer.attach(m_convert);
406     m_producer.seek(startframe);
407     Mlt::Frame * m_frame = m_producer.get_frame();
408     mlt_image_format format = mlt_image_rgb24a;
409     width = width - 2;
410     height = height - 2;
411
412     if (m_frame && m_frame->is_valid()) {
413      uint8_t *thumb = m_frame->get_image(format, width, height);
414      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
415      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
416     }
417     if (m_frame) delete m_frame;
418     emit thumbReady(startframe, image);
419
420     image.fill(Qt::black);
421     m_producer.seek(endframe);
422     m_frame = m_producer.get_frame();
423
424     if (m_frame && m_frame->is_valid()) {
425      uint8_t *thumb = m_frame->get_image(format, width, height);
426      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
427      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
428     }
429     if (m_frame) delete m_frame;
430     emit thumbReady(endframe, image);
431 }
432 */
433 void KThumb::stopAudioThumbs()
434 {
435     if (m_audioThumbProducer.isRunning()) {
436         m_audioThumbProducer.stop_me = true;
437         slotAudioThumbOver();
438     }
439 }
440
441 void KThumb::removeAudioThumb()
442 {
443     if (m_thumbFile.isEmpty()) return;
444     stopAudioThumbs();
445     QFile f(m_thumbFile);
446     f.remove();
447 }
448
449 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
450 {
451     if (channel == 0) {
452         slotAudioThumbOver();
453         return;
454     }
455     if ((m_audioThumbProducer.isRunning() && m_audioThumbProducer.isWorking())) {
456         return;
457     }
458
459     QMap <int, QMap <int, QByteArray> > storeIn;
460     //FIXME: Hardcoded!!!
461     int m_frequency = 48000;
462     int m_channels = channel;
463
464     QFile f(m_thumbFile);
465     if (f.open(QIODevice::ReadOnly)) {
466         QByteArray channelarray = f.readAll();
467         f.close();
468         if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
469             kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
470             f.remove();
471             slotAudioThumbOver();
472             return;
473         }
474         kDebug() << "reading audio thumbs from file";
475         for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
476             for (int c = 0; c < m_channels; c++) {
477                 QByteArray m_array(arrayWidth, '\x00');
478                 for (int i = 0; i < arrayWidth; i++)
479                     m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
480                 storeIn[z][c] = m_array;
481             }
482         }
483         emit audioThumbReady(storeIn);
484         slotAudioThumbOver();
485     } else {
486         if (m_audioThumbProducer.isRunning()) return;
487         m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
488         m_audioThumbProducer.start(QThread::LowestPriority);
489         kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
490     }
491 }
492
493 void KThumb::slotAudioThumbProgress(const int progress)
494 {
495     m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
496 }
497
498 void KThumb::slotAudioThumbOver()
499 {
500     m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
501     m_clipManager->endAudioThumbsGeneration(m_id);
502 }
503
504 void KThumb::askForAudioThumbs(const QString &id)
505 {
506     m_clipManager->askForAudioThumb(id);
507 }
508
509
510 #include "kthumb.moc"
511