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): QObject(parent), m_clipManager(clipManager), m_url(url), m_id(id), m_producer(NULL), m_dar(1), m_mainFrame(-1) {
126 m_thumbFile = clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
127 connect(&audioThumbProducer, SIGNAL(audioThumbProgress(const int)), this, SLOT(slotAudioThumbProgress(const int)));
128 connect(&audioThumbProducer, SIGNAL(audioThumbOver()), this, SLOT(slotAudioThumbOver()));
133 if (audioThumbProducer.isRunning()) {
134 slotAudioThumbOver();
135 audioThumbProducer.stop_me = true;
136 audioThumbProducer.wait(300);
140 void KThumb::setProducer(Mlt::Producer *producer) {
141 m_producer = producer;
142 m_dar = producer->profile()->dar();
145 void KThumb::clearProducer() {
149 bool KThumb::hasProducer() const {
150 return m_producer != NULL;
153 void KThumb::updateThumbUrl(const QString &hash) {
154 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
157 void KThumb::updateClipUrl(KUrl url, const QString &hash) {
160 char *tmp = Render::decodedString(url.path());
161 m_producer->set("resource", tmp);
164 m_thumbFile = m_clipManager->projectFolder() + "/thumbs/" + hash + ".thumb";
168 QPixmap KThumb::getImage(KUrl url, int width, int height) {
169 if (url.isEmpty()) return QPixmap();
170 return getImage(url, 0, width, height);
173 void KThumb::extractImage(int frame, int frame2) {
174 if (m_url.isEmpty() || !KdenliveSettings::videothumbnails() || m_producer == NULL) return;
176 const int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
177 const int theight = KdenliveSettings::trackheight();
179 mlt_image_format format = mlt_image_yuv422;
180 if (m_producer->is_blank()) {
181 QPixmap pix(twidth, theight);
183 emit thumbReady(frame, pix);
186 Mlt::Frame *mltFrame;
188 //videoThumbProducer.getThumb(frame);
189 m_producer->seek(frame);
190 mltFrame = m_producer->get_frame();
191 if (frame2 != -1) m_producer->seek(frame2);
193 kDebug() << "///// BROKEN FRAME";
194 QPixmap p(twidth, theight);
196 emit thumbReady(frame, p);
200 int frame_height = 0;
201 mltFrame->set("normalised_height", theight);
202 mltFrame->set("normalised_width", twidth);
203 QPixmap pix(twidth, theight);
204 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
205 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
206 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
208 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
210 if (!image.isNull()) {
211 pix = QPixmap::fromImage(image.rgbSwapped());
215 mlt_pool_release(new_image);
217 emit thumbReady(frame, pix);
219 } else if (frame2 != -1) m_producer->seek(frame2);
221 mltFrame = m_producer->get_frame();
223 kDebug() << "///// BROKEN FRAME";
224 QPixmap p(twidth, theight);
226 emit thumbReady(frame, p);
230 int frame_height = 0;
231 mltFrame->set("normalised_height", theight);
232 mltFrame->set("normalised_width", twidth);
233 QPixmap pix(twidth, theight);
234 uint8_t *data = mltFrame->get_image(format, frame_width, frame_height, 0);
235 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
236 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
238 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
240 if (!image.isNull()) {
241 pix = QPixmap::fromImage(image.rgbSwapped());
245 mlt_pool_release(new_image);
247 emit thumbReady(frame2, pix);
252 QPixmap KThumb::extractImage(int frame, int width, int height) {
253 return getFrame(m_producer, frame, width, height);
257 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height) {
258 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
259 QPixmap pix(width, height);
260 if (url.isEmpty()) return pix;
262 char *tmp = Render::decodedString(url.path());
263 //"<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
264 //Mlt::Producer producer(profile, "westley-xml", tmp);
265 Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
268 if (producer->is_blank()) {
273 pix = getFrame(producer, frame, width, height);
280 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
281 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
282 QPixmap pix(width, height);
284 QDomElement westley = doc.createElement("westley");
285 QDomElement play = doc.createElement("playlist");
286 doc.appendChild(westley);
287 westley.appendChild(play);
288 play.appendChild(doc.importNode(xml, true));
289 char *tmp = Render::decodedString(doc.toString());
290 Mlt::Producer producer(profile, "westley-xml", tmp);
293 if (producer.is_blank()) {
297 return getFrame(producer, frame, width, height);
301 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height) {
302 if (producer == NULL) {
303 QPixmap p(width, height);
308 producer->seek(framepos);
309 Mlt::Frame *frame = producer->get_frame();
311 kDebug() << "///// BROKEN FRAME";
312 QPixmap p(width, height);
317 mlt_image_format format = mlt_image_yuv422;
319 int frame_height = 0;
320 frame->set("normalised_height", height);
321 frame->set("normalised_width", width);
322 QPixmap pix(width, height);
323 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
324 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
325 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
327 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
329 if (!image.isNull()) {
330 pix = QPixmap::fromImage(image.rgbSwapped());
334 mlt_pool_release(new_image);
339 void KThumb::getImage(KUrl url, int frame, int width, int height)
341 if (url.isEmpty()) return;
342 QPixmap image(width, height);
343 char *tmp = KRender::decodedString(url.path());
344 Mlt::Producer m_producer(tmp);
346 image.fill(Qt::black);
348 if (m_producer.is_blank()) {
349 emit thumbReady(frame, image);
352 Mlt::Filter m_convert("avcolour_space");
353 m_convert.set("forced", mlt_image_rgb24a);
354 m_producer.attach(m_convert);
355 m_producer.seek(frame);
356 Mlt::Frame * m_frame = m_producer.get_frame();
357 mlt_image_format format = mlt_image_rgb24a;
360 if (m_frame && m_frame->is_valid()) {
361 uint8_t *thumb = m_frame->get_image(format, width, height);
362 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
363 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
365 if (m_frame) delete m_frame;
366 emit thumbReady(frame, image);
369 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
371 if (url.isEmpty()) return;
372 QPixmap image(width, height);
373 char *tmp = KRender::decodedString(url.path());
374 Mlt::Producer m_producer(tmp);
376 image.fill(Qt::black);
378 if (m_producer.is_blank()) {
379 emit thumbReady(startframe, image);
380 emit thumbReady(endframe, image);
383 Mlt::Filter m_convert("avcolour_space");
384 m_convert.set("forced", mlt_image_rgb24a);
385 m_producer.attach(m_convert);
386 m_producer.seek(startframe);
387 Mlt::Frame * m_frame = m_producer.get_frame();
388 mlt_image_format format = mlt_image_rgb24a;
392 if (m_frame && m_frame->is_valid()) {
393 uint8_t *thumb = m_frame->get_image(format, width, height);
394 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
395 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
397 if (m_frame) delete m_frame;
398 emit thumbReady(startframe, image);
400 image.fill(Qt::black);
401 m_producer.seek(endframe);
402 m_frame = m_producer.get_frame();
404 if (m_frame && m_frame->is_valid()) {
405 uint8_t *thumb = m_frame->get_image(format, width, height);
406 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
407 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
409 if (m_frame) delete m_frame;
410 emit thumbReady(endframe, image);
413 void KThumb::stopAudioThumbs() {
414 if (audioThumbProducer.isRunning()) audioThumbProducer.stop_me = true;
417 void KThumb::removeAudioThumb() {
418 if (m_thumbFile.isEmpty()) return;
420 QFile f(m_thumbFile);
424 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth) {
426 slotAudioThumbOver();
429 if ((audioThumbProducer.isRunning() && audioThumbProducer.isWorking())) {
433 QMap <int, QMap <int, QByteArray> > storeIn;
434 //FIXME: Hardcoded!!!
435 int m_frequency = 48000;
436 int m_channels = channel;
438 QFile f(m_thumbFile);
439 if (f.open(QIODevice::ReadOnly)) {
440 QByteArray channelarray = f.readAll();
442 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
443 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
445 slotAudioThumbOver();
448 kDebug() << "reading audio thumbs from file";
449 for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
450 for (int c = 0;c < m_channels;c++) {
451 QByteArray m_array(arrayWidth, '\x00');
452 for (int i = 0; i < arrayWidth; i++)
453 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
454 storeIn[z][c] = m_array;
457 emit audioThumbReady(storeIn);
458 slotAudioThumbOver();
460 if (audioThumbProducer.isRunning()) return;
461 audioThumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
462 audioThumbProducer.start(QThread::LowestPriority);
463 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
467 void KThumb::slotAudioThumbProgress(const int progress) {
468 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), progress);
471 void KThumb::slotAudioThumbOver() {
472 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), -1);
473 m_clipManager->endAudioThumbsGeneration(m_id);
476 void KThumb::askForAudioThumbs(const QString &id) {
477 m_clipManager->askForAudioThumb(id);
481 #include "kthumb.moc"