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