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 audioThumbProducer(),
126 m_clipManager(clipManager),
130 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
131 connect(&audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
132 connect(&audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));
138 if (audioThumbProducer.isRunning()) {
139 audioThumbProducer.stop_me = true;
140 audioThumbProducer.wait();
141 slotAudioThumbOver();
145 void KThumb::setProducer(Mlt::Producer *producer)
147 m_producer = producer;
148 // FIXME: the profile() call leaks an object, but trying to free
149 // it leads to a double-free in Profile::~Profile()
150 m_dar = producer->profile()->dar();
153 void KThumb::clearProducer()
158 bool KThumb::hasProducer() const
160 return m_producer != NULL;
163 void KThumb::updateThumbUrl(const QString &hash)
165 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
168 void KThumb::updateClipUrl(KUrl url, const QString &hash)
172 char *tmp = Render::decodedString(url.path());
173 m_producer->set("resource", tmp);
176 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
180 QPixmap KThumb::getImage(KUrl url, int width, int height)
182 if (url.isEmpty()) return QPixmap();
183 return getImage(url, 0, width, height);
186 void KThumb::extractImage(int frame, int 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();
193 mlt_image_format format = mlt_image_yuv422;
194 if (m_producer->is_blank()) {
195 QPixmap pix(twidth, theight);
197 emit thumbReady(frame, pix);
200 Mlt::Frame *mltFrame;
202 //videoThumbProducer.getThumb(frame);
203 m_producer->seek(frame);
204 mltFrame = m_producer->get_frame();
205 if (frame2 != -1) m_producer->seek(frame2);
207 kDebug() << "///// BROKEN FRAME";
208 QPixmap p(twidth, theight);
210 emit thumbReady(frame, p);
214 int frame_height = 0;
215 mltFrame->set("normalised_height", theight);
216 mltFrame->set("normalised_width", twidth);
217 QPixmap pix(twidth, theight);
218 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
219 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
220 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
222 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
224 if (!image.isNull()) {
225 pix = QPixmap::fromImage(image.rgbSwapped());
229 mlt_pool_release(new_image);
231 emit thumbReady(frame, pix);
233 } else if (frame2 != -1) m_producer->seek(frame2);
235 mltFrame = m_producer->get_frame();
237 kDebug() << "///// BROKEN FRAME";
238 QPixmap p(twidth, theight);
240 emit thumbReady(frame, p);
244 int frame_height = 0;
245 mltFrame->set("normalised_height", theight);
246 mltFrame->set("normalised_width", twidth);
247 QPixmap pix(twidth, theight);
248 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
249 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
250 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
252 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
254 if (!image.isNull()) {
255 pix = QPixmap::fromImage(image.rgbSwapped());
259 mlt_pool_release(new_image);
261 emit thumbReady(frame2, pix);
266 QPixmap KThumb::extractImage(int frame, int width, int height)
268 return getFrame(m_producer, frame, width, height);
272 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
274 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
275 QPixmap pix(width, height);
276 if (url.isEmpty()) return pix;
278 char *tmp = Render::decodedString(url.path());
279 //"<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
280 //Mlt::Producer producer(profile, "westley-xml", tmp);
281 Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
284 if (producer->is_blank()) {
289 pix = getFrame(producer, frame, width, height);
296 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
297 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
298 QPixmap pix(width, height);
300 QDomElement westley = doc.createElement("westley");
301 QDomElement play = doc.createElement("playlist");
302 doc.appendChild(westley);
303 westley.appendChild(play);
304 play.appendChild(doc.importNode(xml, true));
305 char *tmp = Render::decodedString(doc.toString());
306 Mlt::Producer producer(profile, "westley-xml", tmp);
309 if (producer.is_blank()) {
313 return getFrame(producer, frame, width, height);
317 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
319 if (producer == NULL) {
320 QPixmap p(width, height);
325 producer->seek(framepos);
326 Mlt::Frame *frame = producer->get_frame();
328 kDebug() << "///// BROKEN FRAME";
329 QPixmap p(width, height);
334 mlt_image_format format = mlt_image_yuv422;
336 int frame_height = 0;
337 frame->set("normalised_height", height);
338 frame->set("normalised_width", width);
339 QPixmap pix(width, height);
340 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
341 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
342 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
344 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
346 if (!image.isNull()) {
347 pix = QPixmap::fromImage(image.rgbSwapped());
351 mlt_pool_release(new_image);
356 void KThumb::getImage(KUrl url, int frame, int width, int height)
358 if (url.isEmpty()) return;
359 QPixmap image(width, height);
360 char *tmp = KRender::decodedString(url.path());
361 Mlt::Producer m_producer(tmp);
363 image.fill(Qt::black);
365 if (m_producer.is_blank()) {
366 emit thumbReady(frame, image);
369 Mlt::Filter m_convert("avcolour_space");
370 m_convert.set("forced", mlt_image_rgb24a);
371 m_producer.attach(m_convert);
372 m_producer.seek(frame);
373 Mlt::Frame * m_frame = m_producer.get_frame();
374 mlt_image_format format = mlt_image_rgb24a;
377 if (m_frame && m_frame->is_valid()) {
378 uint8_t *thumb = m_frame->get_image(format, width, height);
379 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
380 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
382 if (m_frame) delete m_frame;
383 emit thumbReady(frame, image);
386 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
388 if (url.isEmpty()) return;
389 QPixmap image(width, height);
390 char *tmp = KRender::decodedString(url.path());
391 Mlt::Producer m_producer(tmp);
393 image.fill(Qt::black);
395 if (m_producer.is_blank()) {
396 emit thumbReady(startframe, image);
397 emit thumbReady(endframe, image);
400 Mlt::Filter m_convert("avcolour_space");
401 m_convert.set("forced", mlt_image_rgb24a);
402 m_producer.attach(m_convert);
403 m_producer.seek(startframe);
404 Mlt::Frame * m_frame = m_producer.get_frame();
405 mlt_image_format format = mlt_image_rgb24a;
409 if (m_frame && m_frame->is_valid()) {
410 uint8_t *thumb = m_frame->get_image(format, width, height);
411 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
412 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
414 if (m_frame) delete m_frame;
415 emit thumbReady(startframe, image);
417 image.fill(Qt::black);
418 m_producer.seek(endframe);
419 m_frame = m_producer.get_frame();
421 if (m_frame && m_frame->is_valid()) {
422 uint8_t *thumb = m_frame->get_image(format, width, height);
423 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
424 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
426 if (m_frame) delete m_frame;
427 emit thumbReady(endframe, image);
430 void KThumb::stopAudioThumbs()
432 if (audioThumbProducer.isRunning()) {
433 audioThumbProducer.stop_me = true;
434 slotAudioThumbOver();
438 void KThumb::removeAudioThumb()
440 if (m_thumbFile.isEmpty()) return;
442 QFile f(m_thumbFile);
446 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
449 slotAudioThumbOver();
452 if ((audioThumbProducer.isRunning() && audioThumbProducer.isWorking())) {
456 QMap <int, QMap <int, QByteArray> > storeIn;
457 //FIXME: Hardcoded!!!
458 int m_frequency = 48000;
459 int m_channels = channel;
461 QFile f(m_thumbFile);
462 if (f.open(QIODevice::ReadOnly)) {
463 QByteArray channelarray = f.readAll();
465 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
466 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
468 slotAudioThumbOver();
471 kDebug() << "reading audio thumbs from file";
472 for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
473 for (int c = 0;c < m_channels;c++) {
474 QByteArray m_array(arrayWidth, '\x00');
475 for (int i = 0; i < arrayWidth; i++)
476 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
477 storeIn[z][c] = m_array;
480 emit audioThumbReady(storeIn);
481 slotAudioThumbOver();
483 if (audioThumbProducer.isRunning()) return;
484 audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
485 audioThumbProducer.start(QThread::LowestPriority);
486 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
490 void KThumb::slotAudioThumbProgress(const int progress)
492 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
495 void KThumb::slotAudioThumbOver()
497 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
498 m_clipManager->endAudioThumbsGeneration(m_id);
501 void KThumb::askForAudioThumbs(const QString &id)
503 m_clipManager->askForAudioThumb(id);
507 #include "kthumb.moc"