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