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>
44 #include "kdenlivesettings.h"
46 void MyThread::init(KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth)
50 f.setFileName(target);
53 m_frameLength = frameLength;
54 m_frequency = frequency;
55 m_channels = channels;
56 m_arrayWidth = arrayWidth;
60 bool MyThread::isWorking()
67 if (!f.open( QIODevice::WriteOnly )) {
68 kDebug()<<"++++++++ ERROR WRITING TO FILE: "<<f.fileName()<<endl;
69 kDebug()<<"++++++++ DISABLING AUDIO THUMBS"<<endl;
70 //TODO KdenliveSettings::setAudiothumbnails(false);
74 Mlt::Profile prof((char*) KdenliveSettings::current_profile().data());
75 Mlt::Producer m_producer(prof, m_url.path().toAscii().data());
78 /*TODO if (KdenliveSettings::normaliseaudiothumbs()) {
79 Mlt::Filter m_convert("volume");
80 m_convert.set("gain", "normalise");
81 m_producer.attach(m_convert);
84 /*TODO if (qApp->mainWidget())
85 QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
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 //TODO QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(val, 10005));
97 Mlt::Frame *mlt_frame = m_producer.get_frame();
98 if ( 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] = qAbs((*( m_pcm + c + i * m_samples / m_array.size() ))>>8);
115 f.write(QByteArray(m_arrayWidth,'\x00'));
124 //TODO QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
126 //TODO else QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(0, 10005));
130 #define _S(a) (a)>255 ? 255 : (a)<0 ? 0 : (a)
131 #define _R(y,u,v) (0x2568*(y) + 0x3343*(u)) /0x2000
132 #define _G(y,u,v) (0x2568*(y) - 0x0c92*(v) - 0x1a1e*(u)) /0x2000
133 #define _B(y,u,v) (0x2568*(y) + 0x40cf*(v)) /0x2000
135 KThumb::KThumb(KUrl url, int width, int height, QObject * parent, const char *name):QObject(parent), m_url(url), m_width(width), m_height(height)
137 kDebug()<<"+++++++++++ CREATING THMB PROD FOR: "<<url;
138 m_profile = new Mlt::Profile((char*) qstrdup(KdenliveSettings::current_profile().toUtf8()));
143 if (m_profile) delete m_profile;
144 if (thumbProducer.isRunning ()) thumbProducer.exit();
149 QPixmap KThumb::getImage(KUrl url, int width, int height)
151 if (url.isEmpty()) return QPixmap();
152 QPixmap pix(width, height);
153 kDebug()<<"+++++++++++ GET THMB IMG FOR: "<<url;
154 char *tmp = Render::decodedString(url.path());
155 Mlt::Profile prof((char*) KdenliveSettings::current_profile().data());
156 Mlt::Producer m_producer(prof, tmp);
159 if (m_producer.is_blank()) {
163 Mlt::Frame * m_frame;
164 mlt_image_format format = mlt_image_rgb24a;
165 Mlt::Filter m_convert(prof, "avcolour_space");
166 m_convert.set("forced", mlt_image_rgb24a);
167 m_producer.attach(m_convert);
168 //m_producer.seek(frame);
169 m_frame = m_producer.get_frame();
170 if (m_frame && m_frame->is_valid()) {
171 uint8_t *thumb = m_frame->get_image(format, width, height);
172 QImage image(thumb, width, height, QImage::Format_ARGB32);
173 if (!image.isNull()) {
174 pix = pix.fromImage(image);
176 else pix.fill(Qt::black);
178 if (m_frame) delete m_frame;
182 void KThumb::extractImage(int frame, int frame2)
184 if (m_url.isEmpty()) return;
185 QPixmap pix(m_width, m_height);
186 char *tmp = Render::decodedString(m_url.path());
187 Mlt::Producer m_producer(*m_profile, tmp);
190 if (m_producer.is_blank()) {
192 emit thumbReady(frame, pix);
195 Mlt::Frame * m_frame;
196 mlt_image_format format = mlt_image_rgb24a;
197 Mlt::Filter m_convert(*m_profile, "avcolour_space");
198 m_convert.set("forced", mlt_image_rgb24a);
199 m_producer.attach(m_convert);
201 m_producer.seek(frame);
202 m_frame = m_producer.get_frame();
203 if (m_frame && m_frame->is_valid()) {
204 uint8_t *thumb = m_frame->get_image(format, m_width, m_height);
205 QImage image(thumb, m_width, m_height, QImage::Format_ARGB32);
206 if (!image.isNull()) {
207 pix = pix.fromImage(image);
209 else pix.fill(Qt::black);
211 if (m_frame) delete m_frame;
212 emit thumbReady(frame, pix);
214 if (frame2 == -1) return;
215 m_producer.seek(frame2);
216 m_frame = m_producer.get_frame();
217 if (m_frame && m_frame->is_valid()) {
218 uint8_t *thumb = m_frame->get_image(format, m_width, m_height);
219 QImage image(thumb, m_width, m_height, QImage::Format_ARGB32);
220 if (!image.isNull()) {
221 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()
306 if (thumbProducer.isRunning ()) thumbProducer.stop_me = true;
310 void KThumb::removeAudioThumb()
312 if (m_thumbFile.isEmpty()) return;
314 QFile f(m_thumbFile);
318 void KThumb::getAudioThumbs(KUrl url, int channel, double frame, double frameLength, int arrayWidth){
320 if ((thumbProducer.isRunning () && thumbProducer.isWorking()) || channel == 0) {
324 QMap <int, QMap <int, QByteArray> > storeIn;
325 //FIXME: Hardcoded!!!
326 int m_frequency = 48000;
327 int m_channels = channel;
331 QCryptographicHash context(QCryptographicHash::Sha1);
332 context.addData((KFileItem(m_url,"text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
334 m_thumbFile = KdenliveSettings::currenttmpfolder() + context.result().toHex() + ".thumb";
338 QFile f(m_thumbFile);
339 if (f.open( QIODevice::ReadOnly )) {
340 QByteArray channelarray = f.readAll();
342 if (channelarray.size() != arrayWidth*(frame+frameLength)*m_channels) {
343 kDebug()<<"--- BROKEN THUMB FOR: "<<m_url.fileName()<<" ---------------------- "<<endl;
348 for (int z=(int) frame;z<(int) (frame+frameLength);z++) {
349 for (int c=0;c< m_channels;c++){
351 m_array.resize(arrayWidth);
352 for (int i = 0; i < arrayWidth; i++)
353 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
354 storeIn[z][c] = m_array;
357 emit audioThumbReady(storeIn);
360 if (thumbProducer.isRunning()) return;
361 thumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
362 thumbProducer.start(QThread::LowestPriority );