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