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) {
45 f.setFileName(target);
48 m_frameLength = frameLength;
49 m_frequency = frequency;
50 m_channels = channels;
51 m_arrayWidth = arrayWidth;
54 bool MyThread::isWorking() {
58 void MyThread::run() {
59 if (!f.open(QIODevice::WriteOnly)) {
60 kDebug() << "++++++++ ERROR WRITING TO FILE: " << f.fileName() << endl;
61 kDebug() << "++++++++ DISABLING AUDIO THUMBS" << endl;
62 KdenliveSettings::setAudiothumbnails(false);
66 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
67 Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
70 if (KdenliveSettings::normaliseaudiothumbs()) {
71 Mlt::Filter m_convert(prof, "volume");
72 m_convert.set("gain", "normalise");
73 m_producer.attach(m_convert);
76 //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
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);
86 //QApplication::postEvent(m_parent, new ProgressEvent(val, (QEvent::Type)10005));
91 Mlt::Frame *mlt_frame = m_producer.get_frame();
92 if (mlt_frame && mlt_frame->is_valid()) {
93 double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer()); //mlt_frame->get_double( "fps" );
94 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
95 mlt_audio_format m_audioFormat = mlt_audio_pcm;
97 qint16* m_pcm = mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples);
99 for (int c = 0;c < m_channels;c++) {
101 m_array.resize(m_arrayWidth);
102 for (uint i = 0; i < m_array.size(); i++) {
103 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
109 f.write(QByteArray(m_arrayWidth, '\x00'));
120 emit audioThumbOver();
121 //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
125 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/)
127 audioThumbProducer(),
132 m_clipManager(clipManager),
136 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
137 connect(&audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
138 connect(&audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));
143 if (audioThumbProducer.isRunning()) {
144 slotAudioThumbOver();
145 audioThumbProducer.stop_me = true;
146 audioThumbProducer.wait(300);
150 void KThumb::setProducer(Mlt::Producer *producer) {
151 m_producer = producer;
152 m_dar = producer->profile()->dar();
155 void KThumb::clearProducer() {
159 bool KThumb::hasProducer() const {
160 return m_producer != NULL;
163 void KThumb::updateThumbUrl(const QString &hash) {
164 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
167 void KThumb::updateClipUrl(KUrl url, const QString &hash) {
170 char *tmp = Render::decodedString(url.path());
171 m_producer->set("resource", tmp);
174 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
178 QPixmap KThumb::getImage(KUrl url, int width, int height) {
179 if (url.isEmpty()) return QPixmap();
180 return getImage(url, 0, width, height);
183 void KThumb::extractImage(int frame, int frame2) {
184 if (m_url.isEmpty() || !KdenliveSettings::videothumbnails() || m_producer == NULL) return;
186 const int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
187 const int theight = KdenliveSettings::trackheight();
189 mlt_image_format format = mlt_image_yuv422;
190 if (m_producer->is_blank()) {
191 QPixmap pix(twidth, theight);
193 emit thumbReady(frame, pix);
196 Mlt::Frame *mltFrame;
198 //videoThumbProducer.getThumb(frame);
199 m_producer->seek(frame);
200 mltFrame = m_producer->get_frame();
201 if (frame2 != -1) m_producer->seek(frame2);
203 kDebug() << "///// BROKEN FRAME";
204 QPixmap p(twidth, theight);
206 emit thumbReady(frame, p);
210 int frame_height = 0;
211 mltFrame->set("normalised_height", theight);
212 mltFrame->set("normalised_width", twidth);
213 QPixmap pix(twidth, theight);
214 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
215 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
216 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
218 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
220 if (!image.isNull()) {
221 pix = QPixmap::fromImage(image.rgbSwapped());
225 mlt_pool_release(new_image);
227 emit thumbReady(frame, pix);
229 } else if (frame2 != -1) m_producer->seek(frame2);
231 mltFrame = m_producer->get_frame();
233 kDebug() << "///// BROKEN FRAME";
234 QPixmap p(twidth, theight);
236 emit thumbReady(frame, p);
240 int frame_height = 0;
241 mltFrame->set("normalised_height", theight);
242 mltFrame->set("normalised_width", twidth);
243 QPixmap pix(twidth, theight);
244 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
245 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
246 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
248 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
250 if (!image.isNull()) {
251 pix = QPixmap::fromImage(image.rgbSwapped());
255 mlt_pool_release(new_image);
257 emit thumbReady(frame2, pix);
262 QPixmap KThumb::extractImage(int frame, int width, int height) {
263 return getFrame(m_producer, frame, width, height);
267 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height) {
268 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
269 QPixmap pix(width, height);
270 if (url.isEmpty()) return pix;
272 char *tmp = Render::decodedString(url.path());
273 //"<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
274 //Mlt::Producer producer(profile, "westley-xml", tmp);
275 Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
278 if (producer->is_blank()) {
283 pix = getFrame(producer, frame, width, height);
290 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
291 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
292 QPixmap pix(width, height);
294 QDomElement westley = doc.createElement("westley");
295 QDomElement play = doc.createElement("playlist");
296 doc.appendChild(westley);
297 westley.appendChild(play);
298 play.appendChild(doc.importNode(xml, true));
299 char *tmp = Render::decodedString(doc.toString());
300 Mlt::Producer producer(profile, "westley-xml", tmp);
303 if (producer.is_blank()) {
307 return getFrame(producer, frame, width, height);
311 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height) {
312 if (producer == NULL) {
313 QPixmap p(width, height);
318 producer->seek(framepos);
319 Mlt::Frame *frame = producer->get_frame();
321 kDebug() << "///// BROKEN FRAME";
322 QPixmap p(width, height);
327 mlt_image_format format = mlt_image_yuv422;
329 int frame_height = 0;
330 frame->set("normalised_height", height);
331 frame->set("normalised_width", width);
332 QPixmap pix(width, height);
333 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
334 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
335 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
337 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
339 if (!image.isNull()) {
340 pix = QPixmap::fromImage(image.rgbSwapped());
344 mlt_pool_release(new_image);
349 void KThumb::getImage(KUrl url, int frame, int width, int height)
351 if (url.isEmpty()) return;
352 QPixmap image(width, height);
353 char *tmp = KRender::decodedString(url.path());
354 Mlt::Producer m_producer(tmp);
356 image.fill(Qt::black);
358 if (m_producer.is_blank()) {
359 emit thumbReady(frame, image);
362 Mlt::Filter m_convert("avcolour_space");
363 m_convert.set("forced", mlt_image_rgb24a);
364 m_producer.attach(m_convert);
365 m_producer.seek(frame);
366 Mlt::Frame * m_frame = m_producer.get_frame();
367 mlt_image_format format = mlt_image_rgb24a;
370 if (m_frame && m_frame->is_valid()) {
371 uint8_t *thumb = m_frame->get_image(format, width, height);
372 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
373 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
375 if (m_frame) delete m_frame;
376 emit thumbReady(frame, image);
379 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
381 if (url.isEmpty()) return;
382 QPixmap image(width, height);
383 char *tmp = KRender::decodedString(url.path());
384 Mlt::Producer m_producer(tmp);
386 image.fill(Qt::black);
388 if (m_producer.is_blank()) {
389 emit thumbReady(startframe, image);
390 emit thumbReady(endframe, image);
393 Mlt::Filter m_convert("avcolour_space");
394 m_convert.set("forced", mlt_image_rgb24a);
395 m_producer.attach(m_convert);
396 m_producer.seek(startframe);
397 Mlt::Frame * m_frame = m_producer.get_frame();
398 mlt_image_format format = mlt_image_rgb24a;
402 if (m_frame && m_frame->is_valid()) {
403 uint8_t *thumb = m_frame->get_image(format, width, height);
404 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
405 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
407 if (m_frame) delete m_frame;
408 emit thumbReady(startframe, image);
410 image.fill(Qt::black);
411 m_producer.seek(endframe);
412 m_frame = m_producer.get_frame();
414 if (m_frame && m_frame->is_valid()) {
415 uint8_t *thumb = m_frame->get_image(format, width, height);
416 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
417 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
419 if (m_frame) delete m_frame;
420 emit thumbReady(endframe, image);
423 void KThumb::stopAudioThumbs() {
424 if (audioThumbProducer.isRunning()) audioThumbProducer.stop_me = true;
427 void KThumb::removeAudioThumb() {
428 if (m_thumbFile.isEmpty()) return;
430 QFile f(m_thumbFile);
434 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth) {
436 slotAudioThumbOver();
439 if ((audioThumbProducer.isRunning() && audioThumbProducer.isWorking())) {
443 QMap <int, QMap <int, QByteArray> > storeIn;
444 //FIXME: Hardcoded!!!
445 int m_frequency = 48000;
446 int m_channels = channel;
448 QFile f(m_thumbFile);
449 if (f.open(QIODevice::ReadOnly)) {
450 QByteArray channelarray = f.readAll();
452 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
453 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
455 slotAudioThumbOver();
458 kDebug() << "reading audio thumbs from file";
459 for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
460 for (int c = 0;c < m_channels;c++) {
461 QByteArray m_array(arrayWidth, '\x00');
462 for (int i = 0; i < arrayWidth; i++)
463 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
464 storeIn[z][c] = m_array;
467 emit audioThumbReady(storeIn);
468 slotAudioThumbOver();
470 if (audioThumbProducer.isRunning()) return;
471 audioThumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
472 audioThumbProducer.start(QThread::LowestPriority);
473 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
477 void KThumb::slotAudioThumbProgress(const int progress) {
478 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
481 void KThumb::slotAudioThumbOver() {
482 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
483 m_clipManager->endAudioThumbsGeneration(m_id);
486 void KThumb::askForAudioThumbs(const QString &id) {
487 m_clipManager->askForAudioThumb(id);
491 #include "kthumb.moc"