]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
Reindent the codebase using 'linux' bracket placement.
[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(QObject *parent, KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth)
42 {
43     stop_me = false;
44     m_parent = parent;
45     m_isWorking = false;
46     f.setFileName(target);
47     m_url = url;
48     m_frame = frame;
49     m_frameLength = frameLength;
50     m_frequency = frequency;
51     m_channels = channels;
52     m_arrayWidth = arrayWidth;
53 }
54
55 bool MyThread::isWorking()
56 {
57     return m_isWorking;
58 }
59
60 void MyThread::run()
61 {
62     if (!f.open(QIODevice::WriteOnly)) {
63         kDebug() << "++++++++  ERROR WRITING TO FILE: " << f.fileName() << endl;
64         kDebug() << "++++++++  DISABLING AUDIO THUMBS" << endl;
65         KdenliveSettings::setAudiothumbnails(false);
66         return;
67     }
68     m_isWorking = true;
69     Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
70     Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
71
72
73     if (KdenliveSettings::normaliseaudiothumbs()) {
74         Mlt::Filter m_convert(prof, "volume");
75         m_convert.set("gain", "normalise");
76         m_producer.attach(m_convert);
77     }
78
79     //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
80
81     int last_val = 0;
82     int val = 0;
83     kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
84     for (int z = (int) m_frame;z < (int)(m_frame + m_frameLength) && m_producer.is_valid();z++) {
85         if (stop_me) break;
86         val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
87         if (last_val != val && val > 1) {
88             emit audioThumbProgress(val);
89             //QApplication::postEvent(m_parent, new ProgressEvent(val, (QEvent::Type)10005));
90
91             last_val = val;
92         }
93         m_producer.seek(z);
94         Mlt::Frame *mlt_frame = m_producer.get_frame();
95         if (mlt_frame && mlt_frame->is_valid()) {
96             double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer());   //mlt_frame->get_double( "fps" );
97             int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
98             mlt_audio_format m_audioFormat = mlt_audio_pcm;
99
100             qint16* m_pcm = mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples);
101
102             for (int c = 0;c < m_channels;c++) {
103                 QByteArray m_array;
104                 m_array.resize(m_arrayWidth);
105                 for (uint i = 0; i < m_array.size(); i++) {
106                     m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
107                 }
108                 f.write(m_array);
109
110             }
111         } else {
112             f.write(QByteArray(m_arrayWidth, '\x00'));
113         }
114         if (mlt_frame)
115             delete mlt_frame;
116     }
117     kDebug() << "done";
118     f.close();
119     m_isWorking = false;
120     if (stop_me) {
121         f.remove();
122     }
123     emit audioThumbOver();
124     //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
125
126 }
127
128 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/)
129         : QObject(parent),
130         audioThumbProducer(),
131         m_url(url),
132         m_thumbFile(),
133         m_dar(1),
134         m_producer(NULL),
135         m_clipManager(clipManager),
136         m_id(id),
137         m_mainFrame(-1)
138 {
139     m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
140     connect(&audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
141     connect(&audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));
142
143 }
144
145 KThumb::~KThumb()
146 {
147     if (audioThumbProducer.isRunning()) {
148         slotAudioThumbOver();
149         audioThumbProducer.stop_me = true;
150         audioThumbProducer.wait(300);
151     }
152 }
153
154 void KThumb::setProducer(Mlt::Producer *producer)
155 {
156     m_producer = producer;
157     m_dar = producer->profile()->dar();
158 }
159
160 void KThumb::clearProducer()
161 {
162     m_producer = NULL;
163 }
164
165 bool KThumb::hasProducer() const
166 {
167     return m_producer != NULL;
168 }
169
170 void KThumb::updateThumbUrl(const QString &hash)
171 {
172     m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
173 }
174
175 void KThumb::updateClipUrl(KUrl url, const QString &hash)
176 {
177     m_url = url;
178     if (m_producer) {
179         char *tmp = Render::decodedString(url.path());
180         m_producer->set("resource", tmp);
181         delete[] tmp;
182     }
183     m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
184 }
185
186 //static
187 QPixmap KThumb::getImage(KUrl url, int width, int height)
188 {
189     if (url.isEmpty()) return QPixmap();
190     return getImage(url, 0, width, height);
191 }
192
193 void KThumb::extractImage(int frame, int frame2)
194 {
195     if (m_url.isEmpty() || !KdenliveSettings::videothumbnails() || m_producer == NULL) return;
196
197     const int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
198     const int theight = KdenliveSettings::trackheight();
199
200     mlt_image_format format = mlt_image_yuv422;
201     if (m_producer->is_blank()) {
202         QPixmap pix(twidth, theight);
203         pix.fill(Qt::black);
204         emit thumbReady(frame, pix);
205         return;
206     }
207     Mlt::Frame *mltFrame;
208     if (frame != -1) {
209         //videoThumbProducer.getThumb(frame);
210         m_producer->seek(frame);
211         mltFrame = m_producer->get_frame();
212         if (frame2 != -1) m_producer->seek(frame2);
213         if (!mltFrame) {
214             kDebug() << "///// BROKEN FRAME";
215             QPixmap p(twidth, theight);
216             p.fill(Qt::red);
217             emit thumbReady(frame, p);
218             return;
219         } else {
220             int frame_width = 0;
221             int frame_height = 0;
222             mltFrame->set("normalised_height", theight);
223             mltFrame->set("normalised_width", twidth);
224             QPixmap pix(twidth, theight);
225             uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
226             uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
227             mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
228
229             QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
230
231             if (!image.isNull()) {
232                 pix = QPixmap::fromImage(image.rgbSwapped());
233             } else
234                 pix.fill(Qt::red);
235
236             mlt_pool_release(new_image);
237             delete mltFrame;
238             emit thumbReady(frame, pix);
239         }
240     } else if (frame2 != -1) m_producer->seek(frame2);
241     if (frame2 != -1) {
242         mltFrame = m_producer->get_frame();
243         if (!mltFrame) {
244             kDebug() << "///// BROKEN FRAME";
245             QPixmap p(twidth, theight);
246             p.fill(Qt::red);
247             emit thumbReady(frame, p);
248             return;
249         } else {
250             int frame_width = 0;
251             int frame_height = 0;
252             mltFrame->set("normalised_height", theight);
253             mltFrame->set("normalised_width", twidth);
254             QPixmap pix(twidth, theight);
255             uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
256             uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
257             mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
258
259             QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
260
261             if (!image.isNull()) {
262                 pix = QPixmap::fromImage(image.rgbSwapped());
263             } else
264                 pix.fill(Qt::red);
265
266             mlt_pool_release(new_image);
267             delete mltFrame;
268             emit thumbReady(frame2, pix);
269         }
270     }
271 }
272
273 QPixmap KThumb::extractImage(int frame, int width, int height)
274 {
275     return getFrame(m_producer, frame, width, height);
276 }
277
278 //static
279 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
280 {
281     Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
282     QPixmap pix(width, height);
283     if (url.isEmpty()) return pix;
284
285     char *tmp = Render::decodedString(url.path());
286     //"<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
287     //Mlt::Producer producer(profile, "westley-xml", tmp);
288     Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
289     delete[] tmp;
290
291     if (producer->is_blank()) {
292         pix.fill(Qt::black);
293         delete producer;
294         return pix;
295     }
296     pix = getFrame(producer, frame, width, height);
297     delete producer;
298     return pix;
299 }
300
301 //static
302 /*
303 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
304     Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
305     QPixmap pix(width, height);
306     QDomDocument doc;
307     QDomElement westley = doc.createElement("westley");
308     QDomElement play = doc.createElement("playlist");
309     doc.appendChild(westley);
310     westley.appendChild(play);
311     play.appendChild(doc.importNode(xml, true));
312     char *tmp = Render::decodedString(doc.toString());
313     Mlt::Producer producer(profile, "westley-xml", tmp);
314     delete[] tmp;
315
316     if (producer.is_blank()) {
317         pix.fill(Qt::black);
318         return pix;
319     }
320     return getFrame(producer, frame, width, height);
321 }*/
322
323 //static
324 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
325 {
326     if (producer == NULL) {
327         QPixmap p(width, height);
328         p.fill(Qt::red);
329         return p;
330     }
331
332     producer->seek(framepos);
333     Mlt::Frame *frame = producer->get_frame();
334     if (!frame) {
335         kDebug() << "///// BROKEN FRAME";
336         QPixmap p(width, height);
337         p.fill(Qt::red);
338         return p;
339     }
340
341     mlt_image_format format = mlt_image_yuv422;
342     int frame_width = 0;
343     int frame_height = 0;
344     frame->set("normalised_height", height);
345     frame->set("normalised_width", width);
346     QPixmap pix(width, height);
347     uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
348     uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
349     mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
350
351     QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
352
353     if (!image.isNull()) {
354         pix = QPixmap::fromImage(image.rgbSwapped());
355     } else
356         pix.fill(Qt::red);
357
358     mlt_pool_release(new_image);
359     delete frame;
360     return pix;
361 }
362 /*
363 void KThumb::getImage(KUrl url, int frame, int width, int height)
364 {
365     if (url.isEmpty()) return;
366     QPixmap image(width, height);
367     char *tmp = KRender::decodedString(url.path());
368     Mlt::Producer m_producer(tmp);
369     delete tmp;
370     image.fill(Qt::black);
371
372     if (m_producer.is_blank()) {
373  emit thumbReady(frame, image);
374  return;
375     }
376     Mlt::Filter m_convert("avcolour_space");
377     m_convert.set("forced", mlt_image_rgb24a);
378     m_producer.attach(m_convert);
379     m_producer.seek(frame);
380     Mlt::Frame * m_frame = m_producer.get_frame();
381     mlt_image_format format = mlt_image_rgb24a;
382     width = width - 2;
383     height = height - 2;
384     if (m_frame && m_frame->is_valid()) {
385      uint8_t *thumb = m_frame->get_image(format, width, height);
386      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
387      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
388     }
389     if (m_frame) delete m_frame;
390     emit thumbReady(frame, image);
391 }
392
393 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
394 {
395     if (url.isEmpty()) return;
396     QPixmap image(width, height);
397     char *tmp = KRender::decodedString(url.path());
398     Mlt::Producer m_producer(tmp);
399     delete tmp;
400     image.fill(Qt::black);
401
402     if (m_producer.is_blank()) {
403  emit thumbReady(startframe, image);
404  emit thumbReady(endframe, image);
405  return;
406     }
407     Mlt::Filter m_convert("avcolour_space");
408     m_convert.set("forced", mlt_image_rgb24a);
409     m_producer.attach(m_convert);
410     m_producer.seek(startframe);
411     Mlt::Frame * m_frame = m_producer.get_frame();
412     mlt_image_format format = mlt_image_rgb24a;
413     width = width - 2;
414     height = height - 2;
415
416     if (m_frame && m_frame->is_valid()) {
417      uint8_t *thumb = m_frame->get_image(format, width, height);
418      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
419      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
420     }
421     if (m_frame) delete m_frame;
422     emit thumbReady(startframe, image);
423
424     image.fill(Qt::black);
425     m_producer.seek(endframe);
426     m_frame = m_producer.get_frame();
427
428     if (m_frame && m_frame->is_valid()) {
429      uint8_t *thumb = m_frame->get_image(format, width, height);
430      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
431      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
432     }
433     if (m_frame) delete m_frame;
434     emit thumbReady(endframe, image);
435 }
436 */
437 void KThumb::stopAudioThumbs()
438 {
439     if (audioThumbProducer.isRunning()) audioThumbProducer.stop_me = true;
440 }
441
442 void KThumb::removeAudioThumb()
443 {
444     if (m_thumbFile.isEmpty()) return;
445     stopAudioThumbs();
446     QFile f(m_thumbFile);
447     f.remove();
448 }
449
450 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
451 {
452     if (channel == 0) {
453         slotAudioThumbOver();
454         return;
455     }
456     if ((audioThumbProducer.isRunning() && audioThumbProducer.isWorking())) {
457         return;
458     }
459
460     QMap <int, QMap <int, QByteArray> > storeIn;
461     //FIXME: Hardcoded!!!
462     int m_frequency = 48000;
463     int m_channels = channel;
464
465     QFile f(m_thumbFile);
466     if (f.open(QIODevice::ReadOnly)) {
467         QByteArray channelarray = f.readAll();
468         f.close();
469         if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
470             kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
471             f.remove();
472             slotAudioThumbOver();
473             return;
474         }
475         kDebug() << "reading audio thumbs from file";
476         for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
477             for (int c = 0;c < m_channels;c++) {
478                 QByteArray m_array(arrayWidth, '\x00');
479                 for (int i = 0; i < arrayWidth; i++)
480                     m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
481                 storeIn[z][c] = m_array;
482             }
483         }
484         emit audioThumbReady(storeIn);
485         slotAudioThumbOver();
486     } else {
487         if (audioThumbProducer.isRunning()) return;
488         audioThumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
489         audioThumbProducer.start(QThread::LowestPriority);
490         kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
491     }
492 }
493
494 void KThumb::slotAudioThumbProgress(const int progress)
495 {
496     m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
497 }
498
499 void KThumb::slotAudioThumbOver()
500 {
501     m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
502     m_clipManager->endAudioThumbsGeneration(m_id);
503 }
504
505 void KThumb::askForAudioThumbs(const QString &id)
506 {
507     m_clipManager->askForAudioThumb(id);
508 }
509
510
511 #include "kthumb.moc"
512