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