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 ***************************************************************************/
23 #include <qapplication.h>
25 #include <kio/netaccess.h>
28 #include <kdenlivesettings.h>
29 #include <kfileitem.h>
30 #include <kmessagebox.h>
32 #include <mlt++/Mlt.h>
38 #include <QApplication>
39 #include <QCryptographicHash>
41 #include "clipmanager.h"
44 #include "kdenlivesettings.h"
47 void MyThread::init(QObject *parent, KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth) {
51 f.setFileName(target);
54 m_frameLength = frameLength;
55 m_frequency = frequency;
56 m_channels = channels;
57 m_arrayWidth = arrayWidth;
60 bool MyThread::isWorking() {
64 void MyThread::run() {
66 if (!f.open(QIODevice::WriteOnly)) {
67 kDebug() << "++++++++ ERROR WRITING TO FILE: " << f.fileName() << endl;
68 kDebug() << "++++++++ DISABLING AUDIO THUMBS" << endl;
69 //TODO KdenliveSettings::setAudiothumbnails(false);
73 Mlt::Profile prof((char*) qstrdup(KdenliveSettings::current_profile().toUtf8()));
74 Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
77 /*TODO if (KdenliveSettings::normaliseaudiothumbs()) {
78 Mlt::Filter m_convert(prof,"volume");
79 m_convert.set("gain", "normalise");
80 m_producer.attach(m_convert);
83 //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
87 kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
88 for (int z = (int) m_frame;z < (int)(m_frame + m_frameLength) && m_producer.is_valid();z++) {
90 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
91 if (last_val != val & val > 1) {
92 QApplication::postEvent(m_parent, new ProgressEvent(val, (QEvent::Type)10005));
97 Mlt::Frame *mlt_frame = m_producer.get_frame();
98 if (mlt_frame && mlt_frame->is_valid()) {
99 double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer()); //mlt_frame->get_double( "fps" );
100 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
101 mlt_audio_format m_audioFormat = mlt_audio_pcm;
103 int16_t* m_pcm = mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples);
105 for (int c = 0;c < m_channels;c++) {
107 m_array.resize(m_arrayWidth);
108 for (uint i = 0; i < m_array.size(); i++) {
109 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
115 f.write(QByteArray(m_arrayWidth, '\x00'));
125 QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
128 QApplication::postEvent(m_parent, new ProgressEvent(0, (QEvent::Type)10005));
133 #define _S(a) (a)>255 ? 255 : (a)<0 ? 0 : (a)
134 #define _R(y,u,v) (0x2568*(y) + 0x3343*(u)) /0x2000
135 #define _G(y,u,v) (0x2568*(y) - 0x0c92*(v) - 0x1a1e*(u)) /0x2000
136 #define _B(y,u,v) (0x2568*(y) + 0x40cf*(v)) /0x2000
138 KThumb::KThumb(ClipManager *clipManager, KUrl url, int width, int height, QObject * parent, const char *name): QObject(parent), m_clipManager(clipManager), m_url(url), m_width(width), m_height(height) {
140 m_profile = new Mlt::Profile((char*) KdenliveSettings::current_profile().data());
141 QCryptographicHash context(QCryptographicHash::Sha1);
142 context.addData((KFileItem(m_url, "text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
143 m_thumbFile = KdenliveSettings::currenttmpfolder() + context.result().toHex() + ".thumb";
144 kDebug() << "thumbfile=" << m_thumbFile;
148 if (m_profile) delete m_profile;
149 if (thumbProducer.isRunning()) thumbProducer.exit();
154 QPixmap KThumb::getImage(KUrl url, int width, int height) {
155 if (url.isEmpty()) return QPixmap();
156 QPixmap pix(width, height);
157 kDebug() << "+++++++++++ GET THMB IMG FOR: " << url;
158 char *tmp = Render::decodedString(url.path());
159 Mlt::Profile prof((char*) KdenliveSettings::current_profile().data());
160 Mlt::Producer m_producer(prof, tmp);
163 if (m_producer.is_blank()) {
167 Mlt::Frame * m_frame;
168 mlt_image_format format = mlt_image_rgb24a;
169 Mlt::Filter m_convert(prof, "avcolour_space");
170 m_convert.set("forced", mlt_image_rgb24a);
171 m_producer.attach(m_convert);
172 //m_producer.seek(frame);
173 m_frame = m_producer.get_frame();
174 if (m_frame && m_frame->is_valid()) {
175 uint8_t *thumb = m_frame->get_image(format, width, height);
176 QImage image(thumb, width, height, QImage::Format_ARGB32);
177 if (!image.isNull()) {
178 pix = pix.fromImage(image);
179 } else pix.fill(Qt::black);
181 if (m_frame) delete m_frame;
185 void KThumb::extractImage(int frame, int frame2) {
186 if (m_url.isEmpty()) return;
187 QPixmap pix(m_width, m_height);
188 char *tmp = Render::decodedString(m_url.path());
189 Mlt::Producer m_producer(*m_profile, tmp);
192 if (m_producer.is_blank()) {
194 emit thumbReady(frame, pix);
197 Mlt::Frame * m_frame;
198 mlt_image_format format = mlt_image_rgb24a;
199 Mlt::Filter m_convert(*m_profile, "avcolour_space");
200 m_convert.set("forced", mlt_image_rgb24a);
201 m_producer.attach(m_convert);
203 m_producer.seek(frame);
204 m_frame = m_producer.get_frame();
205 if (m_frame && m_frame->is_valid()) {
206 uint8_t *thumb = m_frame->get_image(format, m_width, m_height);
207 QImage image(thumb, m_width, m_height, QImage::Format_ARGB32);
208 if (!image.isNull()) {
209 pix = pix.fromImage(image);
210 } else pix.fill(Qt::black);
212 if (m_frame) delete m_frame;
213 emit thumbReady(frame, pix);
215 if (frame2 == -1) return;
216 m_producer.seek(frame2);
217 m_frame = m_producer.get_frame();
218 if (m_frame && m_frame->is_valid()) {
219 uint8_t *thumb = m_frame->get_image(format, m_width, m_height);
220 QImage image(thumb, m_width, m_height, QImage::Format_ARGB32);
221 if (!image.isNull()) {
222 pix = pix.fromImage(image);
223 } else pix.fill(Qt::black);
225 if (m_frame) delete m_frame;
226 emit thumbReady(frame2, pix);
230 void KThumb::getImage(KUrl url, int frame, int width, int height)
232 if (url.isEmpty()) return;
233 QPixmap image(width, height);
234 char *tmp = KRender::decodedString(url.path());
235 Mlt::Producer m_producer(tmp);
237 image.fill(Qt::black);
239 if (m_producer.is_blank()) {
240 emit thumbReady(frame, image);
243 Mlt::Filter m_convert("avcolour_space");
244 m_convert.set("forced", mlt_image_rgb24a);
245 m_producer.attach(m_convert);
246 m_producer.seek(frame);
247 Mlt::Frame * m_frame = m_producer.get_frame();
248 mlt_image_format format = mlt_image_rgb24a;
251 if (m_frame && m_frame->is_valid()) {
252 uint8_t *thumb = m_frame->get_image(format, width, height);
253 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
254 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
256 if (m_frame) delete m_frame;
257 emit thumbReady(frame, image);
260 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
262 if (url.isEmpty()) return;
263 QPixmap image(width, height);
264 char *tmp = KRender::decodedString(url.path());
265 Mlt::Producer m_producer(tmp);
267 image.fill(Qt::black);
269 if (m_producer.is_blank()) {
270 emit thumbReady(startframe, image);
271 emit thumbReady(endframe, image);
274 Mlt::Filter m_convert("avcolour_space");
275 m_convert.set("forced", mlt_image_rgb24a);
276 m_producer.attach(m_convert);
277 m_producer.seek(startframe);
278 Mlt::Frame * m_frame = m_producer.get_frame();
279 mlt_image_format format = mlt_image_rgb24a;
283 if (m_frame && m_frame->is_valid()) {
284 uint8_t *thumb = m_frame->get_image(format, width, height);
285 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
286 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
288 if (m_frame) delete m_frame;
289 emit thumbReady(startframe, image);
291 image.fill(Qt::black);
292 m_producer.seek(endframe);
293 m_frame = m_producer.get_frame();
295 if (m_frame && m_frame->is_valid()) {
296 uint8_t *thumb = m_frame->get_image(format, width, height);
297 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
298 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
300 if (m_frame) delete m_frame;
301 emit thumbReady(endframe, image);
304 void KThumb::stopAudioThumbs() {
305 if (thumbProducer.isRunning()) thumbProducer.stop_me = true;
309 void KThumb::removeAudioThumb() {
310 if (m_thumbFile.isEmpty()) return;
312 QFile f(m_thumbFile);
316 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth) {
318 if ((thumbProducer.isRunning() && thumbProducer.isWorking()) || channel == 0) {
322 QMap <int, QMap <int, QByteArray> > storeIn;
323 //FIXME: Hardcoded!!!
324 int m_frequency = 48000;
325 int m_channels = channel;
327 QFile f(m_thumbFile);
328 if (f.open(QIODevice::ReadOnly)) {
329 QByteArray channelarray = f.readAll();
331 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
332 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
336 kDebug() << "reading audio thumbs from file";
337 for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
338 for (int c = 0;c < m_channels;c++) {
339 QByteArray m_array(arrayWidth, '\x00');
340 for (int i = 0; i < arrayWidth; i++)
341 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
342 storeIn[z][c] = m_array;
345 emit audioThumbReady(storeIn);
347 if (thumbProducer.isRunning()) return;
348 thumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
349 thumbProducer.start(QThread::LowestPriority);
350 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
354 void KThumb::customEvent(QEvent * event) {
355 if (event->type() == 10005) {
356 ProgressEvent* p = (ProgressEvent*) event;
357 m_clipManager->setThumbsProgress(m_url, p->value());
363 #include "kthumb.moc"