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>
41 void MyThread::init(KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth)
45 f.setFileName(target);
48 m_frameLength = frameLength;
49 m_frequency = frequency;
50 m_channels = channels;
51 m_arrayWidth = arrayWidth;
54 bool MyThread::isWorking()
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);
68 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
69 Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
72 if (KdenliveSettings::normaliseaudiothumbs()) {
73 Mlt::Filter m_convert(prof, "volume");
74 m_convert.set("gain", "normalise");
75 m_producer.attach(m_convert);
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++) {
83 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
84 if (last_val != val && val > 1) {
85 emit audioThumbProgress(val);
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));
96 for (int c = 0; c < m_channels; c++) {
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 ;
106 f.write(QByteArray(m_arrayWidth, '\x00'));
110 //kDebug() << "done";
115 } else emit audioThumbOver();
118 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/) :
120 m_audioThumbProducer(),
125 m_clipManager(clipManager),
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()));
136 if (m_audioThumbProducer.isRunning()) {
137 m_audioThumbProducer.stop_me = true;
138 m_audioThumbProducer.wait();
139 slotAudioThumbOver();
143 void KThumb::setProducer(Mlt::Producer *producer)
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();
151 void KThumb::clearProducer()
156 bool KThumb::hasProducer() const
158 return m_producer != NULL;
161 void KThumb::updateThumbUrl(const QString &hash)
163 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
166 void KThumb::updateClipUrl(KUrl url, const QString &hash)
170 char *tmp = Render::decodedString(url.path());
171 m_producer->set("resource", tmp);
174 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
178 QPixmap KThumb::getImage(KUrl url, int width, int height)
180 if (url.isEmpty()) return QPixmap();
181 return getImage(url, 0, width, height);
184 void KThumb::extractImage(int frame, int frame2)
186 // kDebug() << "//extract thumb: " << frame << ", " << frame2;
187 if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
189 const int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
190 const int theight = KdenliveSettings::trackheight();
193 QPixmap pix = QPixmap::fromImage(getFrame(m_producer, frame, twidth, theight));
194 emit thumbReady(frame, pix);
197 QPixmap pix = QPixmap::fromImage(getFrame(m_producer, frame2, twidth, theight));
198 emit thumbReady(frame2, pix);
202 QPixmap KThumb::extractImage(int frame, int width, int height)
204 return QPixmap::fromImage(getFrame(m_producer, frame, width, height));
208 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
210 char *tmp = Render::decodedString(KdenliveSettings::current_profile());
211 Mlt::Profile profile(tmp);
213 QPixmap pix(width, height);
214 if (url.isEmpty()) return pix;
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);
222 pix = QPixmap::fromImage(getFrame(producer, frame, width, height));
229 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
231 QImage p(width, height, QImage::Format_ARGB32);
232 if (producer == NULL) {
237 if (producer->is_blank()) {
242 producer->seek(framepos);
243 Mlt::Frame *frame = producer->get_frame();
245 kDebug() << "///// BROKEN FRAME";
250 /*Mlt::Producer parentProd(producer->parent());
251 Mlt::Service service(parentProd.get_service());
252 mlt_service_lock(service.get_service());*/
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());
259 if (!image.isNull()) {
260 p = image.rgbSwapped();
268 void KThumb::getImage(KUrl url, int frame, int width, int height)
270 if (url.isEmpty()) return;
271 QPixmap image(width, height);
272 char *tmp = KRender::decodedString(url.path());
273 Mlt::Producer m_producer(tmp);
275 image.fill(Qt::black);
277 if (m_producer.is_blank()) {
278 emit thumbReady(frame, image);
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;
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);
294 if (m_frame) delete m_frame;
295 emit thumbReady(frame, image);
298 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
300 if (url.isEmpty()) return;
301 QPixmap image(width, height);
302 char *tmp = KRender::decodedString(url.path());
303 Mlt::Producer m_producer(tmp);
305 image.fill(Qt::black);
307 if (m_producer.is_blank()) {
308 emit thumbReady(startframe, image);
309 emit thumbReady(endframe, image);
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;
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);
326 if (m_frame) delete m_frame;
327 emit thumbReady(startframe, image);
329 image.fill(Qt::black);
330 m_producer.seek(endframe);
331 m_frame = m_producer.get_frame();
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);
338 if (m_frame) delete m_frame;
339 emit thumbReady(endframe, image);
342 void KThumb::stopAudioThumbs()
344 if (m_audioThumbProducer.isRunning()) {
345 m_audioThumbProducer.stop_me = true;
346 slotAudioThumbOver();
350 void KThumb::removeAudioThumb()
352 if (m_thumbFile.isEmpty()) return;
354 QFile f(m_thumbFile);
358 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
361 slotAudioThumbOver();
364 if ((m_audioThumbProducer.isRunning() && m_audioThumbProducer.isWorking())) {
368 QMap <int, QMap <int, QByteArray> > storeIn;
369 //FIXME: Hardcoded!!!
370 int m_frequency = 48000;
371 int m_channels = channel;
373 QFile f(m_thumbFile);
374 if (f.open(QIODevice::ReadOnly)) {
375 QByteArray channelarray = f.readAll();
377 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
378 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
380 slotAudioThumbOver();
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;
392 emit audioThumbReady(storeIn);
393 slotAudioThumbOver();
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 << " ................................";
402 void KThumb::slotAudioThumbProgress(const int progress)
404 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
407 void KThumb::slotAudioThumbOver()
409 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
410 m_clipManager->endAudioThumbsGeneration(m_id);
413 void KThumb::askForAudioThumbs(const QString &id)
415 m_clipManager->askForAudioThumb(id);
419 #include "kthumb.moc"