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>
42 void MyThread::init(KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth)
46 f.setFileName(target);
49 m_frameLength = frameLength;
50 m_frequency = frequency;
51 m_channels = channels;
52 m_arrayWidth = arrayWidth;
55 bool MyThread::isWorking()
62 if (!f.open(QIODevice::WriteOnly)) {
63 kDebug() << "++++++++ ERROR WRITING TO FILE: " << f.fileName() << endl;
64 kDebug() << "++++++++ DISABLING AUDIO THUMBS" << endl;
65 KdenliveSettings::setAudiothumbnails(false);
69 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
70 Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
73 if (KdenliveSettings::normaliseaudiothumbs()) {
74 Mlt::Filter m_convert(prof, "volume");
75 m_convert.set("gain", "normalise");
76 m_producer.attach(m_convert);
81 kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
82 for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && m_producer.is_valid(); z++) {
84 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
85 if (last_val != val && val > 1) {
86 emit audioThumbProgress(val);
90 Mlt::Frame *mlt_frame = m_producer.get_frame();
91 if (mlt_frame && mlt_frame->is_valid()) {
92 double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer());
93 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
94 mlt_audio_format m_audioFormat = mlt_audio_pcm;
95 qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
97 for (int c = 0; c < m_channels; c++) {
99 m_array.resize(m_arrayWidth);
100 for (int i = 0; i < m_array.size(); i++) {
101 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
107 f.write(QByteArray(m_arrayWidth, '\x00'));
111 //kDebug() << "done";
116 } else emit audioThumbOver();
120 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/) :
122 m_audioThumbProducer(),
127 m_clipManager(clipManager),
129 m_stopAudioThumbs(false)
131 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
132 /*connect(&m_audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
133 connect(&m_audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));*/
138 m_requestedThumbs.clear();
139 if (m_audioThumbProducer.isRunning()) {
140 m_stopAudioThumbs = true;
141 m_audioThumbProducer.waitForFinished();
142 slotAudioThumbOver();
144 m_future.waitForFinished();
147 void KThumb::setProducer(Mlt::Producer *producer)
149 m_requestedThumbs.clear();
150 m_future.waitForFinished();
151 m_producer = producer;
152 // FIXME: the profile() call leaks an object, but trying to free
153 // it leads to a double-free in Profile::~Profile()
154 m_dar = producer->profile()->dar();
157 void KThumb::clearProducer()
162 bool KThumb::hasProducer() const
164 return m_producer != NULL;
167 void KThumb::updateThumbUrl(const QString &hash)
169 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
172 void KThumb::updateClipUrl(KUrl url, const QString &hash)
176 m_producer->set("resource", url.path().toUtf8().constData());
177 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
181 QPixmap KThumb::getImage(KUrl url, int width, int height)
183 if (url.isEmpty()) return QPixmap();
184 return getImage(url, 0, width, height);
187 void KThumb::extractImage(int frame, int frame2)
189 if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
190 if (frame != -1 && !m_requestedThumbs.contains(frame)) m_requestedThumbs.append(frame);
191 if (frame2 != -1 && !m_requestedThumbs.contains(frame2)) m_requestedThumbs.append(frame2);
192 if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &KThumb::doGetThumbs);
195 void KThumb::doGetThumbs()
197 const int theight = KdenliveSettings::trackheight();
198 const int twidth = FRAME_SIZE;//(int)(theight * m_dar + 0.5);
200 while (!m_requestedThumbs.isEmpty()) {
201 int frame = m_requestedThumbs.takeFirst();
203 QImage img = getFrame(m_producer, frame, twidth, theight);
204 emit thumbReady(frame, img);
209 QPixmap KThumb::extractImage(int frame, int width, int height)
211 return QPixmap::fromImage(getFrame(m_producer, frame, width, height));
215 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
217 Mlt::Profile profile(KdenliveSettings::current_profile().toUtf8().constData());
218 QPixmap pix(width, height);
219 if (url.isEmpty()) return pix;
221 //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
222 //Mlt::Producer producer(profile, "xml-string", tmp);
223 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().constData());
225 pix = QPixmap::fromImage(getFrame(producer, frame, width, height));
232 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
234 QImage p(width, height, QImage::Format_ARGB32_Premultiplied);
235 if (producer == NULL) {
240 if (producer->is_blank()) {
245 producer->seek(framepos);
246 Mlt::Frame *frame = producer->get_frame();
248 kDebug() << "///// BROKEN FRAME";
253 /*Mlt::Producer parentProd(producer->parent());
254 Mlt::Service service(parentProd.get_service());
255 mlt_service_lock(service.get_service());*/
258 mlt_image_format format = mlt_image_rgb24a;
259 uint8_t *data = frame->get_image(format, ow, oh, 0);
260 QImage image((uchar *)data, ow, oh, QImage::Format_ARGB32_Premultiplied);
261 //mlt_service_unlock(service.get_service());
263 if (!image.isNull()) {
264 if (ow > (2 * width)) {
265 // there was a scaling problem, do it manually
266 QImage scaled = image.scaled(width, height);
267 p = scaled.rgbSwapped();
268 } else p = image.rgbSwapped();
276 void KThumb::getImage(KUrl url, int frame, int width, int height)
278 if (url.isEmpty()) return;
279 QPixmap image(width, height);
280 Mlt::Producer m_producer(url.path().toUtf8().constData());
281 image.fill(Qt::black);
283 if (m_producer.is_blank()) {
284 emit thumbReady(frame, image);
287 Mlt::Filter m_convert("avcolour_space");
288 m_convert.set("forced", mlt_image_rgb24a);
289 m_producer.attach(m_convert);
290 m_producer.seek(frame);
291 Mlt::Frame * m_frame = m_producer.get_frame();
292 mlt_image_format format = mlt_image_rgb24a;
295 if (m_frame && m_frame->is_valid()) {
296 uint8_t *thumb = m_frame->get_image(format, width, height);
297 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
298 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
300 if (m_frame) delete m_frame;
301 emit thumbReady(frame, image);
304 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
306 if (url.isEmpty()) return;
307 QPixmap image(width, height);
308 Mlt::Producer m_producer(url.path().toUtf8().constData());
309 image.fill(Qt::black);
311 if (m_producer.is_blank()) {
312 emit thumbReady(startframe, image);
313 emit thumbReady(endframe, image);
316 Mlt::Filter m_convert("avcolour_space");
317 m_convert.set("forced", mlt_image_rgb24a);
318 m_producer.attach(m_convert);
319 m_producer.seek(startframe);
320 Mlt::Frame * m_frame = m_producer.get_frame();
321 mlt_image_format format = mlt_image_rgb24a;
325 if (m_frame && m_frame->is_valid()) {
326 uint8_t *thumb = m_frame->get_image(format, width, height);
327 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
328 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
330 if (m_frame) delete m_frame;
331 emit thumbReady(startframe, image);
333 image.fill(Qt::black);
334 m_producer.seek(endframe);
335 m_frame = m_producer.get_frame();
337 if (m_frame && m_frame->is_valid()) {
338 uint8_t *thumb = m_frame->get_image(format, width, height);
339 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
340 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
342 if (m_frame) delete m_frame;
343 emit thumbReady(endframe, image);
346 void KThumb::stopAudioThumbs()
348 if (m_audioThumbProducer.isRunning()) {
349 m_stopAudioThumbs = true;
350 m_audioThumbProducer.waitForFinished();
351 slotAudioThumbOver();
355 void KThumb::removeAudioThumb()
357 if (m_thumbFile.isEmpty()) return;
359 QFile f(m_thumbFile);
363 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
366 slotAudioThumbOver();
369 if (m_audioThumbProducer.isRunning()) {
373 QMap <int, QMap <int, QByteArray> > storeIn;
374 //FIXME: Hardcoded!!!
376 m_channels = channel;
378 QFile f(m_thumbFile);
379 if (f.open(QIODevice::ReadOnly)) {
380 QByteArray channelarray = f.readAll();
382 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
383 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
385 slotAudioThumbOver();
389 kDebug() << "reading audio thumbs from file";
391 int h1 = arrayWidth * m_channels;
392 int h2 = (int) frame * h1;
394 for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
397 for (int c = 0; c < m_channels; c++) {
399 QByteArray m_array(arrayWidth, '\x00');
400 for (int i = 0; i < arrayWidth; i++)
401 m_array[i] = channelarray[h2 + h3 + i];
402 storeIn[z][c] = m_array;
405 emit audioThumbReady(storeIn);
406 slotAudioThumbOver();
408 if (m_audioThumbProducer.isRunning()) return;
409 m_audioThumbFile.setFileName(m_thumbFile);
411 m_frameLength = frameLength;
412 m_arrayWidth = arrayWidth;
413 m_audioThumbProducer = QtConcurrent::run(this, &KThumb::slotCreateAudioThumbs);
414 /*m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
415 m_audioThumbProducer.start(QThread::LowestPriority);*/
416 // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
420 void KThumb::slotCreateAudioThumbs()
422 if (!m_audioThumbFile.open(QIODevice::WriteOnly)) {
423 kDebug() << "++++++++ ERROR WRITING TO FILE: " << m_audioThumbFile.fileName();
424 kDebug() << "++++++++ DISABLING AUDIO THUMBS";
425 KdenliveSettings::setAudiothumbnails(false);
428 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
429 Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
431 if (KdenliveSettings::normaliseaudiothumbs()) {
432 Mlt::Filter m_convert(prof, "volume");
433 m_convert.set("gain", "normalise");
434 m_producer.attach(m_convert);
439 kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
440 for (int z = (int) m_frame; z < (int)(m_frame + m_frameLength) && m_producer.is_valid(); z++) {
441 if (m_stopAudioThumbs) break;
442 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
443 if (last_val != val && val > 1) {
444 slotAudioThumbProgress(val);
448 Mlt::Frame *mlt_frame = m_producer.get_frame();
449 if (mlt_frame && mlt_frame->is_valid()) {
450 double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer());
451 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
452 mlt_audio_format m_audioFormat = mlt_audio_pcm;
453 qint16* m_pcm = static_cast<qint16*>(mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples));
455 for (int c = 0; c < m_channels; c++) {
457 m_array.resize(m_arrayWidth);
458 for (int i = 0; i < m_array.size(); i++) {
459 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
461 m_audioThumbFile.write(m_array);
465 m_audioThumbFile.write(QByteArray(m_arrayWidth, '\x00'));
469 m_audioThumbFile.close();
470 if (m_stopAudioThumbs) {
471 m_audioThumbFile.remove();
473 slotAudioThumbOver();
477 void KThumb::slotAudioThumbProgress(const int progress)
479 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
482 void KThumb::slotAudioThumbOver()
484 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
485 m_clipManager->endAudioThumbsGeneration(m_id);
488 void KThumb::askForAudioThumbs(const QString &id)
490 m_clipManager->askForAudioThumb(id);
494 #include "kthumb.moc"