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"
44 void MyThread::init(QObject *parent, KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth) {
48 f.setFileName(target);
51 m_frameLength = frameLength;
52 m_frequency = frequency;
53 m_channels = channels;
54 m_arrayWidth = arrayWidth;
57 bool MyThread::isWorking() {
61 void MyThread::run() {
63 if (!f.open(QIODevice::WriteOnly)) {
64 kDebug() << "++++++++ ERROR WRITING TO FILE: " << f.fileName() << endl;
65 kDebug() << "++++++++ DISABLING AUDIO THUMBS" << endl;
66 KdenliveSettings::setAudiothumbnails(false);
70 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
71 Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
74 if (KdenliveSettings::normaliseaudiothumbs()) {
75 Mlt::Filter m_convert(prof, "volume");
76 m_convert.set("gain", "normalise");
77 m_producer.attach(m_convert);
80 //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
84 kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
85 for (int z = (int) m_frame;z < (int)(m_frame + m_frameLength) && m_producer.is_valid();z++) {
87 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
88 if (last_val != val & val > 1) {
89 emit audioThumbProgress(val);
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 emit audioThumbOver();
125 //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
129 KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, QObject * parent, const char *name): QObject(parent), m_clipManager(clipManager), m_url(url), m_id(id), m_producer(NULL), m_dar(1), m_mainFrame(-1) {
130 QCryptographicHash context(QCryptographicHash::Sha1);
131 context.addData((KFileItem(m_url, "text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
132 m_thumbFile = KGlobal::dirs()->saveLocation("tmp" , "kdenlive") + context.result().toHex() + ".thumb";
133 //kDebug() << "thumbfile=" << m_thumbFile;
134 connect(&audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
135 connect(&audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));
140 if (audioThumbProducer.isRunning()) audioThumbProducer.exit();
143 void KThumb::setProducer(Mlt::Producer *producer) {
144 m_producer = producer;
145 m_dar = producer->profile()->dar();
148 void KThumb::updateClipUrl(KUrl url) {
151 char *tmp = Render::decodedString(url.path());
152 m_producer->set("resource", tmp);
158 QPixmap KThumb::getImage(KUrl url, int width, int height) {
159 if (url.isEmpty()) return QPixmap();
160 return getImage(url, 0, width, height);
163 void KThumb::extractImage(int frame, int frame2) {
164 if (m_url.isEmpty() || !KdenliveSettings::videothumbnails() || m_producer == NULL) return;
166 const int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
167 const int theight = KdenliveSettings::trackheight();
168 mlt_image_format format = mlt_image_yuv422;
169 if (m_producer->is_blank()) {
170 QPixmap pix(twidth, theight);
172 emit thumbReady(frame, pix);
175 Mlt::Frame *mltFrame;
177 //videoThumbProducer.getThumb(frame);
178 m_producer->seek(frame);
179 mltFrame = m_producer->get_frame();
181 kDebug() << "///// BROKEN FRAME";
182 QPixmap p(twidth, theight);
184 emit thumbReady(frame, p);
187 if (frame2 != -1) m_producer->seek(frame2);
189 int frame_height = 0;
190 mltFrame->set("normalised_height", theight);
191 mltFrame->set("normalised_width", twidth);
192 QPixmap pix(twidth, theight);
193 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
194 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
195 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
197 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
199 if (!image.isNull()) {
200 pix = QPixmap::fromImage(image.rgbSwapped());
204 mlt_pool_release(new_image);
206 emit thumbReady(frame, pix);
208 } else if (frame2 != -1) m_producer->seek(frame2);
210 mltFrame = m_producer->get_frame();
212 kDebug() << "///// BROKEN FRAME";
213 QPixmap p(twidth, theight);
215 emit thumbReady(frame, p);
219 int frame_height = 0;
220 mltFrame->set("normalised_height", theight);
221 mltFrame->set("normalised_width", twidth);
222 QPixmap pix(twidth, theight);
223 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
224 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
225 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
227 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
229 if (!image.isNull()) {
230 pix = QPixmap::fromImage(image.rgbSwapped());
234 mlt_pool_release(new_image);
236 emit thumbReady(frame2, pix);
241 QPixmap KThumb::extractImage(int frame, int width, int height) {
242 return getFrame(m_producer, frame, width, height);
246 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height) {
247 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
248 QPixmap pix(width, height);
249 if (url.isEmpty()) return pix;
251 char *tmp = Render::decodedString(url.path());
252 //"<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
253 //Mlt::Producer producer(profile, "westley-xml", tmp);
254 Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
257 if (producer->is_blank()) {
262 pix = getFrame(producer, frame, width, height);
269 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
270 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
271 QPixmap pix(width, height);
273 QDomElement westley = doc.createElement("westley");
274 QDomElement play = doc.createElement("playlist");
275 doc.appendChild(westley);
276 westley.appendChild(play);
277 play.appendChild(doc.importNode(xml, true));
278 char *tmp = Render::decodedString(doc.toString());
279 Mlt::Producer producer(profile, "westley-xml", tmp);
282 if (producer.is_blank()) {
286 return getFrame(producer, frame, width, height);
290 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height) {
291 if (producer == NULL) {
292 QPixmap p(width, height);
297 producer->seek(framepos);
298 Mlt::Frame *frame = producer->get_frame();
300 kDebug() << "///// BROKEN FRAME";
301 QPixmap p(width, height);
306 mlt_image_format format = mlt_image_yuv422;
308 int frame_height = 0;
309 frame->set("normalised_height", height);
310 frame->set("normalised_width", width);
311 QPixmap pix(width, height);
312 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
313 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
314 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
316 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
318 if (!image.isNull()) {
319 pix = QPixmap::fromImage(image.rgbSwapped());
323 mlt_pool_release(new_image);
328 void KThumb::getImage(KUrl url, int frame, int width, int height)
330 if (url.isEmpty()) return;
331 QPixmap image(width, height);
332 char *tmp = KRender::decodedString(url.path());
333 Mlt::Producer m_producer(tmp);
335 image.fill(Qt::black);
337 if (m_producer.is_blank()) {
338 emit thumbReady(frame, image);
341 Mlt::Filter m_convert("avcolour_space");
342 m_convert.set("forced", mlt_image_rgb24a);
343 m_producer.attach(m_convert);
344 m_producer.seek(frame);
345 Mlt::Frame * m_frame = m_producer.get_frame();
346 mlt_image_format format = mlt_image_rgb24a;
349 if (m_frame && m_frame->is_valid()) {
350 uint8_t *thumb = m_frame->get_image(format, width, height);
351 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
352 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
354 if (m_frame) delete m_frame;
355 emit thumbReady(frame, image);
358 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
360 if (url.isEmpty()) return;
361 QPixmap image(width, height);
362 char *tmp = KRender::decodedString(url.path());
363 Mlt::Producer m_producer(tmp);
365 image.fill(Qt::black);
367 if (m_producer.is_blank()) {
368 emit thumbReady(startframe, image);
369 emit thumbReady(endframe, 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(startframe);
376 Mlt::Frame * m_frame = m_producer.get_frame();
377 mlt_image_format format = mlt_image_rgb24a;
381 if (m_frame && m_frame->is_valid()) {
382 uint8_t *thumb = m_frame->get_image(format, width, height);
383 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
384 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
386 if (m_frame) delete m_frame;
387 emit thumbReady(startframe, image);
389 image.fill(Qt::black);
390 m_producer.seek(endframe);
391 m_frame = m_producer.get_frame();
393 if (m_frame && m_frame->is_valid()) {
394 uint8_t *thumb = m_frame->get_image(format, width, height);
395 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
396 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
398 if (m_frame) delete m_frame;
399 emit thumbReady(endframe, image);
402 void KThumb::stopAudioThumbs() {
403 if (audioThumbProducer.isRunning()) audioThumbProducer.stop_me = true;
406 void KThumb::removeAudioThumb() {
407 if (m_thumbFile.isEmpty()) return;
409 QFile f(m_thumbFile);
413 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth) {
415 slotAudioThumbOver();
418 if ((audioThumbProducer.isRunning() && audioThumbProducer.isWorking())) {
422 QMap <int, QMap <int, QByteArray> > storeIn;
423 //FIXME: Hardcoded!!!
424 int m_frequency = 48000;
425 int m_channels = channel;
427 QFile f(m_thumbFile);
428 if (f.open(QIODevice::ReadOnly)) {
429 QByteArray channelarray = f.readAll();
431 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
432 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
434 slotAudioThumbOver();
437 kDebug() << "reading audio thumbs from file";
438 for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
439 for (int c = 0;c < m_channels;c++) {
440 QByteArray m_array(arrayWidth, '\x00');
441 for (int i = 0; i < arrayWidth; i++)
442 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
443 storeIn[z][c] = m_array;
446 emit audioThumbReady(storeIn);
447 slotAudioThumbOver();
449 if (audioThumbProducer.isRunning()) return;
450 audioThumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
451 audioThumbProducer.start(QThread::LowestPriority);
452 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
456 void KThumb::slotAudioThumbProgress(const int progress) {
457 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
460 void KThumb::slotAudioThumbOver() {
461 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
462 m_clipManager->endAudioThumbsGeneration(m_id);
465 void KThumb::askForAudioThumbs(const QString &id) {
466 m_clipManager->askForAudioThumb(id);
470 #include "kthumb.moc"