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>
31 #include <KStandardDirs>
33 #include <mlt++/Mlt.h>
39 #include <QApplication>
40 #include <QCryptographicHash>
42 #include "clipmanager.h"
45 #include "kdenlivesettings.h"
48 void MyThread::init(QObject *parent, KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth) {
52 f.setFileName(target);
55 m_frameLength = frameLength;
56 m_frequency = frequency;
57 m_channels = channels;
58 m_arrayWidth = arrayWidth;
61 bool MyThread::isWorking() {
65 void MyThread::run() {
67 if (!f.open(QIODevice::WriteOnly)) {
68 kDebug() << "++++++++ ERROR WRITING TO FILE: " << f.fileName() << endl;
69 kDebug() << "++++++++ DISABLING AUDIO THUMBS" << endl;
70 KdenliveSettings::setAudiothumbnails(false);
74 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
75 Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
78 if (KdenliveSettings::normaliseaudiothumbs()) {
79 Mlt::Filter m_convert(prof, "volume");
80 m_convert.set("gain", "normalise");
81 m_producer.attach(m_convert);
84 //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
88 kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
89 for (int z = (int) m_frame;z < (int)(m_frame + m_frameLength) && m_producer.is_valid();z++) {
91 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
92 if (last_val != val & val > 1) {
93 QApplication::postEvent(m_parent, new ProgressEvent(val, (QEvent::Type)10005));
98 Mlt::Frame *mlt_frame = m_producer.get_frame();
99 if (mlt_frame && mlt_frame->is_valid()) {
100 double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer()); //mlt_frame->get_double( "fps" );
101 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
102 mlt_audio_format m_audioFormat = mlt_audio_pcm;
104 int16_t* m_pcm = mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples);
106 for (int c = 0;c < m_channels;c++) {
108 m_array.resize(m_arrayWidth);
109 for (uint i = 0; i < m_array.size(); i++) {
110 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
116 f.write(QByteArray(m_arrayWidth, '\x00'));
126 QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
129 QApplication::postEvent(m_parent, new ProgressEvent(0, (QEvent::Type)10005));
134 #define _S(a) (a)>255 ? 255 : (a)<0 ? 0 : (a)
135 #define _R(y,u,v) (0x2568*(y) + 0x3343*(u)) /0x2000
136 #define _G(y,u,v) (0x2568*(y) - 0x0c92*(v) - 0x1a1e*(u)) /0x2000
137 #define _B(y,u,v) (0x2568*(y) + 0x40cf*(v)) /0x2000
139 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) {
141 m_profile = new Mlt::Profile((char*) KdenliveSettings::current_profile().data());
142 QCryptographicHash context(QCryptographicHash::Sha1);
143 context.addData((KFileItem(m_url, "text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
144 m_thumbFile = KGlobal::dirs()->saveLocation("tmp" , "kdenlive") + context.result().toHex() + ".thumb";
145 kDebug() << "thumbfile=" << m_thumbFile;
149 if (m_profile) delete m_profile;
150 if (thumbProducer.isRunning()) thumbProducer.exit();
155 QPixmap KThumb::getImage(KUrl url, int width, int height) {
156 if (url.isEmpty()) return QPixmap();
157 /*QPixmap pix(width, height);
158 kDebug() << "+++++++++++ GET THMB IMG FOR: " << url;
159 char *tmp = Render::decodedString(url.path());
160 Mlt::Profile prof((char*) KdenliveSettings::current_profile().data());
161 Mlt::Producer m_producer(prof, tmp);
164 if (m_producer.is_blank()) {
168 Mlt::Frame * m_frame;
169 mlt_image_format format = mlt_image_rgb24a;
170 Mlt::Filter m_convert(prof, "avcolour_space");
171 m_convert.set("forced", mlt_image_rgb24a);
172 m_producer.attach(m_convert);
173 //m_producer.seek(frame);
174 m_frame = m_producer.get_frame();
175 if (m_frame && m_frame->is_valid()) {
176 uint8_t *thumb = m_frame->get_image(format, width, height);
177 QImage image(thumb, width, height, QImage::Format_ARGB32);
178 if (!image.isNull()) {
179 pix = pix.fromImage(image);
180 } else pix.fill(Qt::black);
182 if (m_frame) delete m_frame;
184 return getImage(url, 0, width, height);
187 void KThumb::extractImage(int frame, int frame2) {
188 if (m_url.isEmpty()) return;
190 char *tmp = Render::decodedString(m_url.path());
191 Mlt::Producer m_producer(*m_profile, tmp);
194 QPixmap pix(m_width, m_height);
195 if (m_producer.is_blank()) {
196 QPixmap pix(m_width, m_height);
198 emit thumbReady(frame, pix);
202 QPixmap pix = getFrame(&m_producer, frame, m_width, m_height);
203 emit thumbReady(frame, pix);
206 QPixmap pix = getFrame(&m_producer, frame2, m_width, m_height);
207 emit thumbReady(frame2, pix);
212 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height) {
213 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
214 QPixmap pix(width, height);
215 if (url.isEmpty()) return pix;
216 char *tmp = Render::decodedString(url.path());
217 Mlt::Producer producer(profile, tmp);
220 if (producer.is_blank()) {
225 return getFrame(&producer, frame, width, height);
229 QPixmap KThumb::getFrame(Mlt::Producer* producer, int frame, int width, int height) {
230 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
231 Mlt::Filter m_convert(profile, "avcolour_space");
232 m_convert.set("forced", mlt_image_rgb24a);
233 producer->attach(m_convert);
235 producer->seek(frame);
236 Mlt::Frame * m_frame = producer->get_frame();
237 mlt_image_format format = mlt_image_rgb24a;
238 QPixmap pix(width, height);
239 if (m_frame && m_frame->is_valid()) {
240 uint8_t *thumb = m_frame->get_image(format, width, height);
241 QImage image(thumb, width, height, QImage::Format_ARGB32);
242 if (!image.isNull()) {
243 pix = pix.fromImage(image);
244 } else pix.fill(Qt::black);
246 if (m_frame) delete m_frame;
251 void KThumb::getImage(KUrl url, int frame, int width, int height)
253 if (url.isEmpty()) return;
254 QPixmap image(width, height);
255 char *tmp = KRender::decodedString(url.path());
256 Mlt::Producer m_producer(tmp);
258 image.fill(Qt::black);
260 if (m_producer.is_blank()) {
261 emit thumbReady(frame, image);
264 Mlt::Filter m_convert("avcolour_space");
265 m_convert.set("forced", mlt_image_rgb24a);
266 m_producer.attach(m_convert);
267 m_producer.seek(frame);
268 Mlt::Frame * m_frame = m_producer.get_frame();
269 mlt_image_format format = mlt_image_rgb24a;
272 if (m_frame && m_frame->is_valid()) {
273 uint8_t *thumb = m_frame->get_image(format, width, height);
274 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
275 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
277 if (m_frame) delete m_frame;
278 emit thumbReady(frame, image);
281 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
283 if (url.isEmpty()) return;
284 QPixmap image(width, height);
285 char *tmp = KRender::decodedString(url.path());
286 Mlt::Producer m_producer(tmp);
288 image.fill(Qt::black);
290 if (m_producer.is_blank()) {
291 emit thumbReady(startframe, image);
292 emit thumbReady(endframe, image);
295 Mlt::Filter m_convert("avcolour_space");
296 m_convert.set("forced", mlt_image_rgb24a);
297 m_producer.attach(m_convert);
298 m_producer.seek(startframe);
299 Mlt::Frame * m_frame = m_producer.get_frame();
300 mlt_image_format format = mlt_image_rgb24a;
304 if (m_frame && m_frame->is_valid()) {
305 uint8_t *thumb = m_frame->get_image(format, width, height);
306 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
307 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
309 if (m_frame) delete m_frame;
310 emit thumbReady(startframe, image);
312 image.fill(Qt::black);
313 m_producer.seek(endframe);
314 m_frame = m_producer.get_frame();
316 if (m_frame && m_frame->is_valid()) {
317 uint8_t *thumb = m_frame->get_image(format, width, height);
318 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
319 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
321 if (m_frame) delete m_frame;
322 emit thumbReady(endframe, image);
325 void KThumb::stopAudioThumbs() {
326 if (thumbProducer.isRunning()) thumbProducer.stop_me = true;
330 void KThumb::removeAudioThumb() {
331 if (m_thumbFile.isEmpty()) return;
333 QFile f(m_thumbFile);
337 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth) {
339 if ((thumbProducer.isRunning() && thumbProducer.isWorking()) || channel == 0) {
343 QMap <int, QMap <int, QByteArray> > storeIn;
344 //FIXME: Hardcoded!!!
345 int m_frequency = 48000;
346 int m_channels = channel;
348 QFile f(m_thumbFile);
349 if (f.open(QIODevice::ReadOnly)) {
350 QByteArray channelarray = f.readAll();
352 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
353 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
357 kDebug() << "reading audio thumbs from file";
358 for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
359 for (int c = 0;c < m_channels;c++) {
360 QByteArray m_array(arrayWidth, '\x00');
361 for (int i = 0; i < arrayWidth; i++)
362 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
363 storeIn[z][c] = m_array;
366 emit audioThumbReady(storeIn);
368 if (thumbProducer.isRunning()) return;
369 thumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
370 thumbProducer.start(QThread::LowestPriority);
371 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
375 void KThumb::customEvent(QEvent * event) {
376 if (event->type() == 10005) {
377 ProgressEvent* p = (ProgressEvent*) event;
378 m_clipManager->setThumbsProgress(m_url, p->value());
384 #include "kthumb.moc"