]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
Fix broken loading when document profile was different than default profile
[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(KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth)
42 {
43     stop_me = false;
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 {
56     return m_isWorking;
57 }
58
59 void MyThread::run()
60 {
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     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             last_val = val;
87         }
88         m_producer.seek(z);
89         Mlt::Frame *mlt_frame = m_producer.get_frame();
90         if (mlt_frame && mlt_frame->is_valid()) {
91             double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer());
92             int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
93             mlt_audio_format m_audioFormat = mlt_audio_pcm;
94             qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
95
96             for (int c = 0; c < m_channels; c++) {
97                 QByteArray m_array;
98                 m_array.resize(m_arrayWidth);
99                 for (int i = 0; i < m_array.size(); i++) {
100                     m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
101                 }
102                 f.write(m_array);
103
104             }
105         } else {
106             f.write(QByteArray(m_arrayWidth, '\x00'));
107         }
108         delete mlt_frame;
109     }
110     //kDebug() << "done";
111     f.close();
112     m_isWorking = false;
113     if (stop_me) {
114         f.remove();
115     } else emit audioThumbOver();
116 }
117
118 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/) :
119         QObject(parent),
120         m_audioThumbProducer(),
121         m_url(url),
122         m_thumbFile(),
123         m_dar(1),
124         m_producer(NULL),
125         m_clipManager(clipManager),
126         m_id(id)
127 {
128     m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
129     connect(&m_audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
130     connect(&m_audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));
131
132 }
133
134 KThumb::~KThumb()
135 {
136     if (m_audioThumbProducer.isRunning()) {
137         m_audioThumbProducer.stop_me = true;
138         m_audioThumbProducer.wait();
139         slotAudioThumbOver();
140     }
141 }
142
143 void KThumb::setProducer(Mlt::Producer *producer)
144 {
145     m_producer = producer;
146     // FIXME: the profile() call leaks an object, but trying to free
147     // it leads to a double-free in Profile::~Profile()
148     m_dar = producer->profile()->dar();
149 }
150
151 void KThumb::clearProducer()
152 {
153     m_producer = NULL;
154 }
155
156 bool KThumb::hasProducer() const
157 {
158     return m_producer != NULL;
159 }
160
161 void KThumb::updateThumbUrl(const QString &hash)
162 {
163     m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
164 }
165
166 void KThumb::updateClipUrl(KUrl url, const QString &hash)
167 {
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 {
180     if (url.isEmpty()) return QPixmap();
181     return getImage(url, 0, width, height);
182 }
183
184 void KThumb::extractImage(int frame, int frame2)
185 {
186     // kDebug() << "//extract thumb: " << frame << ", " << frame2;
187     if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
188
189     const int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
190     const int theight = KdenliveSettings::trackheight();
191
192     if (frame != -1) {
193         QPixmap pix = getFrame(m_producer, frame, twidth, theight);
194         emit thumbReady(frame, pix);
195     }
196     if (frame2 != -1) {
197         QPixmap pix = getFrame(m_producer, frame2, twidth, theight);
198         emit thumbReady(frame2, pix);
199     }
200 }
201
202 QPixmap KThumb::extractImage(int frame, int width, int height)
203 {
204     return getFrame(m_producer, frame, width, height);
205 }
206
207 //static
208 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
209 {
210     char *tmp = Render::decodedString(KdenliveSettings::current_profile());
211     Mlt::Profile profile(tmp);
212     delete[] tmp;
213     QPixmap pix(width, height);
214     if (url.isEmpty()) return pix;
215
216     tmp = Render::decodedString(url.path());
217     //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
218     //Mlt::Producer producer(profile, "xml-string", tmp);
219     Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
220     delete[] tmp;
221
222     pix = getFrame(producer, frame, width, height);
223     delete producer;
224     return pix;
225 }
226
227
228 //static
229 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
230 {
231     QPixmap p(width, height);
232     if (producer == NULL) {
233         p.fill(Qt::red);
234         return p;
235     }
236
237     if (producer->is_blank()) {
238         p.fill(Qt::black);
239         return p;
240     }
241
242     producer->seek(framepos);
243     Mlt::Frame *frame = producer->get_frame();
244     if (!frame) {
245         kDebug() << "///// BROKEN FRAME";
246         p.fill(Qt::red);
247         return p;
248     }
249
250     /*Mlt::Producer parentProd(producer->parent());
251     Mlt::Service service(parentProd.get_service());
252     mlt_service_lock(service.get_service());*/
253
254     mlt_image_format format = mlt_image_rgb24a;
255     uint8_t *data = frame->get_image(format, width, height, 0);
256     QImage image((uchar *)data, width, height, QImage::Format_ARGB32);
257     //mlt_service_unlock(service.get_service());
258
259     if (!image.isNull()) {
260         p = QPixmap::fromImage(image.rgbSwapped());
261     } else
262         p.fill(Qt::red);
263
264     delete frame;
265     return p;
266 }
267 /*
268 void KThumb::getImage(KUrl url, int frame, int width, int height)
269 {
270     if (url.isEmpty()) return;
271     QPixmap image(width, height);
272     char *tmp = KRender::decodedString(url.path());
273     Mlt::Producer m_producer(tmp);
274     delete tmp;
275     image.fill(Qt::black);
276
277     if (m_producer.is_blank()) {
278  emit thumbReady(frame, image);
279  return;
280     }
281     Mlt::Filter m_convert("avcolour_space");
282     m_convert.set("forced", mlt_image_rgb24a);
283     m_producer.attach(m_convert);
284     m_producer.seek(frame);
285     Mlt::Frame * m_frame = m_producer.get_frame();
286     mlt_image_format format = mlt_image_rgb24a;
287     width = width - 2;
288     height = height - 2;
289     if (m_frame && m_frame->is_valid()) {
290      uint8_t *thumb = m_frame->get_image(format, width, height);
291      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
292      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
293     }
294     if (m_frame) delete m_frame;
295     emit thumbReady(frame, image);
296 }
297
298 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
299 {
300     if (url.isEmpty()) return;
301     QPixmap image(width, height);
302     char *tmp = KRender::decodedString(url.path());
303     Mlt::Producer m_producer(tmp);
304     delete tmp;
305     image.fill(Qt::black);
306
307     if (m_producer.is_blank()) {
308  emit thumbReady(startframe, image);
309  emit thumbReady(endframe, image);
310  return;
311     }
312     Mlt::Filter m_convert("avcolour_space");
313     m_convert.set("forced", mlt_image_rgb24a);
314     m_producer.attach(m_convert);
315     m_producer.seek(startframe);
316     Mlt::Frame * m_frame = m_producer.get_frame();
317     mlt_image_format format = mlt_image_rgb24a;
318     width = width - 2;
319     height = height - 2;
320
321     if (m_frame && m_frame->is_valid()) {
322      uint8_t *thumb = m_frame->get_image(format, width, height);
323      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
324      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
325     }
326     if (m_frame) delete m_frame;
327     emit thumbReady(startframe, image);
328
329     image.fill(Qt::black);
330     m_producer.seek(endframe);
331     m_frame = m_producer.get_frame();
332
333     if (m_frame && m_frame->is_valid()) {
334      uint8_t *thumb = m_frame->get_image(format, width, height);
335      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
336      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
337     }
338     if (m_frame) delete m_frame;
339     emit thumbReady(endframe, image);
340 }
341 */
342 void KThumb::stopAudioThumbs()
343 {
344     if (m_audioThumbProducer.isRunning()) {
345         m_audioThumbProducer.stop_me = true;
346         slotAudioThumbOver();
347     }
348 }
349
350 void KThumb::removeAudioThumb()
351 {
352     if (m_thumbFile.isEmpty()) return;
353     stopAudioThumbs();
354     QFile f(m_thumbFile);
355     f.remove();
356 }
357
358 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
359 {
360     if (channel == 0) {
361         slotAudioThumbOver();
362         return;
363     }
364     if ((m_audioThumbProducer.isRunning() && m_audioThumbProducer.isWorking())) {
365         return;
366     }
367
368     QMap <int, QMap <int, QByteArray> > storeIn;
369     //FIXME: Hardcoded!!!
370     int m_frequency = 48000;
371     int m_channels = channel;
372
373     QFile f(m_thumbFile);
374     if (f.open(QIODevice::ReadOnly)) {
375         QByteArray channelarray = f.readAll();
376         f.close();
377         if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
378             kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
379             f.remove();
380             slotAudioThumbOver();
381             return;
382         }
383         kDebug() << "reading audio thumbs from file";
384         for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
385             for (int c = 0; c < m_channels; c++) {
386                 QByteArray m_array(arrayWidth, '\x00');
387                 for (int i = 0; i < arrayWidth; i++)
388                     m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
389                 storeIn[z][c] = m_array;
390             }
391         }
392         emit audioThumbReady(storeIn);
393         slotAudioThumbOver();
394     } else {
395         if (m_audioThumbProducer.isRunning()) return;
396         m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
397         m_audioThumbProducer.start(QThread::LowestPriority);
398         kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
399     }
400 }
401
402 void KThumb::slotAudioThumbProgress(const int progress)
403 {
404     m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
405 }
406
407 void KThumb::slotAudioThumbOver()
408 {
409     m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
410     m_clipManager->endAudioThumbsGeneration(m_id);
411 }
412
413 void KThumb::askForAudioThumbs(const QString &id)
414 {
415     m_clipManager->askForAudioThumb(id);
416 }
417
418
419 #include "kthumb.moc"
420