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),
130 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
131 connect(&m_audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
132 connect(&m_audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));
137 m_requestedThumbs.clear();
138 if (m_audioThumbProducer.isRunning()) {
139 m_audioThumbProducer.stop_me = true;
140 m_audioThumbProducer.wait();
141 slotAudioThumbOver();
143 m_future.waitForFinished();
146 void KThumb::setProducer(Mlt::Producer *producer)
148 m_requestedThumbs.clear();
149 m_future.waitForFinished();
150 m_producer = producer;
151 // FIXME: the profile() call leaks an object, but trying to free
152 // it leads to a double-free in Profile::~Profile()
153 m_dar = producer->profile()->dar();
156 void KThumb::clearProducer()
161 bool KThumb::hasProducer() const
163 return m_producer != NULL;
166 void KThumb::updateThumbUrl(const QString &hash)
168 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
171 void KThumb::updateClipUrl(KUrl url, const QString &hash)
175 char *tmp = Render::decodedString(url.path());
176 m_producer->set("resource", tmp);
179 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
183 QPixmap KThumb::getImage(KUrl url, int width, int height)
185 if (url.isEmpty()) return QPixmap();
186 return getImage(url, 0, width, height);
189 void KThumb::extractImage(int frame, int frame2)
191 if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
192 if (frame != -1 && !m_requestedThumbs.contains(frame)) m_requestedThumbs.append(frame);
193 if (frame2 != -1 && !m_requestedThumbs.contains(frame2)) m_requestedThumbs.append(frame2);
194 if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &KThumb::doGetThumbs);
197 void KThumb::doGetThumbs()
199 const int theight = KdenliveSettings::trackheight();
200 const int twidth = FRAME_SIZE;//(int)(theight * m_dar + 0.5);
202 while (!m_requestedThumbs.isEmpty()) {
203 int frame = m_requestedThumbs.takeFirst();
205 QImage img = getFrame(m_producer, frame, twidth, theight);
206 emit thumbReady(frame, img);
211 QPixmap KThumb::extractImage(int frame, int width, int height)
213 return QPixmap::fromImage(getFrame(m_producer, frame, width, height));
217 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
219 char *tmp = Render::decodedString(KdenliveSettings::current_profile());
220 Mlt::Profile profile(tmp);
222 QPixmap pix(width, height);
223 if (url.isEmpty()) return pix;
225 tmp = Render::decodedString(url.path());
226 //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
227 //Mlt::Producer producer(profile, "xml-string", tmp);
228 Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
231 pix = QPixmap::fromImage(getFrame(producer, frame, width, height));
238 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
240 QImage p(width, height, QImage::Format_ARGB32);
241 if (producer == NULL) {
246 if (producer->is_blank()) {
251 producer->seek(framepos);
252 Mlt::Frame *frame = producer->get_frame();
254 kDebug() << "///// BROKEN FRAME";
259 /*Mlt::Producer parentProd(producer->parent());
260 Mlt::Service service(parentProd.get_service());
261 mlt_service_lock(service.get_service());*/
264 mlt_image_format format = mlt_image_rgb24a;
265 uint8_t *data = frame->get_image(format, ow, oh, 0);
266 QImage image((uchar *)data, ow, oh, QImage::Format_ARGB32);
267 //mlt_service_unlock(service.get_service());
269 if (!image.isNull()) {
270 if (ow > (2 * width)) {
271 // there was a scaling problem, do it manually
272 QImage scaled = image.scaled(width, height);
273 p = scaled.rgbSwapped();
274 } else p = image.rgbSwapped();
282 void KThumb::getImage(KUrl url, int frame, int width, int height)
284 if (url.isEmpty()) return;
285 QPixmap image(width, height);
286 char *tmp = KRender::decodedString(url.path());
287 Mlt::Producer m_producer(tmp);
289 image.fill(Qt::black);
291 if (m_producer.is_blank()) {
292 emit thumbReady(frame, image);
295 Mlt::Filter m_convert("avcolour_space");
296 m_convert.set("forced", mlt_image_rgb24a);
297 m_producer.attach(m_convert);
298 m_producer.seek(frame);
299 Mlt::Frame * m_frame = m_producer.get_frame();
300 mlt_image_format format = mlt_image_rgb24a;
303 if (m_frame && m_frame->is_valid()) {
304 uint8_t *thumb = m_frame->get_image(format, width, height);
305 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
306 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
308 if (m_frame) delete m_frame;
309 emit thumbReady(frame, image);
312 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
314 if (url.isEmpty()) return;
315 QPixmap image(width, height);
316 char *tmp = KRender::decodedString(url.path());
317 Mlt::Producer m_producer(tmp);
319 image.fill(Qt::black);
321 if (m_producer.is_blank()) {
322 emit thumbReady(startframe, image);
323 emit thumbReady(endframe, image);
326 Mlt::Filter m_convert("avcolour_space");
327 m_convert.set("forced", mlt_image_rgb24a);
328 m_producer.attach(m_convert);
329 m_producer.seek(startframe);
330 Mlt::Frame * m_frame = m_producer.get_frame();
331 mlt_image_format format = mlt_image_rgb24a;
335 if (m_frame && m_frame->is_valid()) {
336 uint8_t *thumb = m_frame->get_image(format, width, height);
337 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
338 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
340 if (m_frame) delete m_frame;
341 emit thumbReady(startframe, image);
343 image.fill(Qt::black);
344 m_producer.seek(endframe);
345 m_frame = m_producer.get_frame();
347 if (m_frame && m_frame->is_valid()) {
348 uint8_t *thumb = m_frame->get_image(format, width, height);
349 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
350 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
352 if (m_frame) delete m_frame;
353 emit thumbReady(endframe, image);
356 void KThumb::stopAudioThumbs()
358 if (m_audioThumbProducer.isRunning()) {
359 m_audioThumbProducer.stop_me = true;
360 slotAudioThumbOver();
364 void KThumb::removeAudioThumb()
366 if (m_thumbFile.isEmpty()) return;
368 QFile f(m_thumbFile);
372 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
375 slotAudioThumbOver();
378 if ((m_audioThumbProducer.isRunning() && m_audioThumbProducer.isWorking())) {
382 QMap <int, QMap <int, QByteArray> > storeIn;
383 //FIXME: Hardcoded!!!
384 int m_frequency = 48000;
385 int m_channels = channel;
387 QFile f(m_thumbFile);
388 if (f.open(QIODevice::ReadOnly)) {
389 QByteArray channelarray = f.readAll();
391 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
392 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
394 slotAudioThumbOver();
397 kDebug() << "reading audio thumbs from file";
398 for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
399 for (int c = 0; c < m_channels; c++) {
400 QByteArray m_array(arrayWidth, '\x00');
401 for (int i = 0; i < arrayWidth; i++)
402 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
403 storeIn[z][c] = m_array;
406 emit audioThumbReady(storeIn);
407 slotAudioThumbOver();
409 if (m_audioThumbProducer.isRunning()) return;
410 m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
411 m_audioThumbProducer.start(QThread::LowestPriority);
412 // kDebug() << "STARTING GENERATE THMB FOR: " <<m_id<<", URL: "<< m_url << " ................................";
416 void KThumb::slotAudioThumbProgress(const int progress)
418 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
421 void KThumb::slotAudioThumbOver()
423 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
424 m_clipManager->endAudioThumbsGeneration(m_id);
427 void KThumb::askForAudioThumbs(const QString &id)
429 m_clipManager->askForAudioThumb(id);
433 #include "kthumb.moc"