1 /***************************************************************************
2 krender.cpp - description
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
12 ***************************************************************************/
14 /***************************************************************************
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. *
21 ***************************************************************************/
24 #include "clipmanager.h"
26 #include "kdenlivesettings.h"
28 #include <mlt++/Mlt.h>
30 #include <kio/netaccess.h>
33 #include <kfileitem.h>
34 #include <kmessagebox.h>
35 #include <KStandardDirs>
39 #include <QApplication>
40 #include <QtConcurrentRun>
43 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/) :
45 m_audioThumbProducer(),
50 m_clipManager(clipManager),
52 m_stopAudioThumbs(false)
54 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
59 m_requestedThumbs.clear();
60 if (m_audioThumbProducer.isRunning()) {
61 m_stopAudioThumbs = true;
62 m_audioThumbProducer.waitForFinished();
65 m_future.waitForFinished();
68 void KThumb::setProducer(Mlt::Producer *producer)
70 m_requestedThumbs.clear();
71 m_future.waitForFinished();
72 m_producer = producer;
73 // FIXME: the profile() call leaks an object, but trying to free
74 // it leads to a double-free in Profile::~Profile()
75 m_dar = producer->profile()->dar();
78 void KThumb::clearProducer()
83 bool KThumb::hasProducer() const
85 return m_producer != NULL;
88 void KThumb::updateThumbUrl(const QString &hash)
90 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
93 void KThumb::updateClipUrl(KUrl url, const QString &hash)
97 m_producer->set("resource", url.path().toUtf8().constData());
98 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
102 QPixmap KThumb::getImage(KUrl url, int width, int height)
104 if (url.isEmpty()) return QPixmap();
105 return getImage(url, 0, width, height);
108 void KThumb::extractImage(int frame, int frame2)
110 if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
111 if (frame != -1 && !m_requestedThumbs.contains(frame)) m_requestedThumbs.append(frame);
112 if (frame2 != -1 && !m_requestedThumbs.contains(frame2)) m_requestedThumbs.append(frame2);
113 if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &KThumb::doGetThumbs);
116 void KThumb::doGetThumbs()
118 const int theight = KdenliveSettings::trackheight();
119 const int twidth = FRAME_SIZE;//(int)(theight * m_dar + 0.5);
121 while (!m_requestedThumbs.isEmpty()) {
122 int frame = m_requestedThumbs.takeFirst();
124 QImage img = getFrame(m_producer, frame, twidth, theight);
125 emit thumbReady(frame, img);
130 QPixmap KThumb::extractImage(int frame, int width, int height)
132 return QPixmap::fromImage(getFrame(m_producer, frame, width, height));
136 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
138 Mlt::Profile profile(KdenliveSettings::current_profile().toUtf8().constData());
139 QPixmap pix(width, height);
140 if (url.isEmpty()) return pix;
142 //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
143 //Mlt::Producer producer(profile, "xml-string", tmp);
144 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
146 pix = QPixmap::fromImage(getFrame(producer, frame, width, height));
153 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
155 QImage p(width, height, QImage::Format_ARGB32_Premultiplied);
156 if (producer == NULL) {
161 if (producer->is_blank()) {
166 producer->seek(framepos);
167 Mlt::Frame *frame = producer->get_frame();
169 kDebug() << "///// BROKEN FRAME";
174 /*Mlt::Producer parentProd(producer->parent());
175 Mlt::Service service(parentProd.get_service());
176 mlt_service_lock(service.get_service());*/
179 mlt_image_format format = mlt_image_rgb24a;
180 uint8_t *data = frame->get_image(format, ow, oh, 0);
181 QImage image((uchar *)data, ow, oh, QImage::Format_ARGB32_Premultiplied);
182 //mlt_service_unlock(service.get_service());
184 if (!image.isNull()) {
185 if (ow > (2 * width)) {
186 // there was a scaling problem, do it manually
187 QImage scaled = image.scaled(width, height);
188 p = scaled.rgbSwapped();
189 } else p = image.rgbSwapped();
197 void KThumb::getImage(KUrl url, int frame, int width, int height)
199 if (url.isEmpty()) return;
200 QPixmap image(width, height);
201 Mlt::Producer m_producer(url.path().toUtf8().constData());
202 image.fill(Qt::black);
204 if (m_producer.is_blank()) {
205 emit thumbReady(frame, image);
208 Mlt::Filter m_convert("avcolour_space");
209 m_convert.set("forced", mlt_image_rgb24a);
210 m_producer.attach(m_convert);
211 m_producer.seek(frame);
212 Mlt::Frame * m_frame = m_producer.get_frame();
213 mlt_image_format format = mlt_image_rgb24a;
216 if (m_frame && m_frame->is_valid()) {
217 uint8_t *thumb = m_frame->get_image(format, width, height);
218 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
219 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
221 if (m_frame) delete m_frame;
222 emit thumbReady(frame, image);
225 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
227 if (url.isEmpty()) return;
228 QPixmap image(width, height);
229 Mlt::Producer m_producer(url.path().toUtf8().constData());
230 image.fill(Qt::black);
232 if (m_producer.is_blank()) {
233 emit thumbReady(startframe, image);
234 emit thumbReady(endframe, image);
237 Mlt::Filter m_convert("avcolour_space");
238 m_convert.set("forced", mlt_image_rgb24a);
239 m_producer.attach(m_convert);
240 m_producer.seek(startframe);
241 Mlt::Frame * m_frame = m_producer.get_frame();
242 mlt_image_format format = mlt_image_rgb24a;
246 if (m_frame && m_frame->is_valid()) {
247 uint8_t *thumb = m_frame->get_image(format, width, height);
248 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
249 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
251 if (m_frame) delete m_frame;
252 emit thumbReady(startframe, image);
254 image.fill(Qt::black);
255 m_producer.seek(endframe);
256 m_frame = m_producer.get_frame();
258 if (m_frame && m_frame->is_valid()) {
259 uint8_t *thumb = m_frame->get_image(format, width, height);
260 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
261 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
263 if (m_frame) delete m_frame;
264 emit thumbReady(endframe, image);
267 void KThumb::stopAudioThumbs()
269 if (m_audioThumbProducer.isRunning()) {
270 m_stopAudioThumbs = true;
271 m_audioThumbProducer.waitForFinished();
272 slotAudioThumbOver();
276 void KThumb::removeAudioThumb()
278 if (m_thumbFile.isEmpty()) return;
280 QFile f(m_thumbFile);
284 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
287 slotAudioThumbOver();
290 if (m_audioThumbProducer.isRunning()) {
294 QMap <int, QMap <int, QByteArray> > storeIn;
295 //FIXME: Hardcoded!!!
297 m_channels = channel;
299 QFile f(m_thumbFile);
300 if (f.open(QIODevice::ReadOnly)) {
301 QByteArray channelarray = f.readAll();
303 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
304 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
306 slotAudioThumbOver();
310 kDebug() << "reading audio thumbs from file";
312 int h1 = arrayWidth * m_channels;
313 int h2 = (int) frame * h1;
315 for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
318 for (int c = 0; c < m_channels; c++) {
320 QByteArray m_array(arrayWidth, '\x00');
321 for (int i = 0; i < arrayWidth; i++)
322 m_array[i] = channelarray[h2 + h3 + i];
323 storeIn[z][c] = m_array;
326 emit audioThumbReady(storeIn);
327 slotAudioThumbOver();
329 if (m_audioThumbProducer.isRunning()) return;
330 m_audioThumbFile.setFileName(m_thumbFile);
332 m_frameLength = frameLength;
333 m_arrayWidth = arrayWidth;
334 m_audioThumbProducer = QtConcurrent::run(this, &KThumb::slotCreateAudioThumbs);
335 /*m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
336 m_audioThumbProducer.start(QThread::LowestPriority);*/
337 // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
341 void KThumb::slotCreateAudioThumbs()
343 if (!m_audioThumbFile.open(QIODevice::WriteOnly)) {
344 kDebug() << "++++++++ ERROR WRITING TO FILE: " << m_audioThumbFile.fileName();
345 kDebug() << "++++++++ DISABLING AUDIO THUMBS";
346 KdenliveSettings::setAudiothumbnails(false);
349 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
350 Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
352 if (KdenliveSettings::normaliseaudiothumbs()) {
353 Mlt::Filter m_convert(prof, "volume");
354 m_convert.set("gain", "normalise");
355 m_producer.attach(m_convert);
360 kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
361 for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && m_producer.is_valid(); z++) {
362 if (m_stopAudioThumbs) break;
363 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
364 if (last_val != val && val > 1) {
365 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), val);
369 Mlt::Frame *mlt_frame = m_producer.get_frame();
370 if (mlt_frame && mlt_frame->is_valid()) {
371 double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer());
372 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
373 mlt_audio_format m_audioFormat = mlt_audio_pcm;
374 qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
376 for (int c = 0; c < m_channels; c++) {
378 m_array.resize(m_arrayWidth);
379 for (int i = 0; i < m_array.size(); i++) {
380 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
382 m_audioThumbFile.write(m_array);
386 m_audioThumbFile.write(QByteArray(m_arrayWidth, '\x00'));
390 m_audioThumbFile.close();
391 if (m_stopAudioThumbs) {
392 m_audioThumbFile.remove();
394 slotAudioThumbOver();
398 void KThumb::slotAudioThumbOver()
400 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
401 m_clipManager->endAudioThumbsGeneration(m_id);
404 void KThumb::askForAudioThumbs(const QString &id)
406 m_clipManager->askForAudioThumb(id);
410 #include "kthumb.moc"