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