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()
68 if (!f.open( QIODevice::WriteOnly )) {
69 kDebug()<<"++++++++ ERROR WRITING TO FILE: "<<f.fileName()<<endl;
70 kDebug()<<"++++++++ DISABLING AUDIO THUMBS"<<endl;
71 //TODO KdenliveSettings::setAudiothumbnails(false);
75 Mlt::Profile prof((char*) KdenliveSettings::current_profile().data());
76 Mlt::Producer m_producer(prof, m_url.path().toAscii().data());
79 /*TODO if (KdenliveSettings::normaliseaudiothumbs()) {*/
80 Mlt::Filter m_convert(prof,"volume");
81 m_convert.set("gain", "normalise");
82 m_producer.attach(m_convert);
85 /*TODO if (qApp->mainWidget())
86 QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
90 kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
91 for (int z=(int) m_frame;z<(int) (m_frame+m_frameLength) && m_producer.is_valid();z++){
93 val=(int)((z-m_frame)/(m_frame+m_frameLength)*100.0);
94 if (last_val!=val & val > 1){
95 //TODO QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(val, 10005));
99 Mlt::Frame *mlt_frame = m_producer.get_frame();
100 if ( mlt_frame->is_valid() )
102 double m_framesPerSecond = mlt_producer_get_fps( m_producer.get_producer() ); //mlt_frame->get_double( "fps" );
103 int m_samples = mlt_sample_calculator( m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()) );
104 mlt_audio_format m_audioFormat = mlt_audio_pcm;
106 int16_t* m_pcm = mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples );
108 for (int c=0;c< m_channels;c++){
110 m_array.resize(m_arrayWidth);
111 for (uint i = 0; i < m_array.size(); i++){
112 m_array[i] = ( (*( m_pcm + c + i * m_samples / m_array.size() )) >> 9 ) +127/2 ;
118 f.write(QByteArray(m_arrayWidth,'\x00'));
127 //TODO QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
129 //TODO else QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(0, 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(KUrl url, int width, int height, QObject * parent, const char *name):QObject(parent), m_url(url), m_width(width), m_height(height)
140 kDebug()<<"+++++++++++ CREATING THMB PROD FOR: "<<url;
141 m_profile = new Mlt::Profile((char*) qstrdup(KdenliveSettings::current_profile().toUtf8()));
142 QCryptographicHash context(QCryptographicHash::Sha1);
143 context.addData((KFileItem(m_url,"text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
144 m_thumbFile = KdenliveSettings::currenttmpfolder() + context.result().toHex() + ".thumb";
145 kDebug() << "thumbfile=" << m_thumbFile;
150 if (m_profile) delete m_profile;
151 if (thumbProducer.isRunning ()) thumbProducer.exit();
156 QPixmap KThumb::getImage(KUrl url, int width, int height)
158 if (url.isEmpty()) return QPixmap();
159 QPixmap pix(width, height);
160 kDebug()<<"+++++++++++ GET THMB IMG FOR: "<<url;
161 char *tmp = Render::decodedString(url.path());
162 Mlt::Profile prof((char*) KdenliveSettings::current_profile().data());
163 Mlt::Producer m_producer(prof, tmp);
166 if (m_producer.is_blank()) {
170 Mlt::Frame * m_frame;
171 mlt_image_format format = mlt_image_rgb24a;
172 Mlt::Filter m_convert(prof, "avcolour_space");
173 m_convert.set("forced", mlt_image_rgb24a);
174 m_producer.attach(m_convert);
175 //m_producer.seek(frame);
176 m_frame = m_producer.get_frame();
177 if (m_frame && m_frame->is_valid()) {
178 uint8_t *thumb = m_frame->get_image(format, width, height);
179 QImage image(thumb, width, height, QImage::Format_ARGB32);
180 if (!image.isNull()) {
181 pix = pix.fromImage(image);
183 else pix.fill(Qt::black);
185 if (m_frame) delete m_frame;
189 void KThumb::extractImage(int frame, int frame2)
191 if (m_url.isEmpty()) return;
192 QPixmap pix(m_width, m_height);
193 char *tmp = Render::decodedString(m_url.path());
194 Mlt::Producer m_producer(*m_profile, tmp);
197 if (m_producer.is_blank()) {
199 emit thumbReady(frame, pix);
202 Mlt::Frame * m_frame;
203 mlt_image_format format = mlt_image_rgb24a;
204 Mlt::Filter m_convert(*m_profile, "avcolour_space");
205 m_convert.set("forced", mlt_image_rgb24a);
206 m_producer.attach(m_convert);
208 m_producer.seek(frame);
209 m_frame = m_producer.get_frame();
210 if (m_frame && m_frame->is_valid()) {
211 uint8_t *thumb = m_frame->get_image(format, m_width, m_height);
212 QImage image(thumb, m_width, m_height, QImage::Format_ARGB32);
213 if (!image.isNull()) {
214 pix = pix.fromImage(image);
216 else pix.fill(Qt::black);
218 if (m_frame) delete m_frame;
219 emit thumbReady(frame, pix);
221 if (frame2 == -1) return;
222 m_producer.seek(frame2);
223 m_frame = m_producer.get_frame();
224 if (m_frame && m_frame->is_valid()) {
225 uint8_t *thumb = m_frame->get_image(format, m_width, m_height);
226 QImage image(thumb, m_width, m_height, QImage::Format_ARGB32);
227 if (!image.isNull()) {
228 pix = pix.fromImage(image);
230 else pix.fill(Qt::black);
232 if (m_frame) delete m_frame;
233 emit thumbReady(frame2, pix);
237 void KThumb::getImage(KUrl url, int frame, int width, int height)
239 if (url.isEmpty()) return;
240 QPixmap image(width, height);
241 char *tmp = KRender::decodedString(url.path());
242 Mlt::Producer m_producer(tmp);
244 image.fill(Qt::black);
246 if (m_producer.is_blank()) {
247 emit thumbReady(frame, image);
250 Mlt::Filter m_convert("avcolour_space");
251 m_convert.set("forced", mlt_image_rgb24a);
252 m_producer.attach(m_convert);
253 m_producer.seek(frame);
254 Mlt::Frame * m_frame = m_producer.get_frame();
255 mlt_image_format format = mlt_image_rgb24a;
258 if (m_frame && m_frame->is_valid()) {
259 uint8_t *thumb = m_frame->get_image(format, width, height);
260 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
261 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
263 if (m_frame) delete m_frame;
264 emit thumbReady(frame, image);
267 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
269 if (url.isEmpty()) return;
270 QPixmap image(width, height);
271 char *tmp = KRender::decodedString(url.path());
272 Mlt::Producer m_producer(tmp);
274 image.fill(Qt::black);
276 if (m_producer.is_blank()) {
277 emit thumbReady(startframe, image);
278 emit thumbReady(endframe, image);
281 Mlt::Filter m_convert("avcolour_space");
282 m_convert.set("forced", mlt_image_rgb24a);
283 m_producer.attach(m_convert);
284 m_producer.seek(startframe);
285 Mlt::Frame * m_frame = m_producer.get_frame();
286 mlt_image_format format = mlt_image_rgb24a;
290 if (m_frame && m_frame->is_valid()) {
291 uint8_t *thumb = m_frame->get_image(format, width, height);
292 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
293 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
295 if (m_frame) delete m_frame;
296 emit thumbReady(startframe, image);
298 image.fill(Qt::black);
299 m_producer.seek(endframe);
300 m_frame = m_producer.get_frame();
302 if (m_frame && m_frame->is_valid()) {
303 uint8_t *thumb = m_frame->get_image(format, width, height);
304 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
305 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
307 if (m_frame) delete m_frame;
308 emit thumbReady(endframe, image);
311 void KThumb::stopAudioThumbs()
313 if (thumbProducer.isRunning ()) thumbProducer.stop_me = true;
317 void KThumb::removeAudioThumb()
319 if (m_thumbFile.isEmpty()) return;
321 QFile f(m_thumbFile);
325 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth){
327 if ((thumbProducer.isRunning () && thumbProducer.isWorking()) || channel == 0) {
331 QMap <int, QMap <int, QByteArray> > storeIn;
332 //FIXME: Hardcoded!!!
333 int m_frequency = 48000;
334 int m_channels = channel;
336 QFile f(m_thumbFile);
337 if (f.open( QIODevice::ReadOnly )) {
338 QByteArray channelarray = f.readAll();
340 if (channelarray.size() != arrayWidth*(frame+frameLength)*m_channels) {
341 kDebug()<<"--- BROKEN THUMB FOR: "<<m_url.fileName()<<" ---------------------- "<<endl;
345 kDebug() << "reading audio thumbs from file";
346 for (int z=(int) frame;z<(int) (frame+frameLength);z++) {
347 for (int c=0;c< m_channels;c++){
348 QByteArray m_array(arrayWidth,'\x00');
349 for (int i = 0; i < arrayWidth; i++)
350 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
351 storeIn[z][c] = m_array;
354 emit audioThumbReady(storeIn);
357 if (thumbProducer.isRunning()) return;
358 thumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
359 thumbProducer.start(QThread::LowestPriority );