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()); //mlt_frame->get_double( "fps" );
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;
95 qint16* m_pcm = 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();
119 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/) :
121 m_audioThumbProducer(),
126 m_clipManager(clipManager),
129 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
130 connect(&m_audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
131 connect(&m_audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));
137 if (m_audioThumbProducer.isRunning()) {
138 m_audioThumbProducer.stop_me = true;
139 m_audioThumbProducer.wait();
140 slotAudioThumbOver();
144 void KThumb::setProducer(Mlt::Producer *producer)
146 m_producer = producer;
147 // FIXME: the profile() call leaks an object, but trying to free
148 // it leads to a double-free in Profile::~Profile()
149 m_dar = producer->profile()->dar();
152 void KThumb::clearProducer()
157 bool KThumb::hasProducer() const
159 return m_producer != NULL;
162 void KThumb::updateThumbUrl(const QString &hash)
164 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
167 void KThumb::updateClipUrl(KUrl url, const QString &hash)
171 char *tmp = Render::decodedString(url.path());
172 m_producer->set("resource", tmp);
175 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
179 QPixmap KThumb::getImage(KUrl url, int width, int height)
181 if (url.isEmpty()) return QPixmap();
182 return getImage(url, 0, width, height);
185 void KThumb::extractImage(int frame, int frame2)
187 // kDebug() << "//extract thumb: " << frame << ", " << frame2;
188 if (m_url.isEmpty() || !KdenliveSettings::videothumbnails() || m_producer == NULL) return;
190 const int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
191 const int theight = KdenliveSettings::trackheight();
194 QPixmap pix = getFrame(m_producer, frame, twidth, theight);
195 emit thumbReady(frame, pix);
198 QPixmap pix = getFrame(m_producer, frame2, twidth, theight);
199 emit thumbReady(frame2, pix);
203 QPixmap KThumb::extractImage(int frame, int width, int height)
205 return getFrame(m_producer, frame, width, height);
209 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
211 char *tmp = Render::decodedString(KdenliveSettings::current_profile());
212 Mlt::Profile profile(tmp);
214 QPixmap pix(width, height);
215 if (url.isEmpty()) return pix;
217 tmp = Render::decodedString(url.path());
218 //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
219 //Mlt::Producer producer(profile, "xml-string", tmp);
220 Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
223 pix = getFrame(producer, frame, width, height);
230 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
232 QPixmap p(width, height);
233 if (producer == NULL) {
238 if (producer->is_blank()) {
243 producer->seek(framepos);
244 Mlt::Frame *frame = producer->get_frame();
246 kDebug() << "///// BROKEN FRAME";
251 mlt_image_format format = mlt_image_rgb24a;
252 int frame_width = width;
253 int frame_height = height;
254 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
255 QImage image((uchar *)data, frame_width, frame_height, QImage::Format_ARGB32);
257 if (!image.isNull()) {
258 p = QPixmap::fromImage(image.rgbSwapped());
266 void KThumb::getImage(KUrl url, int frame, int width, int height)
268 if (url.isEmpty()) return;
269 QPixmap image(width, height);
270 char *tmp = KRender::decodedString(url.path());
271 Mlt::Producer m_producer(tmp);
273 image.fill(Qt::black);
275 if (m_producer.is_blank()) {
276 emit thumbReady(frame, image);
279 Mlt::Filter m_convert("avcolour_space");
280 m_convert.set("forced", mlt_image_rgb24a);
281 m_producer.attach(m_convert);
282 m_producer.seek(frame);
283 Mlt::Frame * m_frame = m_producer.get_frame();
284 mlt_image_format format = mlt_image_rgb24a;
287 if (m_frame && m_frame->is_valid()) {
288 uint8_t *thumb = m_frame->get_image(format, width, height);
289 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
290 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
292 if (m_frame) delete m_frame;
293 emit thumbReady(frame, image);
296 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
298 if (url.isEmpty()) return;
299 QPixmap image(width, height);
300 char *tmp = KRender::decodedString(url.path());
301 Mlt::Producer m_producer(tmp);
303 image.fill(Qt::black);
305 if (m_producer.is_blank()) {
306 emit thumbReady(startframe, image);
307 emit thumbReady(endframe, image);
310 Mlt::Filter m_convert("avcolour_space");
311 m_convert.set("forced", mlt_image_rgb24a);
312 m_producer.attach(m_convert);
313 m_producer.seek(startframe);
314 Mlt::Frame * m_frame = m_producer.get_frame();
315 mlt_image_format format = mlt_image_rgb24a;
319 if (m_frame && m_frame->is_valid()) {
320 uint8_t *thumb = m_frame->get_image(format, width, height);
321 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
322 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
324 if (m_frame) delete m_frame;
325 emit thumbReady(startframe, image);
327 image.fill(Qt::black);
328 m_producer.seek(endframe);
329 m_frame = m_producer.get_frame();
331 if (m_frame && m_frame->is_valid()) {
332 uint8_t *thumb = m_frame->get_image(format, width, height);
333 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
334 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
336 if (m_frame) delete m_frame;
337 emit thumbReady(endframe, image);
340 void KThumb::stopAudioThumbs()
342 if (m_audioThumbProducer.isRunning()) {
343 m_audioThumbProducer.stop_me = true;
344 slotAudioThumbOver();
348 void KThumb::removeAudioThumb()
350 if (m_thumbFile.isEmpty()) return;
352 QFile f(m_thumbFile);
356 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
359 slotAudioThumbOver();
362 if ((m_audioThumbProducer.isRunning() && m_audioThumbProducer.isWorking())) {
366 QMap <int, QMap <int, QByteArray> > storeIn;
367 //FIXME: Hardcoded!!!
368 int m_frequency = 48000;
369 int m_channels = channel;
371 QFile f(m_thumbFile);
372 if (f.open(QIODevice::ReadOnly)) {
373 QByteArray channelarray = f.readAll();
375 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
376 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
378 slotAudioThumbOver();
381 kDebug() << "reading audio thumbs from file";
382 for (int z = (int) frame; z < (int)(frame + frameLength); z++) {
383 for (int c = 0; c < m_channels; c++) {
384 QByteArray m_array(arrayWidth, '\x00');
385 for (int i = 0; i < arrayWidth; i++)
386 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
387 storeIn[z][c] = m_array;
390 emit audioThumbReady(storeIn);
391 slotAudioThumbOver();
393 if (m_audioThumbProducer.isRunning()) return;
394 m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
395 m_audioThumbProducer.start(QThread::LowestPriority);
396 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
400 void KThumb::slotAudioThumbProgress(const int progress)
402 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
405 void KThumb::slotAudioThumbOver()
407 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
408 m_clipManager->endAudioThumbsGeneration(m_id);
411 void KThumb::askForAudioThumbs(const QString &id)
413 m_clipManager->askForAudioThumb(id);
417 #include "kthumb.moc"