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(QObject *parent, 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);
79 //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
83 kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
84 for (int z = (int) m_frame;z < (int)(m_frame + m_frameLength) && m_producer.is_valid();z++) {
86 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
87 if (last_val != val && val > 1) {
88 emit audioThumbProgress(val);
89 //QApplication::postEvent(m_parent, new ProgressEvent(val, (QEvent::Type)10005));
94 Mlt::Frame *mlt_frame = m_producer.get_frame();
95 if (mlt_frame && mlt_frame->is_valid()) {
96 double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer()); //mlt_frame->get_double( "fps" );
97 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
98 mlt_audio_format m_audioFormat = mlt_audio_pcm;
100 qint16* m_pcm = mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples);
102 for (int c = 0;c < m_channels;c++) {
104 m_array.resize(m_arrayWidth);
105 for (int i = 0; i < m_array.size(); i++) {
106 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
112 f.write(QByteArray(m_arrayWidth, '\x00'));
123 emit audioThumbOver();
124 //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
128 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/) :
130 audioThumbProducer(),
135 m_clipManager(clipManager),
139 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
140 connect(&audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
141 connect(&audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));
147 if (audioThumbProducer.isRunning()) {
148 slotAudioThumbOver();
149 audioThumbProducer.stop_me = true;
150 audioThumbProducer.wait(300);
154 void KThumb::setProducer(Mlt::Producer *producer)
156 m_producer = producer;
157 m_dar = producer->profile()->dar();
160 void KThumb::clearProducer()
165 bool KThumb::hasProducer() const
167 return m_producer != NULL;
170 void KThumb::updateThumbUrl(const QString &hash)
172 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
175 void KThumb::updateClipUrl(KUrl url, const QString &hash)
179 char *tmp = Render::decodedString(url.path());
180 m_producer->set("resource", tmp);
183 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
187 QPixmap KThumb::getImage(KUrl url, int width, int height)
189 if (url.isEmpty()) return QPixmap();
190 return getImage(url, 0, width, height);
193 void KThumb::extractImage(int frame, int frame2)
195 if (m_url.isEmpty() || !KdenliveSettings::videothumbnails() || m_producer == NULL) return;
197 const int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
198 const int theight = KdenliveSettings::trackheight();
200 mlt_image_format format = mlt_image_yuv422;
201 if (m_producer->is_blank()) {
202 QPixmap pix(twidth, theight);
204 emit thumbReady(frame, pix);
207 Mlt::Frame *mltFrame;
209 //videoThumbProducer.getThumb(frame);
210 m_producer->seek(frame);
211 mltFrame = m_producer->get_frame();
212 if (frame2 != -1) m_producer->seek(frame2);
214 kDebug() << "///// BROKEN FRAME";
215 QPixmap p(twidth, theight);
217 emit thumbReady(frame, p);
221 int frame_height = 0;
222 mltFrame->set("normalised_height", theight);
223 mltFrame->set("normalised_width", twidth);
224 QPixmap pix(twidth, theight);
225 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
226 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
227 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
229 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
231 if (!image.isNull()) {
232 pix = QPixmap::fromImage(image.rgbSwapped());
236 mlt_pool_release(new_image);
238 emit thumbReady(frame, pix);
240 } else if (frame2 != -1) m_producer->seek(frame2);
242 mltFrame = m_producer->get_frame();
244 kDebug() << "///// BROKEN FRAME";
245 QPixmap p(twidth, theight);
247 emit thumbReady(frame, p);
251 int frame_height = 0;
252 mltFrame->set("normalised_height", theight);
253 mltFrame->set("normalised_width", twidth);
254 QPixmap pix(twidth, theight);
255 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
256 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
257 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
259 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
261 if (!image.isNull()) {
262 pix = QPixmap::fromImage(image.rgbSwapped());
266 mlt_pool_release(new_image);
268 emit thumbReady(frame2, pix);
273 QPixmap KThumb::extractImage(int frame, int width, int height)
275 return getFrame(m_producer, frame, width, height);
279 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
281 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
282 QPixmap pix(width, height);
283 if (url.isEmpty()) return pix;
285 char *tmp = Render::decodedString(url.path());
286 //"<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
287 //Mlt::Producer producer(profile, "westley-xml", tmp);
288 Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
291 if (producer->is_blank()) {
296 pix = getFrame(producer, frame, width, height);
303 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
304 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
305 QPixmap pix(width, height);
307 QDomElement westley = doc.createElement("westley");
308 QDomElement play = doc.createElement("playlist");
309 doc.appendChild(westley);
310 westley.appendChild(play);
311 play.appendChild(doc.importNode(xml, true));
312 char *tmp = Render::decodedString(doc.toString());
313 Mlt::Producer producer(profile, "westley-xml", tmp);
316 if (producer.is_blank()) {
320 return getFrame(producer, frame, width, height);
324 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
326 if (producer == NULL) {
327 QPixmap p(width, height);
332 producer->seek(framepos);
333 Mlt::Frame *frame = producer->get_frame();
335 kDebug() << "///// BROKEN FRAME";
336 QPixmap p(width, height);
341 mlt_image_format format = mlt_image_yuv422;
343 int frame_height = 0;
344 frame->set("normalised_height", height);
345 frame->set("normalised_width", width);
346 QPixmap pix(width, height);
347 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
348 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
349 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
351 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
353 if (!image.isNull()) {
354 pix = QPixmap::fromImage(image.rgbSwapped());
358 mlt_pool_release(new_image);
363 void KThumb::getImage(KUrl url, int frame, int width, int height)
365 if (url.isEmpty()) return;
366 QPixmap image(width, height);
367 char *tmp = KRender::decodedString(url.path());
368 Mlt::Producer m_producer(tmp);
370 image.fill(Qt::black);
372 if (m_producer.is_blank()) {
373 emit thumbReady(frame, image);
376 Mlt::Filter m_convert("avcolour_space");
377 m_convert.set("forced", mlt_image_rgb24a);
378 m_producer.attach(m_convert);
379 m_producer.seek(frame);
380 Mlt::Frame * m_frame = m_producer.get_frame();
381 mlt_image_format format = mlt_image_rgb24a;
384 if (m_frame && m_frame->is_valid()) {
385 uint8_t *thumb = m_frame->get_image(format, width, height);
386 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
387 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
389 if (m_frame) delete m_frame;
390 emit thumbReady(frame, image);
393 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
395 if (url.isEmpty()) return;
396 QPixmap image(width, height);
397 char *tmp = KRender::decodedString(url.path());
398 Mlt::Producer m_producer(tmp);
400 image.fill(Qt::black);
402 if (m_producer.is_blank()) {
403 emit thumbReady(startframe, image);
404 emit thumbReady(endframe, image);
407 Mlt::Filter m_convert("avcolour_space");
408 m_convert.set("forced", mlt_image_rgb24a);
409 m_producer.attach(m_convert);
410 m_producer.seek(startframe);
411 Mlt::Frame * m_frame = m_producer.get_frame();
412 mlt_image_format format = mlt_image_rgb24a;
416 if (m_frame && m_frame->is_valid()) {
417 uint8_t *thumb = m_frame->get_image(format, width, height);
418 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
419 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
421 if (m_frame) delete m_frame;
422 emit thumbReady(startframe, image);
424 image.fill(Qt::black);
425 m_producer.seek(endframe);
426 m_frame = m_producer.get_frame();
428 if (m_frame && m_frame->is_valid()) {
429 uint8_t *thumb = m_frame->get_image(format, width, height);
430 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
431 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
433 if (m_frame) delete m_frame;
434 emit thumbReady(endframe, image);
437 void KThumb::stopAudioThumbs()
439 if (audioThumbProducer.isRunning()) audioThumbProducer.stop_me = true;
442 void KThumb::removeAudioThumb()
444 if (m_thumbFile.isEmpty()) return;
446 QFile f(m_thumbFile);
450 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
453 slotAudioThumbOver();
456 if ((audioThumbProducer.isRunning() && audioThumbProducer.isWorking())) {
460 QMap <int, QMap <int, QByteArray> > storeIn;
461 //FIXME: Hardcoded!!!
462 int m_frequency = 48000;
463 int m_channels = channel;
465 QFile f(m_thumbFile);
466 if (f.open(QIODevice::ReadOnly)) {
467 QByteArray channelarray = f.readAll();
469 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
470 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
472 slotAudioThumbOver();
475 kDebug() << "reading audio thumbs from file";
476 for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
477 for (int c = 0;c < m_channels;c++) {
478 QByteArray m_array(arrayWidth, '\x00');
479 for (int i = 0; i < arrayWidth; i++)
480 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
481 storeIn[z][c] = m_array;
484 emit audioThumbReady(storeIn);
485 slotAudioThumbOver();
487 if (audioThumbProducer.isRunning()) return;
488 audioThumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
489 audioThumbProducer.start(QThread::LowestPriority);
490 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
494 void KThumb::slotAudioThumbProgress(const int progress)
496 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
499 void KThumb::slotAudioThumbOver()
501 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
502 m_clipManager->endAudioThumbsGeneration(m_id);
505 void KThumb::askForAudioThumbs(const QString &id)
507 m_clipManager->askForAudioThumb(id);
511 #include "kthumb.moc"