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 if (m_url.isEmpty() || !KdenliveSettings::videothumbnails() || m_producer == NULL) return;
189 const int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
190 const int theight = KdenliveSettings::trackheight();
192 mlt_image_format format = mlt_image_yuv422;
193 if (m_producer->is_blank()) {
194 QPixmap pix(twidth, theight);
196 emit thumbReady(frame, pix);
199 Mlt::Frame *mltFrame;
201 //videoThumbProducer.getThumb(frame);
202 m_producer->seek(frame);
203 mltFrame = m_producer->get_frame();
204 if (frame2 != -1) m_producer->seek(frame2);
206 kDebug() << "///// BROKEN FRAME";
207 QPixmap p(twidth, theight);
209 emit thumbReady(frame, p);
213 int frame_height = 0;
214 mltFrame->set("normalised_height", theight);
215 mltFrame->set("normalised_width", twidth);
216 QPixmap pix(twidth, theight);
217 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
218 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
219 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
221 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
223 if (!image.isNull()) {
224 pix = QPixmap::fromImage(image.rgbSwapped());
228 mlt_pool_release(new_image);
230 emit thumbReady(frame, pix);
232 } else if (frame2 != -1) m_producer->seek(frame2);
234 mltFrame = m_producer->get_frame();
236 kDebug() << "///// BROKEN FRAME";
237 QPixmap p(twidth, theight);
239 emit thumbReady(frame, p);
243 int frame_height = 0;
244 mltFrame->set("normalised_height", theight);
245 mltFrame->set("normalised_width", twidth);
246 QPixmap pix(twidth, theight);
247 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
248 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
249 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
251 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
253 if (!image.isNull()) {
254 pix = QPixmap::fromImage(image.rgbSwapped());
258 mlt_pool_release(new_image);
260 emit thumbReady(frame2, pix);
265 QPixmap KThumb::extractImage(int frame, int width, int height)
267 return getFrame(m_producer, frame, width, height);
271 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
273 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
274 QPixmap pix(width, height);
275 if (url.isEmpty()) return pix;
277 char *tmp = Render::decodedString(url.path());
278 //"<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
279 //Mlt::Producer producer(profile, "westley-xml", tmp);
280 Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
283 if (producer->is_blank()) {
288 pix = getFrame(producer, frame, width, height);
295 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
296 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
297 QPixmap pix(width, height);
299 QDomElement westley = doc.createElement("westley");
300 QDomElement play = doc.createElement("playlist");
301 doc.appendChild(westley);
302 westley.appendChild(play);
303 play.appendChild(doc.importNode(xml, true));
304 char *tmp = Render::decodedString(doc.toString());
305 Mlt::Producer producer(profile, "westley-xml", tmp);
308 if (producer.is_blank()) {
312 return getFrame(producer, frame, width, height);
316 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height)
318 if (producer == NULL) {
319 QPixmap p(width, height);
324 producer->seek(framepos);
325 Mlt::Frame *frame = producer->get_frame();
327 kDebug() << "///// BROKEN FRAME";
328 QPixmap p(width, height);
333 mlt_image_format format = mlt_image_yuv422;
335 int frame_height = 0;
336 frame->set("normalised_height", height);
337 frame->set("normalised_width", width);
338 QPixmap pix(width, height);
339 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
340 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
341 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
343 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
345 if (!image.isNull()) {
346 pix = QPixmap::fromImage(image.rgbSwapped());
350 mlt_pool_release(new_image);
355 void KThumb::getImage(KUrl url, int frame, int width, int height)
357 if (url.isEmpty()) return;
358 QPixmap image(width, height);
359 char *tmp = KRender::decodedString(url.path());
360 Mlt::Producer m_producer(tmp);
362 image.fill(Qt::black);
364 if (m_producer.is_blank()) {
365 emit thumbReady(frame, image);
368 Mlt::Filter m_convert("avcolour_space");
369 m_convert.set("forced", mlt_image_rgb24a);
370 m_producer.attach(m_convert);
371 m_producer.seek(frame);
372 Mlt::Frame * m_frame = m_producer.get_frame();
373 mlt_image_format format = mlt_image_rgb24a;
376 if (m_frame && m_frame->is_valid()) {
377 uint8_t *thumb = m_frame->get_image(format, width, height);
378 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
379 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
381 if (m_frame) delete m_frame;
382 emit thumbReady(frame, image);
385 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
387 if (url.isEmpty()) return;
388 QPixmap image(width, height);
389 char *tmp = KRender::decodedString(url.path());
390 Mlt::Producer m_producer(tmp);
392 image.fill(Qt::black);
394 if (m_producer.is_blank()) {
395 emit thumbReady(startframe, image);
396 emit thumbReady(endframe, image);
399 Mlt::Filter m_convert("avcolour_space");
400 m_convert.set("forced", mlt_image_rgb24a);
401 m_producer.attach(m_convert);
402 m_producer.seek(startframe);
403 Mlt::Frame * m_frame = m_producer.get_frame();
404 mlt_image_format format = mlt_image_rgb24a;
408 if (m_frame && m_frame->is_valid()) {
409 uint8_t *thumb = m_frame->get_image(format, width, height);
410 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
411 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
413 if (m_frame) delete m_frame;
414 emit thumbReady(startframe, image);
416 image.fill(Qt::black);
417 m_producer.seek(endframe);
418 m_frame = m_producer.get_frame();
420 if (m_frame && m_frame->is_valid()) {
421 uint8_t *thumb = m_frame->get_image(format, width, height);
422 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
423 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
425 if (m_frame) delete m_frame;
426 emit thumbReady(endframe, image);
429 void KThumb::stopAudioThumbs()
431 if (m_audioThumbProducer.isRunning()) {
432 m_audioThumbProducer.stop_me = true;
433 slotAudioThumbOver();
437 void KThumb::removeAudioThumb()
439 if (m_thumbFile.isEmpty()) return;
441 QFile f(m_thumbFile);
445 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth)
448 slotAudioThumbOver();
451 if ((m_audioThumbProducer.isRunning() && m_audioThumbProducer.isWorking())) {
455 QMap <int, QMap <int, QByteArray> > storeIn;
456 //FIXME: Hardcoded!!!
457 int m_frequency = 48000;
458 int m_channels = channel;
460 QFile f(m_thumbFile);
461 if (f.open(QIODevice::ReadOnly)) {
462 QByteArray channelarray = f.readAll();
464 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
465 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
467 slotAudioThumbOver();
470 kDebug() << "reading audio thumbs from file";
471 for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
472 for (int c = 0;c < m_channels;c++) {
473 QByteArray m_array(arrayWidth, '\x00');
474 for (int i = 0; i < arrayWidth; i++)
475 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
476 storeIn[z][c] = m_array;
479 emit audioThumbReady(storeIn);
480 slotAudioThumbOver();
482 if (m_audioThumbProducer.isRunning()) return;
483 m_audioThumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
484 m_audioThumbProducer.start(QThread::LowestPriority);
485 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
489 void KThumb::slotAudioThumbProgress(const int progress)
491 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
494 void KThumb::slotAudioThumbOver()
496 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
497 m_clipManager->endAudioThumbsGeneration(m_id);
500 void KThumb::askForAudioThumbs(const QString &id)
502 m_clipManager->askForAudioThumb(id);
506 #include "kthumb.moc"