]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
Rewrote the handling of timeline in QGraphicsView. Now we use the built in zoom featu...
[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 #include "events.h"
43
44
45 void MyThread::init(QObject *parent, KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth) {
46     stop_me = false;
47     m_parent = parent;
48     m_isWorking = false;
49     f.setFileName(target);
50     m_url = url;
51     m_frame = frame;
52     m_frameLength = frameLength;
53     m_frequency = frequency;
54     m_channels = channels;
55     m_arrayWidth = arrayWidth;
56 }
57
58 bool MyThread::isWorking() {
59     return m_isWorking;
60 }
61
62 void MyThread::run() {
63
64     if (!f.open(QIODevice::WriteOnly)) {
65         kDebug() << "++++++++  ERROR WRITING TO FILE: " << f.fileName() << endl;
66         kDebug() << "++++++++  DISABLING AUDIO THUMBS" << endl;
67         KdenliveSettings::setAudiothumbnails(false);
68         return;
69     }
70     m_isWorking = true;
71     Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
72     Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
73
74
75     if (KdenliveSettings::normaliseaudiothumbs()) {
76         Mlt::Filter m_convert(prof, "volume");
77         m_convert.set("gain", "normalise");
78         m_producer.attach(m_convert);
79     }
80
81     //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
82
83     int last_val = 0;
84     int val = 0;
85     kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
86     for (int z = (int) m_frame;z < (int)(m_frame + m_frameLength) && m_producer.is_valid();z++) {
87         if (stop_me) break;
88         val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
89         if (last_val != val & val > 1) {
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     QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
125
126 }
127
128 void ThumbThread::init(QObject *parent, Mlt::Producer *prod, int width, int height) {
129     stop_me = false;
130     m_parent = parent;
131     m_isWorking = false;
132     m_prod = prod;
133     m_width = width;
134     m_height = height;
135     m_frame1 = -1;
136     m_frame2 = -1;
137 }
138
139 bool ThumbThread::isWorking() {
140     return m_isWorking;
141 }
142
143 void ThumbThread::setThumbFrames(Mlt::Producer *prod, int frame1, int frame2) {
144     if (!m_prod) m_prod = prod;
145     m_frame1 = frame1;
146     m_frame2 = frame2;
147 }
148
149 void ThumbThread::run() {
150     if (m_frame1 != -1 && m_prod) {
151         //mutex.lock();
152         m_prod->seek(m_frame1);
153         Mlt::Frame *avframe = m_prod->get_frame();
154         //mutex.unlock();
155         if (!avframe) {
156             kDebug() << "///// BROKEN FRAME";
157         } else {
158             mlt_image_format format = mlt_image_yuv422;
159             int frame_width = m_width;
160             int frame_height = m_height;
161             avframe->set("normalised_height", m_height);
162             avframe->set("normalised_width", m_width);
163             uint8_t *data = avframe->get_image(format, frame_width, frame_height, 0);
164             uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
165             mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
166
167             QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
168
169             if (!image.isNull()) {
170                 emit gotStartThumb(image.rgbSwapped());
171                 //QApplication::postEvent(m_parent, new ThumbEvent(m_frame1, image.rgbSwapped(), (QEvent::Type)10006));
172                 //pix = QPixmap::fromImage(image.rgbSwapped());
173             } /*else
174   pix.fill(Qt::red);*/
175             mlt_pool_release(new_image);
176             delete avframe;
177         }
178         //pix.fill(Qt::red);
179
180     }
181     if (m_frame2 != -1 && m_prod) {
182         //mutex.lock();
183         m_prod->seek(m_frame2);
184         Mlt::Frame *avframe = m_prod->get_frame();
185         //mutex.unlock();
186         if (!avframe) {
187             kDebug() << "///// BROKEN FRAME";
188         } else {
189             mlt_image_format format = mlt_image_yuv422;
190             int frame_width = 0;
191             int frame_height = 0;
192             avframe->set("normalised_height", m_height);
193             avframe->set("normalised_width", m_width);
194             uint8_t *data = avframe->get_image(format, frame_width, frame_height, 0);
195             uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
196             mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
197
198             QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
199
200             if (!image.isNull()) {
201                 emit gotEndThumb(image.rgbSwapped());
202                 //QApplication::postEvent(m_parent, new ThumbEvent(m_frame2, image.rgbSwapped(), (QEvent::Type)10006));
203                 //pix = QPixmap::fromImage(image.rgbSwapped());
204             } /*else
205   pix.fill(Qt::red);*/
206             mlt_pool_release(new_image);
207             delete avframe;
208         }
209         //pix.fill(Qt::red);
210         //QApplication::postEvent(m_parent, new ThumbEvent(m_frame2, pix, (QEvent::Type)10006));
211     }
212 }
213
214
215 KThumb::KThumb(ClipManager *clipManager, KUrl url, QObject * parent, const char *name): QObject(parent), m_clipManager(clipManager), m_url(url), m_producer(NULL), m_dar(1) {
216     QCryptographicHash context(QCryptographicHash::Sha1);
217     context.addData((KFileItem(m_url, "text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
218     m_thumbFile = KGlobal::dirs()->saveLocation("tmp" , "kdenlive") + context.result().toHex() + ".thumb";
219     kDebug() << "thumbfile=" << m_thumbFile;
220 }
221
222 KThumb::~KThumb() {
223     if (audioThumbProducer.isRunning()) audioThumbProducer.exit();
224 }
225
226 void KThumb::setProducer(Mlt::Producer *producer) {
227     m_producer = producer;
228     m_dar = producer->profile()->dar();
229 }
230
231 void KThumb::updateClipUrl(KUrl url) {
232     m_url = url;
233 }
234
235 //static
236 QPixmap KThumb::getImage(KUrl url, int width, int height) {
237     if (url.isEmpty()) return QPixmap();
238     return getImage(url, 0, width, height);
239 }
240
241 void KThumb::extractImage(int frame, int frame2) {
242     if (m_url.isEmpty() || !KdenliveSettings::videothumbnails() || m_producer == NULL) return;
243
244     int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
245     if (m_producer->is_blank()) {
246         QPixmap pix(twidth, KdenliveSettings::trackheight());
247         pix.fill(Qt::black);
248         emit thumbReady(frame, pix);
249         return;
250     }
251     if (frame != -1) {
252         //videoThumbProducer.getThumb(frame);
253         QPixmap pix = getFrame(m_producer, frame, twidth, KdenliveSettings::trackheight());
254         emit thumbReady(frame, pix);
255     }
256     if (frame2 != -1) {
257         //videoThumbProducer.getThumb(frame2);
258         QPixmap pix = getFrame(m_producer, frame2, twidth , KdenliveSettings::trackheight());
259         emit thumbReady(frame2, pix);
260     }
261 }
262
263 QPixmap KThumb::extractImage(int frame, int width, int height) {
264     return getFrame(m_producer, frame, width, height);
265 }
266
267 //static
268 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height) {
269     Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
270     QPixmap pix(width, height);
271     if (url.isEmpty()) return pix;
272
273     char *tmp = Render::decodedString(url.path());
274     //"<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
275     //Mlt::Producer producer(profile, "westley-xml", tmp);
276     Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
277     delete[] tmp;
278
279     if (producer->is_blank()) {
280         pix.fill(Qt::black);
281         delete producer;
282         return pix;
283     }
284     pix = getFrame(producer, frame, width, height);
285     delete producer;
286     return pix;
287 }
288
289 //static
290 /*
291 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
292     Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
293     QPixmap pix(width, height);
294     QDomDocument doc;
295     QDomElement westley = doc.createElement("westley");
296     QDomElement play = doc.createElement("playlist");
297     doc.appendChild(westley);
298     westley.appendChild(play);
299     play.appendChild(doc.importNode(xml, true));
300     char *tmp = Render::decodedString(doc.toString());
301     Mlt::Producer producer(profile, "westley-xml", tmp);
302     delete[] tmp;
303
304     if (producer.is_blank()) {
305         pix.fill(Qt::black);
306         return pix;
307     }
308     return getFrame(producer, frame, width, height);
309 }*/
310
311 //static
312 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height) {
313     if (producer == NULL) {
314         QPixmap p(width, height);
315         p.fill(Qt::red);
316         return p;
317     }
318
319     producer->seek(framepos);
320     Mlt::Frame *frame = producer->get_frame();
321     if (!frame) {
322         kDebug() << "///// BROKEN FRAME";
323         QPixmap p(width, height);
324         p.fill(Qt::red);
325         return p;
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 = pix.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
428 void KThumb::removeAudioThumb() {
429     if (m_thumbFile.isEmpty()) return;
430     stopAudioThumbs();
431     QFile f(m_thumbFile);
432     f.remove();
433 }
434
435 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth) {
436
437     if ((audioThumbProducer.isRunning() && audioThumbProducer.isWorking()) || channel == 0) {
438         return;
439     }
440
441     QMap <int, QMap <int, QByteArray> > storeIn;
442     //FIXME: Hardcoded!!!
443     int m_frequency = 48000;
444     int m_channels = channel;
445
446     QFile f(m_thumbFile);
447     if (f.open(QIODevice::ReadOnly)) {
448         QByteArray channelarray = f.readAll();
449         f.close();
450         if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
451             kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
452             f.remove();
453             return;
454         }
455         kDebug() << "reading audio thumbs from file";
456         for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
457             for (int c = 0;c < m_channels;c++) {
458                 QByteArray m_array(arrayWidth, '\x00');
459                 for (int i = 0; i < arrayWidth; i++)
460                     m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
461                 storeIn[z][c] = m_array;
462             }
463         }
464         emit audioThumbReady(storeIn);
465     } else {
466         if (audioThumbProducer.isRunning()) return;
467         audioThumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
468         audioThumbProducer.start(QThread::LowestPriority);
469         kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
470     }
471 }
472
473 void KThumb::customEvent(QEvent * event) {
474     if (event->type() == 10005) {
475         ProgressEvent* p = static_cast <ProgressEvent*>(event);
476         m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), p->value());
477     }
478 }
479
480
481
482 #include "kthumb.moc"
483