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();
193 if (m_producer->is_blank()) {
194 QPixmap pix(twidth, theight);
196 if (frame != -1) emit thumbReady(frame, pix);
197 if (frame2 != -1) emit thumbReady(frame2, pix);
200 Mlt::Frame *mltFrame;
201 mlt_image_format format = mlt_image_yuv422;
203 //videoThumbProducer.getThumb(frame);
204 m_producer->seek(frame);
205 mltFrame = m_producer->get_frame();
206 if (frame2 != -1) m_producer->seek(frame2);
208 kDebug() << "///// BROKEN FRAME";
209 QPixmap p(twidth, theight);
211 emit thumbReady(frame, p);
215 int frame_height = 0;
216 mltFrame->set("normalised_height", theight);
217 mltFrame->set("normalised_width", twidth);
218 QPixmap pix(twidth, theight);
219 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
220 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
221 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
223 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
225 if (!image.isNull()) {
226 pix = QPixmap::fromImage(image.rgbSwapped());
230 mlt_pool_release(new_image);
232 emit thumbReady(frame, pix);
234 } else if (frame2 != -1) m_producer->seek(frame2);
236 mltFrame = m_producer->get_frame();
238 kDebug() << "///// BROKEN FRAME";
239 QPixmap p(twidth, theight);
241 emit thumbReady(frame2, p);
245 int frame_height = 0;
246 mltFrame->set("normalised_height", theight);
247 mltFrame->set("normalised_width", twidth);
248 QPixmap pix(twidth, theight);
249 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
250 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
251 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
253 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
255 if (!image.isNull()) {
256 pix = QPixmap::fromImage(image.rgbSwapped());
260 mlt_pool_release(new_image);
262 emit thumbReady(frame2, pix);
267 QPixmap KThumb::extractImage(int frame, int width, int height)
269 return getFrame(m_producer, frame, width, height);
273 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
275 char *tmp = Render::decodedString(KdenliveSettings::current_profile());
276 Mlt::Profile profile(tmp);
278 QPixmap pix(width, height);
279 if (url.isEmpty()) return pix;
281 tmp = Render::decodedString(url.path());
282 //"<mlt><playlist><producer resource=\"" + url.path() + "\" /></playlist></mlt>");
283 //Mlt::Producer producer(profile, "xml-string", tmp);
284 Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
287 if (producer->is_blank()) {
292 pix = getFrame(producer, frame, width, height);
299 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
300 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
301 QPixmap pix(width, height);
303 QDomElement mlt = doc.createElement("mlt");
304 QDomElement play = doc.createElement("playlist");
305 doc.appendChild(mlt);
306 mlt.appendChild(play);
307 play.appendChild(doc.importNode(xml, true));
308 char *tmp = Render::decodedString(doc.toString());
309 Mlt::Producer producer(profile, "xml-string", tmp);
312 if (producer.is_blank()) {
316 return getFrame(producer, frame, width, height);
320 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
322 if (producer == NULL) {
323 QPixmap p(width, height);
328 producer->seek(framepos);
329 Mlt::Frame *frame = producer->get_frame();
331 kDebug() << "///// BROKEN FRAME";
332 QPixmap p(width, height);
337 mlt_image_format format = mlt_image_yuv422;
339 int frame_height = 0;
340 frame->set("normalised_height", height);
341 frame->set("normalised_width", width);
342 QPixmap pix(width, height);
343 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
344 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
345 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
347 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
349 if (!image.isNull()) {
350 pix = QPixmap::fromImage(image.rgbSwapped());
354 mlt_pool_release(new_image);
359 void KThumb::getImage(KUrl url, int frame, int width, int height)
361 if (url.isEmpty()) return;
362 QPixmap image(width, height);
363 char *tmp = KRender::decodedString(url.path());
364 Mlt::Producer m_producer(tmp);
366 image.fill(Qt::black);
368 if (m_producer.is_blank()) {
369 emit thumbReady(frame, image);
372 Mlt::Filter m_convert("avcolour_space");
373 m_convert.set("forced", mlt_image_rgb24a);
374 m_producer.attach(m_convert);
375 m_producer.seek(frame);
376 Mlt::Frame * m_frame = m_producer.get_frame();
377 mlt_image_format format = mlt_image_rgb24a;
380 if (m_frame && m_frame->is_valid()) {
381 uint8_t *thumb = m_frame->get_image(format, width, height);
382 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
383 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
385 if (m_frame) delete m_frame;
386 emit thumbReady(frame, image);
389 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
391 if (url.isEmpty()) return;
392 QPixmap image(width, height);
393 char *tmp = KRender::decodedString(url.path());
394 Mlt::Producer m_producer(tmp);
396 image.fill(Qt::black);
398 if (m_producer.is_blank()) {
399 emit thumbReady(startframe, image);
400 emit thumbReady(endframe, image);
403 Mlt::Filter m_convert("avcolour_space");
404 m_convert.set("forced", mlt_image_rgb24a);
405 m_producer.attach(m_convert);
406 m_producer.seek(startframe);
407 Mlt::Frame * m_frame = m_producer.get_frame();
408 mlt_image_format format = mlt_image_rgb24a;
412 if (m_frame && m_frame->is_valid()) {
413 uint8_t *thumb = m_frame->get_image(format, width, height);
414 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
415 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
417 if (m_frame) delete m_frame;
418 emit thumbReady(startframe, image);
420 image.fill(Qt::black);
421 m_producer.seek(endframe);
422 m_frame = m_producer.get_frame();
424 if (m_frame && m_frame->is_valid()) {
425 uint8_t *thumb = m_frame->get_image(format, width, height);
426 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
427 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
429 if (m_frame) delete m_frame;
430 emit thumbReady(endframe, image);
433 void KThumb::stopAudioThumbs()
435 if (m_audioThumbProducer.isRunning()) {
436 m_audioThumbProducer.stop_me = true;
437 slotAudioThumbOver();
441 void KThumb::removeAudioThumb()
443 if (m_thumbFile.isEmpty()) return;
445 QFile f(m_thumbFile);
449 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
452 slotAudioThumbOver();
455 if ((m_audioThumbProducer.isRunning() && m_audioThumbProducer.isWorking())) {
459 QMap <int, QMap <int, QByteArray> > storeIn;
460 //FIXME: Hardcoded!!!
461 int m_frequency = 48000;
462 int m_channels = channel;
464 QFile f(m_thumbFile);
465 if (f.open(QIODevice::ReadOnly)) {
466 QByteArray channelarray = f.readAll();
468 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
469 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
471 slotAudioThumbOver();
474 kDebug() << "reading audio thumbs from file";
475 for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
476 for (int c = 0;c < m_channels;c++) {
477 QByteArray m_array(arrayWidth, '\x00');
478 for (int i = 0; i < arrayWidth; i++)
479 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
480 storeIn[z][c] = m_array;
483 emit audioThumbReady(storeIn);
484 slotAudioThumbOver();
486 if (m_audioThumbProducer.isRunning()) return;
487 m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
488 m_audioThumbProducer.start(QThread::LowestPriority);
489 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
493 void KThumb::slotAudioThumbProgress(const int progress)
495 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
498 void KThumb::slotAudioThumbOver()
500 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
501 m_clipManager->endAudioThumbsGeneration(m_id);
504 void KThumb::askForAudioThumbs(const QString &id)
506 m_clipManager->askForAudioThumb(id);
510 #include "kthumb.moc"