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 ***************************************************************************/
25 #include <QApplication>
26 #include <QCryptographicHash>
28 #include <kio/netaccess.h>
31 #include <kdenlivesettings.h>
32 #include <kfileitem.h>
33 #include <kmessagebox.h>
34 #include <KStandardDirs>
36 #include <mlt++/Mlt.h>
38 #include "clipmanager.h"
41 #include "kdenlivesettings.h"
45 void MyThread::init(QObject *parent, KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth) {
49 f.setFileName(target);
52 m_frameLength = frameLength;
53 m_frequency = frequency;
54 m_channels = channels;
55 m_arrayWidth = arrayWidth;
58 bool MyThread::isWorking() {
62 void MyThread::run() {
64 if (!f.open(QIODevice::WriteOnly)) {
65 kDebug() << "++++++++ ERROR WRITING TO FILE: " << f.fileName() << endl;
66 kDebug() << "++++++++ DISABLING AUDIO THUMBS" << endl;
67 KdenliveSettings::setAudiothumbnails(false);
71 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
72 Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
75 if (KdenliveSettings::normaliseaudiothumbs()) {
76 Mlt::Filter m_convert(prof, "volume");
77 m_convert.set("gain", "normalise");
78 m_producer.attach(m_convert);
81 //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
85 kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
86 for (int z = (int) m_frame;z < (int)(m_frame + m_frameLength) && m_producer.is_valid();z++) {
88 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
89 if (last_val != val & val > 1) {
90 QApplication::postEvent(m_parent, new ProgressEvent(val, (QEvent::Type)10005));
95 Mlt::Frame *mlt_frame = m_producer.get_frame();
96 if (mlt_frame && mlt_frame->is_valid()) {
97 double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer()); //mlt_frame->get_double( "fps" );
98 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
99 mlt_audio_format m_audioFormat = mlt_audio_pcm;
101 int16_t* m_pcm = mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples);
103 for (int c = 0;c < m_channels;c++) {
105 m_array.resize(m_arrayWidth);
106 for (uint i = 0; i < m_array.size(); i++) {
107 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
113 f.write(QByteArray(m_arrayWidth, '\x00'));
124 QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
128 void ThumbThread::init(QObject *parent, Mlt::Producer *prod, int width, int height) {
139 bool ThumbThread::isWorking() {
143 void ThumbThread::setThumbFrames(Mlt::Producer *prod, int frame1, int frame2) {
144 if (!m_prod) m_prod = prod;
149 void ThumbThread::run() {
150 if (m_frame1 != -1 && m_prod) {
152 m_prod->seek(m_frame1);
153 Mlt::Frame *avframe = m_prod->get_frame();
156 kDebug() << "///// BROKEN FRAME";
158 mlt_image_format format = mlt_image_yuv422;
159 int frame_width = m_width;
160 int frame_height = m_height;
161 avframe->set("normalised_height", m_height);
162 avframe->set("normalised_width", m_width);
163 uint8_t *data = avframe->get_image(format, frame_width, frame_height, 0);
164 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
165 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
167 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
169 if (!image.isNull()) {
170 emit gotStartThumb(image.rgbSwapped());
171 //QApplication::postEvent(m_parent, new ThumbEvent(m_frame1, image.rgbSwapped(), (QEvent::Type)10006));
172 //pix = QPixmap::fromImage(image.rgbSwapped());
175 mlt_pool_release(new_image);
181 if (m_frame2 != -1 && m_prod) {
183 m_prod->seek(m_frame2);
184 Mlt::Frame *avframe = m_prod->get_frame();
187 kDebug() << "///// BROKEN FRAME";
189 mlt_image_format format = mlt_image_yuv422;
191 int frame_height = 0;
192 avframe->set("normalised_height", m_height);
193 avframe->set("normalised_width", m_width);
194 uint8_t *data = avframe->get_image(format, frame_width, frame_height, 0);
195 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
196 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
198 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
200 if (!image.isNull()) {
201 emit gotEndThumb(image.rgbSwapped());
202 //QApplication::postEvent(m_parent, new ThumbEvent(m_frame2, image.rgbSwapped(), (QEvent::Type)10006));
203 //pix = QPixmap::fromImage(image.rgbSwapped());
206 mlt_pool_release(new_image);
210 //QApplication::postEvent(m_parent, new ThumbEvent(m_frame2, pix, (QEvent::Type)10006));
215 KThumb::KThumb(ClipManager *clipManager, KUrl url, QObject * parent, const char *name): QObject(parent), m_clipManager(clipManager), m_url(url), m_producer(NULL), m_dar(1) {
216 QCryptographicHash context(QCryptographicHash::Sha1);
217 context.addData((KFileItem(m_url, "text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
218 m_thumbFile = KGlobal::dirs()->saveLocation("tmp" , "kdenlive") + context.result().toHex() + ".thumb";
219 kDebug() << "thumbfile=" << m_thumbFile;
223 if (audioThumbProducer.isRunning()) audioThumbProducer.exit();
226 void KThumb::setProducer(Mlt::Producer *producer) {
227 m_producer = producer;
228 m_dar = producer->profile()->dar();
231 void KThumb::updateClipUrl(KUrl url) {
234 char *tmp = Render::decodedString(url.path());
235 m_producer->set("resource", tmp);
241 QPixmap KThumb::getImage(KUrl url, int width, int height) {
242 if (url.isEmpty()) return QPixmap();
243 return getImage(url, 0, width, height);
246 void KThumb::extractImage(int frame, int frame2) {
247 if (m_url.isEmpty() || !KdenliveSettings::videothumbnails() || m_producer == NULL) return;
249 int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
250 if (m_producer->is_blank()) {
251 QPixmap pix(twidth, KdenliveSettings::trackheight());
253 emit thumbReady(frame, pix);
257 //videoThumbProducer.getThumb(frame);
258 QPixmap pix = getFrame(m_producer, frame, twidth, KdenliveSettings::trackheight());
259 emit thumbReady(frame, pix);
262 //videoThumbProducer.getThumb(frame2);
263 QPixmap pix = getFrame(m_producer, frame2, twidth , KdenliveSettings::trackheight());
264 emit thumbReady(frame2, pix);
268 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) {
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) {
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() {
430 if (audioThumbProducer.isRunning()) audioThumbProducer.stop_me = true;
434 void KThumb::removeAudioThumb() {
435 if (m_thumbFile.isEmpty()) return;
437 QFile f(m_thumbFile);
441 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth) {
443 if ((audioThumbProducer.isRunning() && audioThumbProducer.isWorking()) || channel == 0) {
447 QMap <int, QMap <int, QByteArray> > storeIn;
448 //FIXME: Hardcoded!!!
449 int m_frequency = 48000;
450 int m_channels = channel;
452 QFile f(m_thumbFile);
453 if (f.open(QIODevice::ReadOnly)) {
454 QByteArray channelarray = f.readAll();
456 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
457 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
461 kDebug() << "reading audio thumbs from file";
462 for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
463 for (int c = 0;c < m_channels;c++) {
464 QByteArray m_array(arrayWidth, '\x00');
465 for (int i = 0; i < arrayWidth; i++)
466 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
467 storeIn[z][c] = m_array;
470 emit audioThumbReady(storeIn);
472 if (audioThumbProducer.isRunning()) return;
473 audioThumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
474 audioThumbProducer.start(QThread::LowestPriority);
475 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
479 void KThumb::customEvent(QEvent * event) {
480 if (event->type() == 10005) {
481 ProgressEvent* p = static_cast <ProgressEvent*>(event);
482 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), p->value());
488 #include "kthumb.moc"